classMyQueue { public: /** Initialize your data structure here. */ MyQueue() {} /** Push element x to the back of queue. */ voidpush(int x){ inStack.push(x); } /** Removes the element from in front of queue and returns that element. */ intpop(){ if (outStack.empty()) move(); int val = outStack.top(); outStack.pop();
return val; } /** Get the front element. */ intpeek(){ if (outStack.empty()) move(); return outStack.top(); } /** Returns whether the queue is empty. */ boolempty(){ return inStack.empty() && outStack.empty(); } private: stack<int> inStack, outStack; voidmove(){ while (!inStack.empty()) { outStack.push(inStack.top()); inStack.pop(); } } };
/** * 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(); */