{"id":116229,"date":"2020-03-02T11:06:05","date_gmt":"2020-03-02T11:06:05","guid":{"rendered":"https:\/\/www.softwaretestinghelp.com\/?page_id=116229"},"modified":"2025-04-01T08:29:15","modified_gmt":"2025-04-01T08:29:15","slug":"cpp-circular-queue-data-structure","status":"publish","type":"page","link":"https:\/\/www.softwaretestinghelp.com\/cpp-circular-queue-data-structure\/","title":{"rendered":"C++ Circular Queue Data Structure: Implementation &#038; Applications"},"content":{"rendered":"\n<p><strong>This Tutorial on C++ Circular Queue Data Structure Explains What is Circular Queue, What are the Basic Operations along with Implementations &amp; Applications:<\/strong><\/p>\n\n\n\n<p>A circular queue is an extension of the basic queue that we have discussed earlier. It&nbsp;is also known as \u201cRing buffer\u201d.<\/p>\n\n\n\n<p><strong>What is Circular Queue in C++?<\/strong><\/p>\n\n\n\n<p>A circular queue is a linear data structure that is used to store data items. It performs operations by following the FIFO (First In, First Out) approach and the last position in the queue is connected back to the first position to form a circle.<\/p>\n\n\n\n<p><strong>=&gt; <a href=\"https:\/\/www.softwaretestinghelp.com\/cpp-tutorials\/\">Look For The Entire C++ Training Series Here<\/a><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/Circular-Queue.png\"><img decoding=\"async\" width=\"650\" height=\"366\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/Circular-Queue.png\" alt=\"C++ Circular Queue\" class=\"wp-image-116242\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/Circular-Queue.png 650w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/Circular-Queue-300x169.png 300w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Circular Queue In C++<\/h2>\n\n\n\n<p><strong>The following diagram shows a circular queue.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/1.png\"><img decoding=\"async\" width=\"321\" height=\"288\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/1.png\" alt=\"circular queue\" class=\"wp-image-116230\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/1.png 321w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/1-300x269.png 300w\" sizes=\"(max-width: 321px) 100vw, 321px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>The above image shows a circular data structure of size 10. The first six elements are already in the queue and we see that the first position and last position are joined. Due to this arrangement, space doesn\u2019t go wasted as it happens in a linear queue.<\/p>\n\n\n\n<p>In a linear queue after the queue is full, we delete the elements from another end, and the status of the queue is still shown as full and we cannot insert more elements.<\/p>\n\n\n\n<p>In the circular queue, when the queue is full, and when we remove elements from the front since last and first positions are connected, we can insert the elements at the rear which was vacated by deleting the element.<\/p>\n\n\n\n<p>In the next section, we will learn about the basic operations of the circular queue.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span style=\"color: #ff6600;\">Basic Operations<\/span><\/h3>\n\n\n\n<p><strong>Some of the basic operations of the circular queue are as follows:<\/strong><\/p>\n\n\n\n<p><span style=\"color: #000000;\"><strong>Front: <\/strong>Returns the front position in the circular queue.<\/span><\/p>\n\n\n\n<p><span style=\"color: #000000;\"><strong>Rear: <\/strong>Returns the rear position in the circular queue.<\/span><\/p>\n\n\n\n<p><span style=\"color: #000000;\"><strong>Enqueue: <\/strong>Enqueue (value) is used to insert an element in the circular queue. The element is always inserted at the rear end of the queue.<\/span><\/p>\n\n\n\n<p><span style=\"color: #000000;\"><strong>We follow the following sequence of steps to insert a new element in the circular queue.<\/strong><\/span><\/p>\n\n\n\n<p><span style=\"color: #000000;\"><strong>#1)<\/strong> Check if the circular queue is full: test ((rear == SIZE-1 &amp;&amp; front == 0) || (rear == front-1)), where \u2018SIZE\u2019 is the size of the circular queue.<\/span><\/p>\n\n\n\n<p><span style=\"color: #000000;\"><strong>#2)<\/strong> If the circular queue is full then it displays a message as \u201cQueue is full\u201d. If queue is not full then, check if (rear == SIZE \u2013 1 &amp;&amp; front != 0). If it is true then set rear=0 and insert element.<\/span><\/p>\n\n\n\n<p><span style=\"color: #000000;\"><strong>Dequeue: <\/strong><\/span>Dequeue function is used to delete an element from the queue. In the circular queue, the element is always deleted from the front end. Given below is the sequence for dequeue operation in a circular queue.<\/p>\n\n\n\n<p><strong> Steps:<\/strong><\/p>\n\n\n\n<p><strong>#1)<\/strong> Check if the circular queue is Empty: check if (front==-1).<\/p>\n\n\n\n<p><strong>#2)<\/strong> If it is empty then display the message \u201cQueue is empty\u201d. If queue is not empty then perform step 3.<\/p>\n\n\n\n<p><strong>#3)<\/strong> Check if (front==rear). If it is true then set front=rear= -1 else check if (front==size-1), if it is true then set front=0 and return the element.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span style=\"color: #ff6600;\">Illustration<\/span><\/h3>\n\n\n\n<p>In this section, we will go through a detailed illustration of adding\/removing elements in the circular queue.<\/p>\n\n\n\n<p><strong>Consider the following circular queue of 5 elements as shown below:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/2.png\"><img decoding=\"async\" width=\"464\" height=\"169\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/2.png\" alt=\"circular queue of 5 elements \" class=\"wp-image-116231\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/2.png 464w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/2-300x109.png 300w\" sizes=\"(max-width: 464px) 100vw, 464px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, we insert item 1 in the queue.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/3.png\"><img decoding=\"async\" width=\"447\" height=\"134\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/3.png\" alt=\"insert item 1 in the queue\" class=\"wp-image-116232\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/3.png 447w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/3-300x90.png 300w\" sizes=\"(max-width: 447px) 100vw, 447px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, we insert an item with value 3.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/4-2.png\"><img decoding=\"async\" width=\"430\" height=\"150\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/4-2.png\" alt=\"insert an item with value 3\" class=\"wp-image-116233\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/4-2.png 430w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/4-2-300x105.png 300w\" sizes=\"(max-width: 430px) 100vw, 430px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>When we insert the elements to make a queue full, the representation will be as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/5.png\"><img decoding=\"async\" width=\"457\" height=\"143\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/5.png\" alt=\"insert the elements to make queue full\" class=\"wp-image-116234\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/5.png 457w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/5-300x94.png 300w\" sizes=\"(max-width: 457px) 100vw, 457px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Now we delete the two elements i.e. item 1 and item 3 from the queue as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/6.png\"><img decoding=\"async\" width=\"452\" height=\"161\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/6.png\" alt=\"delete the two elements item 1 and item 3 from the queue\" class=\"wp-image-116235\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/6.png 452w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/6-300x107.png 300w\" sizes=\"(max-width: 452px) 100vw, 452px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, we insert or enqueue element 11 in the circular queue as represented below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/7.png\"><img decoding=\"async\" width=\"406\" height=\"145\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/7.png\" alt=\"insert or enqueue element 11\" class=\"wp-image-116236\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/7.png 406w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/7-300x107.png 300w\" sizes=\"(max-width: 406px) 100vw, 406px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Again let us insert element 13 in the circular queue. The queue will look as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/8-1.png\"><img decoding=\"async\" width=\"452\" height=\"191\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/8-1.png\" alt=\"us insert element 13 \" class=\"wp-image-116237\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/8-1.png 452w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/8-1-300x127.png 300w\" sizes=\"(max-width: 452px) 100vw, 452px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>We see that in the circular queue we move or insert elements in a circle. Hence we can consume the entire space of the queue till it becomes full.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span style=\"color: #ff6600;\">Implementation<\/span><\/h3>\n\n\n\n<p>Let&#8217;s implement the circular queue using C++.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n#include&lt;iostream&gt; \nusing namespace std; \n   \nclass Queue { \n    public:\n    \/\/ Initialize front and rear \n    int rear, front; \n   \n    \/\/ Circular Queue \n    int size; \n    int *circular_queue; \n   \n    Queue(int sz) { \n       front = rear = -1; \n       size = sz; \n       circular_queue = new int&#x5B;sz]; \n    } \n   void enQueue(int elem); \n    int deQueue(); \n    void displayQueue(); \n}; \n   \n\/* Function to create Circular queue *\/\nvoid Queue::enQueue(int elem) \n{ \n    if ((front == 0 &amp;&amp; rear == size-1) || (rear == (front-1)%(size-1)))  { \n        cout&lt;&lt;&quot;\\nQueue is Full&quot;; \n        return; \n    } \n    else if (front == -1) {     \/* Insert First Element *\/\n         front = rear = 0; \n        circular_queue&#x5B;rear] = elem; \n    } \n   else if (rear == size-1 &amp;&amp; front != 0) { \n        rear = 0; \n        circular_queue&#x5B;rear] = elem; \n    } \n    else {  \n        rear++; \n        circular_queue&#x5B;rear] = elem; \n    } \n}\n\/\/ Function to delete element from Circular Queue \nint Queue::deQueue() \n{ \n    if (front == -1)  { \n        cout&lt;&lt;&quot;\\nQueue is Empty&quot;; \n        return -1; \n    } \n   \n    int data = circular_queue&#x5B;front]; \n    circular_queue&#x5B;front] = -1; \n    if (front == rear)  { \n        front = -1; \n        rear = -1; \n    } \n    else if (front == size-1) \n        front = 0; \n    else\n        front++; \n   \n    return data; \n} \n  \n\/\/display elements of Circular Queue \nvoid Queue::displayQueue() \n{ \n    if (front == -1) { \n        cout&lt;&lt;&quot;\\nQueue is Empty&quot;&lt;&lt;endl; \n        return; \n    } \n    cout&lt;&lt;&quot;\\nCircular Queue elements: &quot;; \n    if (rear &gt;= front) { \n        for (int i = front; i &lt;= rear; i++) \n            cout&lt;&lt;circular_queue&#x5B;i]&lt;&lt;&quot; &quot;; \n    } \n    Else  { \n        for (int i = front; i &lt; size; i++) \n            cout&lt;&lt;circular_queue&#x5B;i]&lt;&lt;&quot; &quot;; \n   \n        for (int i = 0; i &lt;= rear; i++) \n            cout&lt;&lt;circular_queue&#x5B;i]&lt;&lt;&quot; &quot;; \n    } \n} \n   \n\/\/main program\nint main() \n{ \n    Queue pq(5); \n   \n    \/\/ Insert elements in Circular Queue \n    pq.enQueue(2); \n    pq.enQueue(4); \n    pq.enQueue(6); \n    pq.enQueue(8); \n   \n    \/\/ Display elements present in Circular Queue \n    pq.displayQueue(); \n   \n    \/\/ Delete elements from Circular Queue \n    cout&lt;&lt;&quot;\\nElement Dequeued = &quot;&lt;&lt;pq.deQueue(); \n    cout&lt;&lt;&quot;\\nElement Dequeued = &quot;&lt;&lt;pq.deQueue(); \n   \n    pq.displayQueue(); \n   \n    pq.enQueue(10); \n    pq.enQueue(12); \n    pq.enQueue(14); \n   \n    pq.displayQueue(); \n   \n    pq.enQueue(10); \n    return 0; \n}\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/output1.png\"><img decoding=\"async\" width=\"335\" height=\"172\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/output1.png\" alt=\"Circular Queue Output\" class=\"wp-image-116238\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/output1.png 335w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/output1-300x154.png 300w\" sizes=\"(max-width: 335px) 100vw, 335px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Above shown is the output of circular queue operations. First, we add the elements and then dequeue or remove two elements. Next, we insert or enqueue three more elements in the circular queue. We see that unlike linear queue, the elements are added at the end of the queue.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span style=\"color: #ff6600;\">Linked List Implementation<\/span><\/h3>\n\n\n\n<p>Let&#8217;s discuss the linked list implementation of a circular queue now. Given below is the linked list implementation of the circular queue in C++. Note that we make use of struct to represent each node. The operations are the same as discussed before except that in this case, we have to perform them with respect to the linked list nodes.<\/p>\n\n\n\n<p><strong>The output shows the circular queue after enqueue operation, dequeue and also after the second enqueue operation.<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n#include&lt;iostream&gt; \nusing namespace std; \n   \nstruct Node \n{ \n    int data; \n    struct Node* link; \n}; \n   \nstruct PQueue \n{ \n    struct Node *front, *rear; \n}; \n   \n\/* this functions performs enqueue operation for circular queue *\/\nvoid enQueue(PQueue *pq,int elem) \n{ \n     \n   struct Node *temp = new Node; \n    temp-&gt;data = elem; \n    if (pq-&gt;front == NULL) \n        pq-&gt;front = temp; \n    else\n        pq-&gt;rear-&gt;link = temp; \n   \n    pq-&gt;rear = temp; \n    pq-&gt;rear-&gt;link = pq-&gt;front; \n}\n\/\/ This function performs dequeue operation for Circular Queue \nint deQueue(PQueue *pq) \n{ \n   if (pq-&gt;front == NULL) \n    { \n        cout&lt;&lt;&quot;Queue is empty!!&quot;; \n        return -1; \n    } \n   \n    int elem; \/\/ item to be dequeued\n    \/\/ item is the last node to be deleted \n    if (pq-&gt;front == pq-&gt;rear) \n    { \n        elem = pq-&gt;front-&gt;data; \n        free(pq-&gt;front); \n        pq-&gt;front = NULL; \n        pq-&gt;rear = NULL; \n    } \n    else  \/\/more than one nodes \n    { \n        struct Node *temp = pq-&gt;front; \n        elem = temp-&gt;data; \n        pq-&gt;front = pq-&gt;front-&gt;link; \n        pq-&gt;rear-&gt;link= pq-&gt;front; \n        free(temp); \n    } \n   \n    return elem ; \n} \n  \n\/\/display elements of Circular Queue \nvoid displayQueue(struct PQueue *pq) \n{ \n    struct Node *temp = pq-&gt;front; \n    while (temp-&gt;link != pq-&gt;front) \n    { \n        cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;; \n        temp = temp-&gt;link; \n    } \n    cout&lt;&lt;temp-&gt;data;\n} \n   \n\/\/main program\nint main() \n{ \n    \/\/ Create a circular queue and initialize front and rear \n    PQueue *pq = new PQueue; \n    pq-&gt;front = pq-&gt;rear = NULL; \n   \n    \/\/ Insert\/enqueue elements in Circular Queue \n    enQueue(pq, 1); \n    enQueue(pq, 3); \n    enQueue(pq, 5); \n   \n    cout&lt;&lt;&quot;\\nCircular Queue elements after enqueue operation: &quot;; \n    \/\/ Display elements in Circular Queue \n    displayQueue(pq); \n   \n    \/\/ Delete\/dequeue elements from Circular Queue \n    cout&lt;&lt;&quot;\\nDequeued Item: &quot;&lt;&lt;deQueue(pq); \n    cout&lt;&lt;&quot;\\nDequeued Item: &quot;&lt;&lt;deQueue(pq); \n   \n    cout&lt;&lt;&quot;\\nCircular Queue elements after two dequeue operation: &quot;; \n    \/\/ Remaining elements in Circular Queue after dequeue\n    displayQueue(pq); \n   \n    enQueue(pq, 7); \n    enQueue(pq, 9); \n    cout&lt;&lt;&quot;\\nCircular Queue elements after two enqueue operations: &quot;; \n    displayQueue(pq); \n   \n    return 0; \n}\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/output2.png\"><img decoding=\"async\" width=\"530\" height=\"143\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/output2.png\" alt=\"Linked list implementation - output\" class=\"wp-image-116239\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/output2.png 530w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/output2-300x81.png 300w\" sizes=\"(max-width: 530px) 100vw, 530px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Next implementation is a Java program to demonstrate circular queue using the linked list.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nimport java.util.* ; \n   \nclass Main \n{ \n\/\/ Node structure \nstatic class Node  {  \n    int data;  \n     Node  link;  \n} \n     \nstatic class CQueue {  \n     Node  front,  rear;  \n} \n     \n\/\/ Enqueue operation for circular queue  \nstatic void enQueue(CQueue  cq, int value)  {  \n     Node  temp = new Node();  \n    temp .data = value;  \n    if (cq .front ==  null)  \n        cq .front = temp;  \n    else\n        cq .rear .link = temp;  \n     \n    cq .rear = temp;  \n    cq .rear .link = cq .front;  \n}  \n     \n\/\/ Dequeue operation for Circular Queue  \nstatic  int deQueue(CQueue  cq)  {  \n    if (cq .front ==  null)  {  \n        System.out.printf (\"Queue is empty!!\");  \n        return Integer.MIN_VALUE;  \n    }  \n     int value; \/\/ Value to be dequeued  \n    \/\/ the last node to be deleted\n    if (cq.front == cq.rear)   {  \n        value = cq.front.data;  \n        cq.front =  null;  \n        cq.rear =  null;  \n    }  \n    else   {         \/\/ There are more than one nodes    \n         Node  temp = cq.front;  \n        value = temp.data;  \n        cq.front = cq.front.link;  \n        cq.rear.link= cq.front;  \n    }  \n    return value ;  \n}  \n \/\/ display the elements of Circular Queue  \nstatic void displayQueue( CQueue  cq)  \n{  \n     Node  temp = cq.front;  \n    while (temp.link != cq.front)  \n    {  \n        System.out.printf(\"%d \", temp.data);  \n        temp = temp.link;  \n    }  \n    System.out.printf(\"%d\", temp.data);  \n}  \n     \n\/*  main program *\/\npublic static void main(String args&#x5B;]) \n{  \n    \/\/ Create a queue and initialize front and rear  \n    CQueue  cq = new CQueue();  \n    cq.front = cq.rear =  null;  \n     \n    \/\/ Insert\/enqueue elements in Circular Queue  \n    enQueue(cq, 2);  \n    enQueue(cq, 4);  \n    enQueue(cq, 6);  \n     \n    System.out.print(\"\\nCircular Queue elements after Enqueue Operation:\");    \n    \/\/ Display elements in Circular Queue  \n    displayQueue(cq);  \n     \n    \/\/ Delete\/dequeue elements from Circular Queue  \n    System.out.printf(\"\\nDequeued Item = %d\", deQueue(cq));  \n    System.out.printf(\"\\nDequeued Item = %d\", deQueue(cq));  \n     \n    System.out.print(\"\\nCircular Queue elements after Dequeue Operation:\");\n    displayQueue(cq);  \n     \n    enQueue(cq, 8);  \n    enQueue(cq, 10);  \n    System.out.print(\"\\nCircular Queue elements after second Enqueue Operation:\");\n    displayQueue(cq);  \n     \n}  \n}\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/output3.png\"><img decoding=\"async\" width=\"537\" height=\"142\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/output3.png\" alt=\"Output\" class=\"wp-image-116240\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/output3.png 537w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/01\/output3-300x79.png 300w\" sizes=\"(max-width: 537px) 100vw, 537px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>The output of the above program is similar to the previous program.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span style=\"color: #ff6600;\">Applications<\/span><\/h3>\n\n\n\n<p><strong>Let&#8217;s discuss some of the applications of the circular queue.<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>CPU Scheduling: <\/strong>Operating system process that requires some event to occur or for some other processes to complete for execution is often maintained in a circular queue so that they execute one after the other when all the conditions are met or when all events occur.<\/li>\n\n\n\n<li><strong>Memory Management: <\/strong>Use of ordinary queues wastes memory space as already mentioned in our above discussion. Using a circular queue for memory management is beneficial for optimum memory usage.<\/li>\n\n\n\n<li><strong>Computer Controlled Traffic Signal System: <\/strong>Computerized traffic signals are often added to a circular queue so that they repeat themselves after the specified time interval has elapsed.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Circular queues fix the major disadvantage of a normal queue wherein we cannot insert elements when the rear pointer is at the end of the queue even when we delete the elements and space is emptied. In a circular queue, elements are arranged in a circular fashion, so that space is not wasted at all.<\/p>\n\n\n\n<p>We have also seen the major operations of the circular queue. Circular queues are mostly useful for scheduling purposes and applications like traffic signal systems where the signals glow in turns.<\/p>\n\n\n\n<p><em><strong>In our next tutorial, we will learn about the double-ended queues that are simply called \u201cdeque\u201d.<\/strong><\/em><\/p>\n\n\n\n<p><strong>=&gt; <a href=\"https:\/\/www.softwaretestinghelp.com\/cpp-tutorials\/\">Visit Here To Learn C++ From Scratch<\/a><\/strong><\/p>\n\r\n\t\t\t<div id=\"daexthefup-container\"\r\n\t\t\t\tclass=\"daexthefup-container daexthefup-layout-stacked daexthefup-alignment-center\"\r\n\t\t\t\tdata-post-id=\"116229\">\r\n\r\n\t\t\t\t<div class=\"daexthefup-feedback\">\r\n\t\t\t\t\t<div class=\"daexthefup-text\">\r\n\t\t\t\t\t\t<h3 class=\"daexthefup-title\">Was this helpful?<\/h3>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<div class=\"daexthefup-buttons-container\">\r\n\t\t\t\t\t\t<div class=\"daexthefup-buttons\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t<div class=\"daexthefup-yes daexthefup-button daexthefup-button-type-icon\" data-value=\"1\">\r\n\t\t\t\t\r\n                <svg>\r\n                    <defs>\r\n                        <style>.thumb-up-cls-1{fill:#c9c9c9;}.thumb-up-cls-2{fill:#e1e1e1;}.thumb-up-cls-3{fill:#676767;}<\/style>\r\n                    <\/defs>\r\n                    <g id=\"thumb_up\">\r\n                        <path class=\"thumb-up-cls-2 daexthefup-icon-circle\" d=\"m24,3c11.58,0,21,9.42,21,21s-9.42,21-21,21S3,35.58,3,24,12.42,3,24,3m0-1C11.85,2,2,11.85,2,24s9.85,22,22,22,22-9.85,22-22S36.15,2,24,2h0Z\" \/>\r\n                        <g>\r\n                            <rect class=\"thumb-up-cls-3 daexthefup-icon-secondary-color\" x=\"10\" y=\"20\" width=\"6\" height=\"15\" rx=\"1.5\" ry=\"1.5\" \/>\r\n                            <path class=\"thumb-up-cls-1 daexthefup-icon-primary-color\" d=\"m30.57,9.06l-.49-.1c-.81-.17-1.61.35-1.78,1.16l-5.3,11.74c-.17.81,3.16,1.61,3.97,1.78l1.96.41c.81.17,1.61-.35,1.78-1.16l2.18-10.27c.34-1.61-.7-3.21-2.31-3.56Z\" \/>\r\n                            <path class=\"thumb-up-cls-1 daexthefup-icon-primary-color\" d=\"m38.17,20h-18.67c-.83,0-1.5.67-1.5,1.5v12c0,.83.67,1.5,1.5,1.5h16.27c.71,0,1.33-.5,1.47-1.21l2.4-12c.19-.93-.53-1.8-1.47-1.8Z\" \/>\r\n                        <\/g>\r\n                    <\/g>\r\n                <\/svg>\t\t\t<\/div>\r\n\r\n\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t<div class=\"daexthefup-no daexthefup-button daexthefup-button-type-icon\" data-value=\"0\">\r\n\t\t\t\t\r\n                <svg>\r\n                    <defs>\r\n                        <style>.thumb-down-cls-1{fill:#c9c9c9;}.thumb-down-cls-2{fill:#e1e1e1;}.thumb-down-cls-3{fill:#676767;}<\/style>\r\n                    <\/defs>\r\n                    <g id=\"thumb_down\">\r\n                        <path class=\"thumb-down-cls-2 daexthefup-icon-circle\" d=\"m24,3c11.58,0,21,9.42,21,21s-9.42,21-21,21S3,35.58,3,24,12.42,3,24,3m0-1C11.85,2,2,11.85,2,24s9.85,22,22,22,22-9.85,22-22S36.15,2,24,2h0Z\" \/>\r\n                        <g>\r\n                            <rect class=\"thumb-down-cls-3 daexthefup-icon-secondary-color\" x=\"10\" y=\"13\" width=\"6\" height=\"15\" rx=\"1.5\" ry=\"1.5\" \/>\r\n                            <path class=\"thumb-down-cls-1 daexthefup-icon-primary-color\" d=\"m30.57,38.94l-.49.1c-.81.17-1.61-.35-1.78-1.16l-5.3-11.74c-.17-.81,3.16-1.61,3.97-1.78l1.96-.41c.81-.17,1.61.35,1.78,1.16l2.18,10.27c.34,1.61-.7,3.21-2.31,3.56Z\" \/>\r\n                            <path class=\"thumb-down-cls-1 daexthefup-icon-primary-color\" d=\"m38.17,28h-18.67c-.83,0-1.5-.67-1.5-1.5v-12c0-.83.67-1.5,1.5-1.5h16.27c.71,0,1.33.5,1.47,1.21l2.4,12c.19.93-.53,1.8-1.47,1.8Z\" \/>\r\n                        <\/g>\r\n                    <\/g>\r\n                <\/svg>\t\t\t<\/div>\r\n\r\n\t\t\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t<\/div>\r\n\r\n\t\t\t\t<div class=\"daexthefup-comment\">\r\n\t\t\t\t\t<div class=\"daexthefup-comment-top-container\">\r\n\t\t\t\t\t\t<label id=\"daexthefup-comment-label\" class=\"daexthefup-comment-label\"><\/label>\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"daexthefup-comment-character-counter-container\">\r\n\t\t\t\t\t\t\t\t<div id=\"daexthefup-comment-character-counter-number\"\r\n\t\t\t\t\t\t\t\t\tclass=\"daexthefup-comment-character-counter-number\"><\/div>\r\n\t\t\t\t\t\t\t\t<div class=\"daexthefup-comment-character-counter-text\"><\/div>\r\n\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<textarea id=\"daexthefup-comment-textarea\" class=\"daexthefup-comment-textarea\"\r\n\t\t\t\t\t\t\t\tplaceholder=\"Type your message\"\r\n\t\t\t\t\t\t\t\tmaxlength=\"\r\n\t\t\t\t\t\t\t\t400\t\t\t\t\t\t\t\t\t\"><\/textarea>\r\n\t\t\t\t\t<div class=\"daexthefup-comment-buttons-container\">\r\n\t\t\t\t\t\t<button class=\"daexthefup-comment-submit daexthefup-button\">Submit<\/button>\r\n\t\t\t\t\t\t<button class=\"daexthefup-comment-cancel daexthefup-button\">Cancel<\/button>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t<\/div>\r\n\r\n\t\t\t\t<div class=\"daexthefup-successful-submission-text\">Thanks for your feedback!<\/div>\r\n\r\n\t\t\t<\/div>\r\n\r\n\t\t\t","protected":false},"excerpt":{"rendered":"<p>This Tutorial on C++ Circular Queue Data Structure Explains What is Circular Queue, What are the Basic Operations along with Implementations &amp; Applications: A circular queue is an extension of the basic queue that we have discussed earlier. It&nbsp;is also known as \u201cRing buffer\u201d. What is Circular Queue in C++? &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"C++ Circular Queue Data Structure: Implementation &#038; Applications\" class=\"read-more button\" href=\"https:\/\/www.softwaretestinghelp.com\/cpp-circular-queue-data-structure\/#more-116229\" aria-label=\"Read more about C++ Circular Queue Data Structure: Implementation &#038; Applications\">Read more<\/a><\/p>\n","protected":false},"author":9,"featured_media":116242,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_acf_changed":false,"_helpful_pro_status":1,"footnotes":""},"categories":[403],"tags":[],"class_list":{"0":"post-116229","1":"page","2":"type-page","3":"status-publish","4":"has-post-thumbnail","6":"category-cpp"},"acf":[],"_links":{"self":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/116229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/comments?post=116229"}],"version-history":[{"count":0,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/116229\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/media\/116242"}],"wp:attachment":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/media?parent=116229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/categories?post=116229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/tags?post=116229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}