{"id":1011,"date":"2025-11-21T08:52:48","date_gmt":"2025-11-21T03:22:48","guid":{"rendered":"https:\/\/codexplained.in\/?p=1011"},"modified":"2025-11-21T08:52:48","modified_gmt":"2025-11-21T03:22:48","slug":"implementation-of-circular-queue-using-arrays","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=1011","title":{"rendered":"Implementation of  Circular Queue using Arrays"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Steps for Implementation:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Queue Representation<\/strong>:\n<ul class=\"wp-block-list\">\n<li>We need an array to hold the queue elements.<\/li>\n\n\n\n<li>Two pointers, <code>front<\/code> and <code>rear<\/code>, will indicate the start and end of the queue.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Enqueue Operation<\/strong>:\n<ul class=\"wp-block-list\">\n<li>We add an element at the rear of the queue.<\/li>\n\n\n\n<li>If the queue is full, we can&#8217;t add any more elements.<\/li>\n\n\n\n<li>If the queue isn&#8217;t full, we move the rear pointer forward in a circular manner (using modulo <code>%<\/code>).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Dequeue Operation<\/strong>:\n<ul class=\"wp-block-list\">\n<li>We remove an element from the front of the queue.<\/li>\n\n\n\n<li>If the queue is empty, we can&#8217;t remove any elements.<\/li>\n\n\n\n<li>After dequeuing, we move the front pointer forward in a circular manner.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Check for Empty and Full Conditions<\/strong>:\n<ul class=\"wp-block-list\">\n<li>A queue is <strong>empty<\/strong> if <code>front == -1<\/code>.<\/li>\n\n\n\n<li>A queue is <strong>full<\/strong> if <code>(rear + 1) % maxSize == front<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Display Operation<\/strong>:\n<ul class=\"wp-block-list\">\n<li>To display the queue, we need to traverse it starting from <code>front<\/code> to <code>rear<\/code> considering the circular nature.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Program in C<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#define MAX 5 \/\/ Define the maximum size of the circular queue\n\n\/\/ Circular Queue Structure\nstruct Circular Queue \n{\n    int items&#x5B;MAX];\n    int front, rear;\n};\n\n\/\/ Function to initialize the queue\nvoid initialize Queue(struct Circular Queue *q) \n{\n    q-&gt;front = -1;\n    q-&gt;rear = -1;\n}\n\n\/\/ Function to check if the queue is full\nint is Full(struct Circular Queue *q) \n{\n    if ((q-&gt;rear + 1) % MAX == q-&gt;front) \n{\n        return 1;\n    }\n    return 0;\n}\n\n\/\/ Function to check if the queue is empty\nint is Empty(struct Circular Queue *q) \n{\n    if (q-&gt;front == -1) \n{\n        return 1;\n    }\n    return 0;\n}\n\n\/\/ Function to add an element to the circular queue\nvoid enqueue(struct Circular Queue *q, int value) \n{\n    if (is Full(q)) \n{\n        printf(&quot;Queue is Full!\\n&quot;);\n    } \nelse \n{\n        if (q-&gt;front == -1) \/\/ Inserting the first element\n            q-&gt;front = 0;\n        q-&gt;rear = (q-&gt;rear + 1) % MAX;\n        q-&gt;items&#x5B;q-&gt;rear] = value;\n        printf(&quot;Inserted %d\\n&quot;, value);\n    }\n}\n\n\/\/ Function to remove an element from the circular queue\nint dequeue(struct Circular Queue *q) \n{\n    int element;\n    if (is Empty(q)) \n{\n        printf(&quot;Queue is Empty!\\n&quot;);\n        return -1;\n    } \nelse \n{\n        element = q-&gt;items&#x5B;q-&gt;front];\n        if (q-&gt;front == q-&gt;rear) \n{ \/\/ Queue has only one element\n            q-&gt;front = -1;\n            q-&gt;rear = -1;\n        } \nelse \n{\n            q-&gt;front = (q-&gt;front + 1) % MAX;\n        }\n        printf(&quot;Deleted %d\\n&quot;, element);\n        return element;\n    }\n}\n\n\/\/ Function to display the circular queue\nvoid display(struct Circular Queue *q) \n{\n    int i;\n    if (is Empty(q)) \n{\n        printf(&quot;Queue is Empty!\\n&quot;);\n    } \nelse \n{\n        printf(&quot;Front -&gt; %d\\n&quot;, q-&gt;front);\n        printf(&quot;Items -&gt; &quot;);\n        for (i = q-&gt;front; i != q-&gt;rear; i = (i + 1) % MAX) \n{\n            printf(&quot;%d &quot;, q-&gt;items&#x5B;i]);\n        }\n        printf(&quot;%d &quot;, q-&gt;items&#x5B;i]);\n        printf(&quot;\\nRear -&gt; %d\\n&quot;, q-&gt;rear);\n    }\n}\n\n\/\/ Main function\nint main() \n{\n    struct Circular Queue q;\n    initialize Queue(&amp;q);\n\n    \/\/ Test enqueue operation\n    enqueue(&amp;q, 10);\n    enqueue(&amp;q, 20);\n    enqueue(&amp;q, 30);\n    enqueue(&amp;q, 40);\n    enqueue(&amp;q, 50); \/\/ Queue is full now\n\n    \/\/ Test display operation\n    display(&amp;q);\n\n    \/\/ Test dequeue operation\n    dequeue(&amp;q);\n    dequeue(&amp;q);\n\n    \/\/ Display after two dequeue operations\n    display(&amp;q);\n\n    \/\/ Enqueue after dequeue operations\n    enqueue(&amp;q, 60);\n    display(&amp;q);\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>initializeQueue()<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Initializes the queue by setting both <code>front<\/code> and <code>rear<\/code> to <code>-1<\/code>, which indicates an empty queue.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>isFull()<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Checks if the queue is full by checking if <code>(rear + 1) % MAX == front<\/code>. This means that if the position just after <code>rear<\/code> is <code>front<\/code>, the queue is full.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>isEmpty()<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Checks if the queue is empty by verifying if <code>front == -1<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>enqueue()<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Adds an element to the rear of the queue.<\/li>\n\n\n\n<li>If the queue is empty, we set <code>front<\/code> to <code>0<\/code> because we are inserting the first element.<\/li>\n\n\n\n<li>Then, we update the <code>rear<\/code> to the next position using <code>(rear + 1) % MAX<\/code>.<\/li>\n\n\n\n<li>Finally, the value is inserted at the <code>rear<\/code> position.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>dequeue()<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Removes the front element from the queue.<\/li>\n\n\n\n<li>If the queue becomes empty after dequeuing (i.e., <code>front == rear<\/code>), we reset both pointers to <code>-1<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>display()<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Prints the queue from the front to the rear, accounting for the circular nature by looping through indices using modulo.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Sample Output:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nInserted 10\nInserted 20\nInserted 30\nInserted 40\nInserted 50\nQueue is Full!\nFront -&gt; 0\nItems -&gt; 10 20 30 40 50 \nRear -&gt; 4\nDeleted 10\nDeleted 20\nFront -&gt; 2\nItems -&gt; 30 40 50 \nRear -&gt; 4\nInserted 60\nFront -&gt; 2\nItems -&gt; 30 40 50 60 \nRear -&gt; 0\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Key Points:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Circular queues help in efficient use of space by reusing positions in the array once elements are dequeued.<\/li>\n\n\n\n<li>The implementation uses simple operations like modulo arithmetic to wrap around the array when needed.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\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>","protected":false},"excerpt":{"rendered":"<p>Steps for Implementation: Program in C Explanation: Sample Output: Key Points:<\/p>\n","protected":false},"author":45,"featured_media":0,"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-1011","post","type-post","status-publish","format-standard","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1011","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\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1011"}],"version-history":[{"count":3,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1011\/revisions"}],"predecessor-version":[{"id":1232,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1011\/revisions\/1232"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}