主要就是数据在两个栈里倒来倒去。
class MyQueue {
public:
stack<int>xstack;
stack<int>reversestack;
/** Initialize your data structure here. */
MyQueue() {
//xstack.push(0);
}
/** Push element x to the back of queue. */
void push(int x) {
while(!reversestack.empty()){
int i=reversestack.top();
xstack.push(i);
reversestack.pop();
}
xstack.push(x);
while(!xstack.empty()){
int i=xstack.top();
reversestack.push(i);
xstack.pop();
}
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
int i=reversestack.top();
reversestack.pop();
return i;
}
/** Get the front element. */
int peek() {
return reversestack.top();
}
/** Returns whether the queue is empty. */
bool empty() {
return reversestack.empty();
}
};
/**
* 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();
* bool param_4 = obj->empty();
*/