- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
//栈:先进后出
//队列:先进先出
// push把项压入堆栈顶部。
// pop移除堆栈顶部的对象,并作为此函数的值返回该对象。
public void push(int node) {
stack1.push(node);
}
public int pop() {
while(0 != stack1.size()){
stack2.push(stack1.pop());
}
int val = stack2.pop();
while(0 != stack2.size()){
stack1.push(stack2.pop());
}
return val;
}
}