fork download
  1. #include <iostream>
  2. #include <queue>
  3.  
  4. using namespace std;
  5.  
  6. class queue_stack
  7. {
  8. public:
  9. void push(int n);
  10. void pop();
  11. int peek();
  12. bool empty();
  13. private:
  14. queue<int> q1, q2;
  15. };
  16.  
  17. void queue_stack::push(int n)
  18. {
  19. q2.push(n);
  20. while (!q1.empty())
  21. {
  22. q2.push(q1.front());
  23. q1.pop();
  24. }
  25. q1.swap(q2);
  26. }
  27.  
  28. void queue_stack::pop()
  29. {
  30. q1.pop();
  31. }
  32.  
  33. int queue_stack::peek()
  34. {
  35. return q1.front();
  36. }
  37.  
  38. bool queue_stack::empty()
  39. {
  40. return q1.empty();
  41. }
  42.  
  43. void assert(int expected, int actual)
  44. {
  45. if (expected != actual)
  46. {
  47. cout << "Expected " << expected << " got " << actual << endl;
  48. }
  49. }
  50.  
  51. int main()
  52. {
  53. queue_stack s;
  54. s.push(1);
  55. assert(1, s.peek());
  56. s.push(2);
  57. assert(2, s.peek());
  58. s.push(3);
  59. assert(3, s.peek());
  60. s.pop();
  61. assert(2, s.peek());
  62. for (int i = 3; i <= 50; i++)
  63. {
  64. s.push(i);
  65. assert(i, s.peek());
  66. }
  67.  
  68. int i;
  69. for (i = 50; !s.empty(); i--)
  70. {
  71. assert(i, s.peek());
  72. s.pop();
  73. }
  74. assert(0, i);
  75. return 0;
  76. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
Standard output is empty