This is implemented using two queue:
Insertion:- O(1)
Deletion:- O(n)
classMyStack {public: /** Initialize your data structure here. */ queue<int> i; queue<int> d;MyStack() { } /** Push element x onto stack. */voidpush(int x) {i.push(x); } /** Removes the element on top of the stack and returns that element. */intpop() {int k;while(i.size()!=1){ k =i.front();d.push(k);i.pop(); } k =i.front();i.pop();while(!d.empty()){int s =d.front();i.push(s);d.pop(); }return k; } /** Get the top element. */inttop() {int k; k =i.back();return k; } /** Returns whether the stack is empty. */boolempty() {if(i.empty()){returntrue; }returnfalse; }};