classMyStack { public: /** Initialize your data structure here. */ MyStack() { } /** Push element x onto stack. */ voidpush(int x){ q.push(x); for(unsigned i = 0; i != q.size() - 1; ++i){ int tmp = q.front(); q.pop(); q.push(tmp); } } /** Removes the element on top of the stack and returns that element. */ intpop(){ int tmp = q.front(); q.pop(); return tmp; } /** Get the top element. */ inttop(){ return q.front(); } /** 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 { int curr_size; queue<int> q1,q2; public: /** Initialize your data structure here. */ MyStack() { curr_size =0; } /** Push element x onto stack. */ voidpush(int x){ q1.push(x); curr_size++; } /** Removes the element on top of the stack and returns that element. */ intpop(){ if(q1.empty()) return-1; while(q1.size()!=1){ q2.push(q1.front()); q1.pop(); } int temp = q1.front(); q1.pop(); curr_size--; queue<int> q = q1; q1 = q2; q2 = q; return temp; } /** Get the top element. */ inttop(){ return q1.back(); } /** Returns whether the stack is empty. */ boolempty(){ return q1.empty(); } };
/** * 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(); */