Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- class Stack
- {
- private:
- queue<int> q1,q2,q;
- int cur_size;
- public:
- Stack()
- {
- cur_size=0;
- }
- void push(int x)
- {
- cur_size++;
- q2.push(x);
- while(q1.empty()==false)
- {
- q2.push(q1.front());
- q1.pop();
- }
- q=q1;
- q1=q2;
- q2=q;
- }
- void pop()
- {
- if(q1.empty())
- return;
- q1.pop();
- cur_size--;
- }
- int top()
- {
- if(q1.empty())
- return -1;
- else
- return q1.front();
- }
- int size()
- {
- return cur_size;
- }
- };
- int main()
- {
- Stack s;
- s.push(1);
- s.push(2);
- s.push(3);
- cout << "current size: " << s.size() << endl;
- cout << s.top() << endl;
- s.pop();
- cout << s.top() << endl;
- s.pop();
- cout << s.top() << endl;
- cout << "current size: " << s.size() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment