classMyStack { public: /** Initialize your data structure here. */ MyStack() {} /** Push element x onto stack. */ voidpush(int x){ q.push(x); } /** Removes the element on top of the stack and returns that element. */ intpop(){ int n = q.size(); for (int i = 1; i < n; i++) { q.push(q.front()); q.pop(); } int val = q.front(); q.pop();
return val; } /** Get the top element. */ inttop(){ int val = pop(); q.push(val);
return val; } /** Returns whether the stack is empty. */ boolempty(){ return q.empty(); }
private: queue<int> q; };
/** * Your MyStack object will be instantiated and called as such: * MyStack* obj = new MyStack(); * obj->push(x); * int param_2 = obj->pop(); * int param_3 = obj->top(); * bool param_4 = obj->empty(); */
classMyStack { public: /** Initialize your data structure here. */ MyStack() {} /** Push element x onto stack. */ voidpush(int x){ q.push(x); int n = q.size(); for (int i = 1; i < n; i++) { q.push(q.front()); q.pop(); } } /** Removes the element on top of the stack and returns that element. */ intpop(){ int val = q.front(); q.pop();
return val; } /** Get the top element. */ inttop(){ return q.front(); } /** Returns whether the stack is empty. */ boolempty(){ return q.empty(); }