Adrita

stack using queues

Aug 17th, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class Stack
  5. {
  6. private:
  7. queue<int> q1,q2,q;
  8. int cur_size;
  9. public:
  10. Stack()
  11. {
  12. cur_size=0;
  13. }
  14. void push(int x)
  15. {
  16. cur_size++;
  17. q2.push(x);
  18. while(q1.empty()==false)
  19. {
  20. q2.push(q1.front());
  21. q1.pop();
  22. }
  23. q=q1;
  24. q1=q2;
  25. q2=q;
  26. }
  27. void pop()
  28. {
  29. if(q1.empty())
  30. return;
  31. q1.pop();
  32. cur_size--;
  33. }
  34. int top()
  35. {
  36. if(q1.empty())
  37. return -1;
  38. else
  39. return q1.front();
  40. }
  41. int size()
  42. {
  43. return cur_size;
  44. }
  45. };
  46.  
  47. int main()
  48. {
  49. Stack s;
  50. s.push(1);
  51. s.push(2);
  52. s.push(3);
  53. cout << "current size: " << s.size() << endl;
  54. cout << s.top() << endl;
  55. s.pop();
  56. cout << s.top() << endl;
  57. s.pop();
  58. cout << s.top() << endl;
  59.  
  60. cout << "current size: " << s.size() << endl;
  61. return 0;
  62.  
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment