fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Queue
  6. {
  7. private:
  8. int sz ;
  9. int capacity ;
  10. int* arr ;
  11. int last ;
  12. int top ;
  13. public:
  14.  
  15. Queue()
  16. {
  17. sz = 0 ;
  18. capacity = 3 ;
  19. arr = new int [capacity] ;
  20. last = 0 ;
  21. top = 0 ;
  22. }
  23.  
  24. void push(int x)
  25. {
  26. if(last == capacity)
  27. {
  28. int oldcap = capacity ;
  29. capacity *= 2 ;
  30. int *temp = new int [capacity] ;
  31. for(int i = 0 ; i < oldcap ; ++i)
  32. {
  33. temp[i] = arr[i] ;
  34. }
  35. delete [] arr ;
  36. arr = temp ;
  37. }
  38. arr[last] = x ;
  39. last++ ;
  40. sz++ ;
  41.  
  42. }
  43.  
  44. void pop()
  45. {
  46. if(Empty())
  47. {
  48. cout << "underflow" << endl;
  49. return;
  50. }
  51. top++ ;
  52. sz-- ;
  53. }
  54.  
  55. int Top()
  56. {
  57. return arr[top] ;
  58. }
  59.  
  60. bool Empty()
  61. {
  62. if(top == sz) return true ;
  63.  
  64. return false ;
  65. }
  66.  
  67. int getSz()
  68. {
  69. return sz ;
  70. }
  71.  
  72. void print()
  73. {
  74. for(int i = top ; i < last ; ++i)
  75. {
  76. cout << arr[i] << ' ' ;
  77. }
  78. cout << endl;
  79. }
  80. };
  81.  
  82. int main()
  83. {
  84. Queue q ;
  85. q.push(2);
  86. q.push(6);
  87. q.push(1);
  88. q.push(0);
  89. q.push(90);
  90. q.print() ;
  91. q.pop() ;
  92. q.print() ;
  93.  
  94. q.pop();
  95. q.pop();
  96. q.pop();
  97. q.pop();
  98. q.pop();
  99. if(q.Empty())
  100. {
  101. cout << "Queue is Empty" << endl;
  102. }
  103.  
  104. q.push(123);
  105. q.push(124);
  106. q.push(125);
  107. q.push(126);
  108. q.push(127);
  109. q.push(128);
  110. cout << q.getSz() << endl;
  111.  
  112. cout << q.Top() << endl;
  113. q.pop() ;
  114. cout << q.Top() << endl;
  115.  
  116. }
  117.  
Success #stdin #stdout 0.01s 5308KB
stdin
Standard input is empty
stdout
2 6 1 0 90 
6 1 0 90 
5
124
125