We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include <stack>#include <iostream>usingnamespacestd;intmain(){stack<int>Front,Rear;intQ;cin>>Q;while(Q--){inttype,x;cin>>type;if(type==1){cin>>x;Rear.push(x);}else{if(Front.empty()){// move all the elements from "Rear" stack to "Front" stackwhile(!Rear.empty()){Front.push(Rear.top());Rear.pop();}}if(!Front.empty()){if(type==2)Front.pop();if(type==3)cout<<Front.top()<<endl;}}}return0;}
Queue using Two Stacks
You are viewing a single comment's thread. Return to all comments →
// Implementation of "queue" using two stacks