mplement the following operations of a stack using queues.
push to back
, peek/pop from front
, size
, andis empty
operations are valid.
class Stack { public: queue<int> myQueue; void push(int x) { int n = myQueue.size(); myQueue.push(x); for(int i = 0; i < n; i ++) { int tmp = myQueue.front(); myQueue.pop(); myQueue.push(tmp); } } // Removes the element on top of the stack. void pop() { myQueue.pop(); } // Get the top element. int top() { return myQueue.front(); } // Return whether the stack is empty. bool empty() { return myQueue.empty(); } };