Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MyQueue {
- public:
- stack <int> s1; //Primary Stack
- stack <int> s2; //Secondary Stack
- MyQueue() {
- }
- void push(int x) {
- s1.push(x);
- }
- int pop() {
- while(s1.size()!=0){
- s2.push(s1.top());
- s1.pop();
- }
- int ans = s2.top();
- s2.pop();
- while(s2.size()!=0){
- s1.push(s2.top());
- s2.pop();
- }
- return ans;
- }
- int peek() {
- while(s1.size()!=0){
- s2.push(s1.top());
- s1.pop();
- }
- int ans = s2.top();
- while(s2.size()!=0){
- s1.push(s2.top());
- s2.pop();
- }
- return ans;
- }
- bool empty() {
- if(s1.size()==0){
- return true;
- }
- return false;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement