public class stack { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push(int node) { stack1.push(node); } public int pop() { int node=0; int len=stack1.size(); for(int i=0;i<len;i++){ node=stack1.pop(); stack2.push(node); } node=stack2.pop(); for (int i = 0; i <stack2.size() ; i++) { int temp=stack2.pop(); stack1.push(temp); } return node; } }
基本思路在储存元素的时候,先把元素储存在一个栈里。
在需要取出元素的时候,第一个栈pop;第二个栈push
最后第二个栈pop
就可以达到两个栈模拟队列
以上纯属个人思路不喜勿喷