C++ Program to Implement Circular Queue

This C++ Program demonstrates the implementation of Circular Queue.

Here is source code of the C++ Program to demonstrate the implementation of Circular Queue. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Implement Circular Queue
  3.  */
  4. #include <iostream>
  5. #define MAX 5
  6. using namespace std;
  7. /*
  8.  * Class Circular Queue
  9.  */
  10. class Circular_Queue
  11. {
  12.     private:
  13.         int *cqueue_arr;
  14.         int front, rear;
  15.     public:
  16.         Circular_Queue()
  17.         {
  18.             cqueue_arr = new int [MAX];
  19.             rear = front = -1;
  20.         }
  21.         /*
  22.          * Insert into Circular Queue
  23.          */
  24.         void insert(int item)
  25.         {
  26.             if ((front == 0 && rear == MAX-1) || (front == rear+1))
  27.             {
  28.                 cout<<"Queue Overflow \n";
  29.                 return;
  30.             }
  31.             if (front == -1)
  32.             {
  33.                 front = 0;
  34.                 rear = 0;
  35.             }
  36.             else
  37.             {
  38.                 if (rear == MAX - 1)
  39.                     rear = 0;
  40.                 else
  41.                     rear = rear + 1;
  42.             }
  43.             cqueue_arr[rear] = item ;
  44.         }
  45.         /*
  46.          * Delete from Circular Queue
  47.          */
  48.         void del()
  49.         {
  50.             if (front == -1)
  51.             {
  52.                 cout<<"Queue Underflow\n";
  53.                 return ;
  54.             }
  55.             cout<<"Element deleted from queue is : "<<cqueue_arr[front]<<endl;
  56.             if (front == rear)
  57.             {
  58.                 front = -1;
  59.                 rear = -1;
  60.             }
  61.             else
  62.             {
  63.                 if (front == MAX - 1)
  64.                     front = 0;
  65.                 else
  66.                     front = front + 1;
  67.             }
  68.         }
  69.         /*
  70.          * Display Circular Queue
  71.          */
  72.         void display()
  73.         {
  74.             int front_pos = front, rear_pos = rear;
  75.             if (front == -1)
  76.             {
  77.                 cout<<"Queue is empty\n";
  78.                 return;
  79.             }
  80.             cout<<"Queue elements :\n";
  81.             if (front_pos <= rear_pos)
  82.             {
  83.                 while (front_pos <= rear_pos)
  84.                 {
  85.                     cout<<cqueue_arr[front_pos]<<"  ";
  86.                     front_pos++;
  87.                 }
  88.             }
  89.             else
  90.             {
  91.                 while (front_pos <= MAX - 1)
  92.                 {
  93.                     cout<<cqueue_arr[front_pos]<<"  ";
  94.                     front_pos++;
  95.                 }
  96.                 front_pos = 0;
  97.                 while (front_pos <= rear_pos)
  98.                 {
  99.                     cout<<cqueue_arr[front_pos]<<"  ";
  100.                     front_pos++;
  101.                 }
  102.             }
  103.             cout<<endl;
  104.         }
  105. };
  106. /*
  107.  * Main
  108.  */
  109. int main()
  110. {
  111.     int choice, item;
  112.     Circular_Queue cq;
  113.     do
  114.     {
  115.         cout<<"1.Insert\n";
  116.         cout<<"2.Delete\n";
  117.         cout<<"3.Display\n";
  118.         cout<<"4.Quit\n";
  119.         cout<<"Enter your choice : ";
  120.         cin>>choice;
  121.         switch(choice)
  122.         {
  123.         case 1:
  124.             cout<<"Input the element for insertion in queue : ";
  125.             cin>>item;	
  126.             cq.insert(item);
  127. 	    break;
  128. 	case 2:
  129.             cq.del();
  130. 	    break;
  131.         case 3:
  132.             cq.display();
  133. 	    break;
  134. 	case 4:
  135. 	    break;
  136. 	default:
  137. 	    cout<<"Wrong choice\n";
  138. 	}/*End of switch*/
  139.     }
  140.     while(choice != 4);
  141.     return 0;
  142. }

$ g++ circular_queue.cpp
$ a.out
 
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the element for insertion in queue : 3
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the element for insertion in queue : 2
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the element for insertion in queue : 6
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the element for insertion in queue : 4
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the element for insertion in queue : 1
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 3
Queue elements :
3  2  6  4  1
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 2
Element deleted from queue is : 3
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 3
Queue elements :
2  6  4  1
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 4
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.