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