实现思路:用栈1(stack1)来实现队列的入队push操作(数据存在stack1),用stack2来实现队列的pop操作,将stack1的数据全部压入stack2,数据从stack2弹出实现队列的pop操作。
转载:https://blog.csdn.net/qq_23217629/article/details/51723295
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(new Integer(node));
}
public int pop() {
int pop;
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
pop=stack2.pop().intValue();
while(!stack2.isEmpty()){
stack1.push(stack2.pop());
}
return pop;
}
}