使用递归实现一个栈的逆置
栈的逆置的方法很多,常见的一种就是借助另一个栈实现,本文要求使用递归来实现栈的逆置,算法如下:
public void reverseStack(Stack<Integer>stack){if(!stack.isEmpty()){int temp=stack.pop();reverseStack(stack);addToBottom(stack,temp);}}public void addToBottom(Stack<Integer> stack,int x){if(stack.isEmpty()){stack.push(x);}else{int temp=stack.pop();addToBottom(stack,x);stack.push(temp);}}