class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
if (forwardStack.empty()) {
while (!reverseStack.empty()) {
forwardStack.push(reverseStack.top());
reverseStack.pop();
}
}
forwardStack.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
if (reverseStack.empty()) {
while (!forwardStack.empty()) {
reverseStack.push(forwardStack.top());
forwardStack.pop();
}
}
int top = reverseStack.top();
reverseStack.pop();
return top;
}
/** Get the front element. */
int peek() {
if (reverseStack.empty()) {
while (!forwardStack.empty()) {
reverseStack.push(forwardStack.top());
forwardStack.pop();
}
}
int top = reverseStack.top();
return top;
}
/** Returns whether the queue is empty. */
bool empty() {
return forwardStack.empty() && reverseStack.empty();
}
private:
stack<int> forwardStack;
stack<int> reverseStack;
};
/**
* 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();
*/