用兩個棧來實現(xiàn)一個隊列辕录,完成隊列的Push和Pop操作睦霎。 隊列中的元素為int類型。
棧是后進先出走诞,隊列是先進先出副女,使用兩個棧,導(dǎo)一下即可蚣旱。
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
if(stack2.size() < 1){
while(stack1.size() > 0){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}