import java.util.Stack;
public class Test {
public static void main(String[] args) {
Stack<Integer> integers = new Stack<>();
integers.push(1);
integers.push(2);
integers.push(3);
integers.push(4);
reverse(integers);
System.out.println("integers = " + integers);
}
private 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;
}
}
private static void reverse(Stack<Integer> stack){
if(stack.isEmpty()){
return;
}
int i = f(stack);
reverse(stack);
stack.push(i);
}
}
复制代码
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END