You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
791 B
45 lines
791 B
package class17;
|
|
|
|
import java.util.Stack;
|
|
|
|
public class Code05_ReverseStackUsingRecursive {
|
|
|
|
public static void reverse(Stack<Integer> stack) {
|
|
if (stack.isEmpty()) {
|
|
return;
|
|
}
|
|
int i = f(stack);
|
|
reverse(stack);
|
|
stack.push(i);
|
|
}
|
|
|
|
// 栈底元素移除掉
|
|
// 上面的元素盖下来
|
|
// 返回移除掉的栈底元素
|
|
public static int f(Stack<Integer> stack) {
|
|
int result = stack.pop();
|
|
if (stack.isEmpty()) {
|
|
return result;
|
|
} else {
|
|
int last = f(stack);
|
|
stack.push(result);
|
|
return last;
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
Stack<Integer> test = new Stack<Integer>();
|
|
test.push(1);
|
|
test.push(2);
|
|
test.push(3);
|
|
test.push(4);
|
|
test.push(5);
|
|
reverse(test);
|
|
while (!test.isEmpty()) {
|
|
System.out.println(test.pop());
|
|
}
|
|
|
|
}
|
|
|
|
}
|