标签:
代码:
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();
}
}
Jan 12 - Implement Queue using Stacks; Stack; Queue Implementation;
标签:
原文地址:http://www.cnblogs.com/5683yue/p/5126125.html