代码:
class MyQueue {
// Push element x to the back of queue.
Stack<Integer> stack = new Stack<>();
Stack<Integer> aux = new Stack<>();
public void push(int x) {
while(!stack.isEmpty()){
aux.push(stack.pop());
}
stack.push(x);
while(!aux.isEmpty()){
stack.push(aux.pop());
}
}
// Removes the element from in front of queue.
public void pop() {
stack.pop();
}
// Get the front element.
public int peek() {
return stack.peek();
}
// Return whether the queue is empty.
public boolean empty() {
return stack.isEmpty();
}
}