用两个栈来操作,t存储队列的逆序,每次加入新元素就倒序使用s
class Queue {
public:
// Push element x to the back of queue.
void push(int x) {
stack<int> s;
while(!t.empty()) {
s.push(t.top());
t.pop();
}
s.push(x);
while(!s.empty()){
t.push(s.top());
s.pop();
}
}
// Removes the element from in front of queue.
void pop(void) {
t.pop();
}
// Get the front element.
int peek(void) {
return t.top();
}
// Return whether the queue is empty.
bool empty(void) {
return t.empty();
}
private:
stack<int> t;
};