此题与剑指offer第九题类似,都是用两个维护栈实现的队列操作
栈stack1维护队列的offer操作
栈stack2维护队列的poll操作
class MyQueue {
linkedList stack1=new linkedList();
linkedList stack2=new linkedList();
public MyQueue() {
}
public void push(int x) {
stack1.push(x);
}
public int pop() {
if(stack1.isEmpty()&&stack2.isEmpty()){
return -1;
}
if(stack2.isEmpty()){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
public int peek() {
int pee=pop();
stack2.push(pee);
return pee;
}
public boolean empty() {
if(stack2.isEmpty()&&stack1.isEmpty()){
return true;
}
return false;
}
}



