Implement the following operations of a queue using stacks.
Example:
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // returns 1
queue.pop(); // returns 1
queue.empty(); // returns false
Notes:
/**
* 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();
*/
class MyQueue {
private:
stack<int> stackPush, stackPop;
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
stackPush.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
if(stackPop.empty()) {
while(!stackPush.empty()) {
int temp = stackPush.top();
stackPush.pop();
stackPop.push(temp);
}
}
int result = stackPop.top();
stackPop.pop();
return result;
}
/** Get the front element. */
int peek() {
if(stackPop.empty()) {
while(!stackPush.empty()) {
int temp = stackPush.top();
stackPush.pop();
stackPop.push(temp);
}
}
return stackPop.top();
}
/** Returns whether the queue is empty. */
bool empty() {
return stackPop.empty() && stackPush.empty();
}
};