fork download
  1. #include<bits/stdc++.h>
  2. #define MAX_SIZE 5
  3. using namespace std;
  4.  
  5. class CircularQueue{
  6. public:
  7. /** Initialize your data structure here. */
  8. int queue[MAX_SIZE];
  9. int fPtr, rPtr;
  10. int qsize;
  11.  
  12. CircularQueue() {
  13. fPtr = rPtr = -1, qsize = 0;
  14. }
  15.  
  16. bool push(int &val){
  17. if(isFull()){
  18. cout<<"Queue is full.\n";
  19. return false;
  20. }
  21.  
  22. if(fPtr == -1){
  23. fPtr = rPtr = 0;
  24. }
  25. else{
  26. rPtr = (rPtr+1) % MAX_SIZE;
  27. }
  28.  
  29. queue[rPtr] = val;
  30.  
  31. ++qsize;
  32.  
  33. return true;
  34. }
  35.  
  36. int front(){
  37. return queue[fPtr];
  38. }
  39.  
  40. int rear(){
  41. return queue[rPtr];
  42. }
  43.  
  44. int size(){
  45. return qsize;
  46. }
  47.  
  48. bool isEmpty(){
  49. return fPtr == -1 && rPtr == -1;
  50. }
  51.  
  52. bool isFull(){
  53. return (rPtr+1) % MAX_SIZE == fPtr;
  54. }
  55.  
  56. int pop(){
  57.  
  58. if(isEmpty()){
  59. cout<<"Queue is empty.\n";
  60. return INT_MAX;
  61. }
  62.  
  63. int retVal = queue[fPtr];
  64.  
  65. if(rPtr == fPtr){
  66. rPtr = fPtr = -1;
  67. }
  68. else{
  69. fPtr = (fPtr+1) % MAX_SIZE;
  70. }
  71.  
  72. --qsize;
  73.  
  74. return retVal;
  75. }
  76.  
  77. void print(){
  78.  
  79. if(isEmpty()){
  80. cout<<"Queue is empty.\n";
  81. return;
  82. }
  83.  
  84. for(int i = fPtr, qSize = size(); qSize-- > 0; i = (i+1) % MAX_SIZE){
  85. cout<<queue[i]<<(i ^ rPtr ? "->" : "");
  86. }
  87.  
  88. cout<<"\n";
  89. }
  90. };
  91.  
  92.  
  93. int main() {
  94.  
  95. int opt, val;
  96.  
  97. CircularQueue cq;
  98.  
  99. cout<<"Operations available on the circular queue:\n";
  100. cout<<"1. Push\n2. Pop\n3. Front\n4. Rear\n"
  101. "5. Print from front to rear\n"
  102. "6. Is queue empty\n7. Exit\n";
  103.  
  104. while(true){
  105. cin>>opt;
  106.  
  107. switch(opt){
  108. case 1:
  109. cin>>val;
  110. cout<<"Element "<<val<<" is "<<(cq.push(val) ? "":"not")<<" pushed into the queue.\n";
  111. break;
  112. case 2:
  113. val = cq.pop();
  114. if(val == INT_MAX)
  115. cout<<"Front element can't be popped from the queue.\n";
  116. else
  117. cout<<"Front element "<<val<<" popped from the queue.\n";
  118. break;
  119. case 3:
  120. cout<<"The front element in the queue is: "<<cq.front()<<"\n";
  121. break;
  122. case 4:
  123. cout<<"The rear element in the queue is: "<<cq.rear()<<"\n";
  124. break;
  125. case 5:
  126. cout<<"The elements in the queue from front to rear:\n";
  127. cq.print();
  128. break;
  129. case 6:
  130. cout<<"Queue is "<<(cq.isEmpty() ? "": "not")<<" empty.\n";
  131. break;
  132. case 7:
  133. exit(0);
  134. }
  135. cout<<"==========================================================\n";
  136. }
  137.  
  138. return 0;
  139. }
Success #stdin #stdout 0s 4632KB
stdin
1 2
1 3
1 3
1 5
1 235
5
2
2
2
2
2
5
1 45
1 23
2
2
1 35
1 23
6
3
4
5
7
stdout
Operations available on the circular queue:
1. Push
2. Pop
3. Front
4. Rear
5. Print from front to rear
6. Is queue empty
7. Exit
Element 2 is  pushed into the queue.
==========================================================
Element 3 is  pushed into the queue.
==========================================================
Element 3 is  pushed into the queue.
==========================================================
Element 5 is  pushed into the queue.
==========================================================
Element 235 is  pushed into the queue.
==========================================================
The elements in the queue from front to rear:
2->3->3->5->235
==========================================================
Front element 2 popped from the queue.
==========================================================
Front element 3 popped from the queue.
==========================================================
Front element 3 popped from the queue.
==========================================================
Front element 5 popped from the queue.
==========================================================
Front element 235 popped from the queue.
==========================================================
The elements in the queue from front to rear:
Queue is empty.
==========================================================
Element 45 is  pushed into the queue.
==========================================================
Element 23 is  pushed into the queue.
==========================================================
Front element 45 popped from the queue.
==========================================================
Front element 23 popped from the queue.
==========================================================
Element 35 is  pushed into the queue.
==========================================================
Element 23 is  pushed into the queue.
==========================================================
Queue is not empty.
==========================================================
The front element in the queue is: 35
==========================================================
The rear element in the queue is: 23
==========================================================
The elements in the queue from front to rear:
35->23
==========================================================