用两个栈实现队列
class MyQueue {
//使用两个栈实现队列
//主栈
private Deque<Integer> deque1;
//辅助栈
private Deque<Integer> deque2;
public MyQueue() {
deque1 = new LinkedList<>();
deque2 = new LinkedList<>();
}
public void push(int x) {
while(deque1.size() != 0){
deque2.offerLast(deque1.pollLast());
}
deque1.offerLast(x);
while(deque2.size() != 0){
deque1.offerLast(deque2.pollLast());
}
}
public int pop() {
return deque1.pollLast();
}
public int peek() {
return deque1.peekLast();
}
public boolean empty() {
return deque1.size() == 0;
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/