

{"id":59894,"date":"2019-06-19T16:15:34","date_gmt":"2019-06-19T10:45:34","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=59894"},"modified":"2022-12-30T09:37:18","modified_gmt":"2022-12-30T04:07:18","slug":"stacks-and-queues-in-c","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/stacks-and-queues-in-c\/","title":{"rendered":"Stacks and Queues in C &#8211; Master the Concepts of LIFO &amp; FIFO"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:1479,&quot;href&quot;:&quot;https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/LIFO&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20251207042809\\\/https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/LIFO&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 08:38:12&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-12 08:51:31&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-15 21:09:52&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-19 03:46:35&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-22 05:27:53&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-25 07:38:29&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-28 18:23:31&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-05 10:06:30&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-09 22:08:57&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-13 09:50:56&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-17 16:38:00&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-21 11:40:02&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-24 11:52:23&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-28 03:47:48&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-31 08:57:01&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-03 13:56:17&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-08 12:36:21&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-12 10:34:44&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-17 05:06:48&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-20 09:54:36&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-23 14:07:05&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-27 03:16:21&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-02 06:01:23&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-05 07:08:32&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-08 18:19:55&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-12 09:33:55&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-15 10:54:21&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-18 15:28:02&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-21 15:57:47&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-24 22:52:02&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-29 08:35:28&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-01 09:30:26&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-04-05 18:48:52&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-08 19:32:00&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-12 04:35:29&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-15 05:10:08&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-18 18:31:33&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-21 19:35:57&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-25 04:24:19&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-28 05:04:31&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-01 10:53:10&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-04 14:18:51&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-07 16:49:01&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-10 23:09:55&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-14 11:24:44&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-05-19 04:39:20&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-22 08:16:01&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-26 01:54:27&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-30 09:36:32&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-03 10:27:34&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-08 12:28:02&quot;,&quot;http_code&quot;:404}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-08 12:28:02&quot;,&quot;http_code&quot;:404},&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p>After getting well-versed with linked lists and <strong><em><a href=\"https:\/\/data-flair.training\/blogs\/arrays-in-c\/\">arrays in C<\/a><\/em><\/strong>, you are now ready to explore a new concept, that is stacks and queues. Both stacks and queues in C are data structures that can be implemented using either arrays or linked lists.<\/p>\n<p>Newbies to programming often find it cumbersome to implement stacks and queues in C as it requires a thorough knowledge of all the concepts of C that we have covered so far.\u00a0So, we will understand each and every important concept involved in stacks and queues in C in detail so that you would develop a clear understanding of the topic.<\/p>\n<p>In this\u00a0Stacks and Queues in C tutorial, we will discuss:<\/p>\n<ul>\n<li>Stack in C<\/li>\n<li>Array implementation of a Stack<\/li>\n<li>Linked list implementation of a Stack<\/li>\n<li>Applications of Stack<\/li>\n<li>Queue in C<\/li>\n<li>Array implementation of a Queue<\/li>\n<li>Linked list implementation of a Queue<\/li>\n<li>Circular Queue<\/li>\n<li>Applications of Queue<\/li>\n<\/ul>\n<p>If we want to learn this concept then we have to go deep inside it. That&#8217;s why we are going to discuss its key topics, which will be helping us to grasp the concept in an efficient way. So let&#8217;s start &#8211;<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Stacks-and-queues-in-C.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59995\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Stacks-and-queues-in-C.jpg\" alt=\"Stacks and queues in C\" width=\"802\" height=\"420\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Stacks-and-queues-in-C.jpg 802w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Stacks-and-queues-in-C-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Stacks-and-queues-in-C-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Stacks-and-queues-in-C-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Stacks-and-queues-in-C-520x272.jpg 520w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/a><\/p>\n<h2>1. What is Stack in C?<\/h2>\n<p><em>A stack in C is nothing but a linear data structure that follows the <strong>LIFO<\/strong> rule (Last In First Out). In a stack, both insertion and deletion take place from just one end, that is, from the top.<\/em><\/p>\n<p>In order to better understand it, consider the following scenario: Imagine you have 5 plates that you have to keep on top of each other of distinct colors: Red, Green, Blue, White, and Orange.<\/p>\n<p>You start by placing the red plate on the table. This is the first element of the stack. Then, you place the green plate on top of the red plate. This is the second element of the stack. Similarly, you place the blue plate followed by white and then finally orange.\u00a0Note that the first plate you inserted into the stack was the red one. Now, you want to remove the red plate. But, before that, you need to remove the rest of the plates that are on top of the red one.<\/p>\n<p>From this discussion, it is pretty obvious that the first plate (first data element) to be inserted is removed at the last. And, the last plate to be inserted is removed at first, that is, it follows the \u201cLast In First Out\u201d rule. Also, we infer that placing and removing the plate is done from the top, that is, insertion and deletion are done from the top<\/p>\n<h4><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Stack-insertion-and-deletion.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59981\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Stack-insertion-and-deletion.jpg\" alt=\"Insertion and Deletion of Element in C Stack\" width=\"800\" height=\"600\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Stack-insertion-and-deletion.jpg 800w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Stack-insertion-and-deletion-150x113.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Stack-insertion-and-deletion-300x225.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Stack-insertion-and-deletion-768x576.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Stack-insertion-and-deletion-520x390.jpg 520w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/a><\/h4>\n<h4>We can implement a stack in C in 2 ways:<\/h4>\n<ol>\n<li><strong>Statically:<\/strong> Array implementation of stacks allows the static memory allocation of its data elements. It is important to note that in this method, the stack acquires all the features of an array.<\/li>\n<li><strong>Dynamically:<\/strong> Linked list implementation of stacks follow the dynamic memory allocation of its data elements. It is important to note that in this method, the stack inherits all the characteristics of a<strong> <em><a href=\"https:\/\/data-flair.training\/blogs\/linked-list-in-c-tutorial\/\">linked list in C<\/a><\/em>.<\/strong><\/li>\n<\/ol>\n<h3>1.1 Array Implementation of Stack in C<\/h3>\n<p>As we already discussed,<em> arrays support the static memory allocation of the data elements of the stack<\/em>. Therefore, it is important to determine the size of the stack prior to the program run.<\/p>\n<p><strong>The C stack functions basically include:<\/strong><\/p>\n<h4>1.1.1 Insertion<\/h4>\n<p>In a stack, the operation of inserting an element into the stack is referred to as pushing an element in the stack. The elements are inserted into the stack from the top and hence would compel the elements to shift.<\/p>\n<p><strong>Here is a diagrammatic representation of how elements are pushed into a stack:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Elements-are-pushed-into-a-stack-in-C.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-60014\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Elements-are-pushed-into-a-stack-in-C.jpg\" alt=\"Insertion in C Stack\" width=\"600\" height=\"400\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Elements-are-pushed-into-a-stack-in-C.jpg 600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Elements-are-pushed-into-a-stack-in-C-150x100.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Elements-are-pushed-into-a-stack-in-C-300x200.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Elements-are-pushed-into-a-stack-in-C-520x347.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Elements-are-pushed-into-a-stack-in-C-272x182.jpg 272w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p><strong>This is how we can insert elements into a C stack:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#define LIMIT 100\r\nvoid push()\r\n{\r\nint stack[LIMIT], top, element;\r\nif(top == LIMIT- 1)\r\n{\r\nprintf(\"Stack Overflow\\n\");\r\n}\r\nelse\r\n{\r\nprintf(\"Enter the element to be inserted:\");\r\nscanf(\"%d\", &amp;element);\r\ntop++;\r\nstack[top]=element;\r\n}\r\n}<\/pre>\n<p><em><strong>Enhance your Fundamental Skills with <a href=\"https:\/\/data-flair.training\/blogs\/operators-in-c\/\">Different types of Operators in C<\/a><\/strong><\/em><\/p>\n<h4>1.1.2 Deletion<\/h4>\n<p>In a stack, the operation of deleting an element into the stack is referred to as popping an element in the stack. The deletion of a data element from the stack is done from the top.<\/p>\n<p><strong>Here is a diagrammatic representation of how elements are pushed into a stack:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Elements-are-pushed-into-a-stack-in-C-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-60016\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Elements-are-pushed-into-a-stack-in-C-1.jpg\" alt=\"Deletion in Stack in C\" width=\"600\" height=\"400\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Elements-are-pushed-into-a-stack-in-C-1.jpg 600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Elements-are-pushed-into-a-stack-in-C-1-150x100.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Elements-are-pushed-into-a-stack-in-C-1-300x200.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Elements-are-pushed-into-a-stack-in-C-1-520x347.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Elements-are-pushed-into-a-stack-in-C-1-272x182.jpg 272w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p><strong>This is how we can delete elements from a stack:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#define LIMIT 100\r\nvoid pop()\r\n{\r\nint stack[LIMIT], top, element;\r\nif(top == -1)\r\n{\r\nprintf(\"Stack underflow\\n\");\r\n}\r\nelse\r\n{\r\nelement=stack[top];\r\nprintf(\"The deleted item is %d\\n\",stack[top]);\r\ntop--; \/\/ The element below the topmost element is deleted\r\n}\r\n}\r\n<\/pre>\n<h4>1.1.3 Display<\/h4>\n<p>The stack data elements are displayed in the stack according to the LIFO rule.<\/p>\n<p><strong>This is how you can display the stack in C:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#define LIMIT 100\r\nvoid display()\r\n{\r\nint stack[LIMIT], top, i;\r\nif(top == -1)\r\n{\r\nprintf(\"Stack underflow\\n\"); \/\/ Stack is empty\r\n}\r\nelse if(top &gt; 0)\r\n{\r\nprintf(\"The elements of the stack are:\\n\");\r\nfor(i = top; i &gt;= 0; i--) \/\/ top to bottom traversal\r\n{\r\nprintf(\"%d\\n\",stack[i]);\r\n}\r\n}\r\n}<\/pre>\n<p>Apart from these 3 main functions, it is necessary to check the overflow and underflow conditions to avoid unfavorable situations.<\/p>\n<h4>1.1.4 Stack Overflow<\/h4>\n<p>Here we are talking about the static memory allocation of data elements of a stack. Therefore, if the stack is filled completely, that is, no more elements can be inserted in the stack, then the condition would be called STACK-FULL condition. It is also referred to as stack overflow.<\/p>\n<h4>1.1.5 Stack Underflow<\/h4>\n<p>In case we wish to display the data elements of the stack or perform the deletion operation, but no elements have been inserted into the stack yet, this condition is called STACK-EMPTY. It is also referred to as stack underflow.<\/p>\n<p><em><strong>Before discussing the example, let&#8217;s revise the concept of <a href=\"https:\/\/data-flair.training\/blogs\/variables-in-c\/\">Variables in C<\/a><\/strong><\/em><\/p>\n<p><strong>Here is a program in C that illustrates the array implementation of stacks:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#define LIMIT 100 \/\/ Specifying the maximum limit of the stack\r\n\r\n\/* Global declaration of variables *\/\r\n\r\nint stack[LIMIT]; \/\/ Array implementation of stack\r\nint top; \/\/ To insert and delete the data elements in the stack\r\nint i; \/\/ To traverse the loop to while displaying the stack\r\nint choice; \/\/ To choose either of the 3 stack operations\r\n\r\nvoid push(); \/\/ Function used to insert the element into the stack\r\nvoid pop(); \/\/ Function used to delete the element from the stack\r\nvoid display(); \/\/ Function used to display all the elements in the stack according to LIFO rule\r\n\r\nint main()\r\n{\r\n\r\nprintf(\"Welcome to DataFlair tutorials!\\n\\n\");\r\n\r\nprintf (\"ARRAY IMPLEMENTATION USING STACKS\\n\\n\");\r\ntop = -1; \/\/ Initializing top to -1 indicates that it is empty\r\ndo\r\n{\r\n\r\nprintf(\"1. Insert\\n2. Delete\\n3. Display\\n4. Exit\\n\\n\");\r\nprintf(\"Enter your choice:\");\r\nscanf(\"%d\",&amp;choice);\r\n\r\nswitch(choice)\r\n{\r\ncase 1:\r\npush();\r\nbreak;\r\ncase 2:\r\npop();\r\nbreak;\r\ncase 3:\r\ndisplay();\r\nbreak;\r\ncase 4:\r\nexit(0);\r\nbreak;\r\ndefault:\r\nprintf(\"Sorry, invalid choice!\\n\");\r\nbreak;\r\n}\r\n} while(choice!=4);\r\nreturn 0;\r\n}\r\n\r\nvoid push()\r\n{\r\nint element;\r\nif(top == LIMIT- 1)\r\n{\r\nprintf(\"Stack underflow\\n\");\r\n}\r\nelse\r\n{\r\nprintf(\"Enter the element to be inserted:\");\r\nscanf(\"%d\", &amp;element);\r\ntop++;\r\nstack[top]=element;\r\n}\r\n}\r\n\r\nvoid pop()\r\n{\r\nint element;\r\nif(top == -1)\r\n{\r\nprintf(\"Stack underflow\\n\");\r\n}\r\nelse\r\n{\r\nelement=stack[top];\r\nprintf(\"The deleted item is %d\\n\",stack[top]);\r\ntop--; \/\/ The element below the topmost element is deleted\r\n}\r\n}\r\n\r\nvoid display()\r\n{\r\nif(top == -1)\r\n{\r\nprintf(\"Stack underflow\\n\"); \/\/ Stack is empty\r\n}\r\nelse if(top &gt; 0)\r\n{\r\nprintf(\"The elements of the stack are:\\n\");\r\nfor(i = top; i &gt;= 0; i--) \/\/ top to bottom traversal\r\n{\r\nprintf(\"%d\\n\",stack[i]);\r\n}\r\n}\r\n}<\/pre>\n<p><strong>Output-<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-array-implementation-of-stacks.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59950\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-array-implementation-of-stacks.jpg\" alt=\"Output of array implementation of stacks in C\" width=\"1303\" height=\"740\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-array-implementation-of-stacks.jpg 1303w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-array-implementation-of-stacks-150x85.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-array-implementation-of-stacks-300x170.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-array-implementation-of-stacks-768x436.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-array-implementation-of-stacks-1024x582.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-array-implementation-of-stacks-520x295.jpg 520w\" sizes=\"auto, (max-width: 1303px) 100vw, 1303px\" \/><\/a><\/p>\n<h3><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Implementation-of-stacks-in-C-with-Output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59974\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Implementation-of-stacks-in-C-with-Output.png\" alt=\"Implementation of stacks in C with Output\" width=\"1305\" height=\"735\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Implementation-of-stacks-in-C-with-Output.png 1305w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Implementation-of-stacks-in-C-with-Output-150x84.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Implementation-of-stacks-in-C-with-Output-300x169.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Implementation-of-stacks-in-C-with-Output-768x433.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Implementation-of-stacks-in-C-with-Output-1024x577.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Implementation-of-stacks-in-C-with-Output-520x293.png 520w\" sizes=\"auto, (max-width: 1305px) 100vw, 1305px\" \/><\/a><\/h3>\n<h3>1.2 Linked List Implementation of Stack in C<\/h3>\n<p>As we already discussed, linked lists support the dynamic memory allocation of the data elements of the stack. Therefore, the size of the stack is allocated during the program run and needn\u2019t be specified beforehand.<\/p>\n<p>The 3 basic operations of Insertion, Deletion, and Display follow a similar trend as we saw in the array implementation of stacks.<\/p>\n<p><em><strong><a href=\"https:\/\/data-flair.training\/blogs\/functions-in-c\/\">Functions in C<\/a> &#8211; An Important Concept for beginners<\/strong><\/em><\/p>\n<p>The stack functions basically include:<\/p>\n<h4>1.2.1\u00a0 Insertion<\/h4>\n<p><strong>This is how we can insert elements into the stack in C:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">void push ()\r\n{\r\nint data;\r\nstruct node *pointer = (struct node*)malloc(sizeof(struct node));\r\nif(pointer == NULL)\r\n{\r\nprintf(\"Stack overflow\");\r\n}\r\nelse\r\n{\r\nprintf(\"Enter the element to be inserted: \");\r\nscanf(\"%d\",&amp;data);\r\nif(temp == NULL)\r\n{\r\npointer -&gt; data = data;\r\npointer -&gt; next = NULL;\r\ntemp = pointer;\r\n}\r\nelse\r\n{\r\npointer -&gt; data = data;\r\npointer -&gt; next = temp;\r\ntemp = pointer;\r\n}\r\n}\r\n}\r\n\r\n<\/pre>\n<h4>1.2.2 Deletion<\/h4>\n<p><strong>This is how we can delete elements from a stack in C:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">void pop()\r\n{\r\nint item;\r\nstruct node *pointer;\r\nif (temp == NULL)\r\n{\r\nprintf(\"Stack Underflow\\n\");\r\n}\r\nelse\r\n{\r\nitem = temp -&gt; data;\r\npointer = temp;\r\ntemp = temp -&gt; next;\r\nfree(pointer);\r\nprintf(\"The deleted item is %d\\n\",item);\r\n}\r\n}\r\n<\/pre>\n<h4>1.2.3 Display<\/h4>\n<p><strong>This is how we display the elements of a stack:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">void display()\r\n{\r\nint i;\r\nstruct node *pointer;\r\npointer = temp;\r\nif(pointer == NULL)\r\n{\r\nprintf(\"Stack underflow\\n\");\r\n}\r\nelse\r\n{\r\nprintf(\"The elements of the stack are:\\n\");\r\nwhile(pointer!= NULL)\r\n{\r\nprintf(\"%d\\n\",pointer -&gt; data);\r\npointer = pointer -&gt; next;\r\n}\r\n}\r\n}\r\n<\/pre>\n<p><strong>Here is a code in C that illustrates the linked list implementation of arrays:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nvoid push(); \/\/ Function used to insert the element into the stack\r\nvoid pop(); \/\/ Function used to delete the elememt from the stack\r\nvoid display(); \/\/ Function used to display all the elements in the stack according to LIFO rule\r\n\r\nstruct node\r\n{\r\nint data;\r\nstruct node *next;\r\n};\r\nstruct node *temp;\r\n\r\nint main()\r\n{\r\n\r\nprintf(\"Welcome to DataFlair tutorials!\\n\\n\");\r\n\r\nint choice;\r\nprintf (\"LINKED LIST IMPLEMENTATION USING STACKS\\n\\n\");\r\ndo\r\n{\r\n\r\nprintf(\"1. Insert\\n2. Delete\\n3. Display\\n4. Exit\\n\\n\");\r\nprintf(\"Enter your choice:\");\r\nscanf(\"%d\",&amp;choice);\r\n\r\nswitch(choice)\r\n{\r\ncase 1:\r\npush();\r\nbreak;\r\ncase 2:\r\npop();\r\nbreak;\r\ncase 3:\r\ndisplay();\r\nbreak;\r\ncase 4:\r\nexit(0);\r\nbreak;\r\ndefault:\r\nprintf(\"Sorry, invalid choice!\\n\");\r\nbreak;\r\n}\r\n} while(choice!=4);\r\nreturn 0;\r\n}\r\n\r\nvoid push ()\r\n{\r\nint data;\r\nstruct node *pointer = (struct node*)malloc(sizeof(struct node));\r\nif(pointer == NULL)\r\n{\r\nprintf(\"Stack overflow\");\r\n}\r\nelse\r\n{\r\nprintf(\"Enter the element to be inserted: \");\r\nscanf(\"%d\",&amp;data);\r\nif(temp == NULL)\r\n{\r\npointer -&gt; data = data;\r\npointer -&gt; next = NULL;\r\ntemp = pointer;\r\n}\r\nelse\r\n{\r\npointer -&gt; data = data;\r\npointer -&gt; next = temp;\r\ntemp = pointer;\r\n}\r\n}\r\n}\r\n\r\nvoid pop()\r\n{\r\nint item;\r\nstruct node *pointer;\r\nif (temp == NULL)\r\n{\r\nprintf(\"Stack Underflow\\n\");\r\n}\r\nelse\r\n{\r\nitem = temp -&gt; data;\r\npointer = temp;\r\ntemp = temp -&gt; next;\r\nfree(pointer);\r\nprintf(\"The deleted item is %d\\n\",item);\r\n}\r\n}\r\nvoid display()\r\n{\r\nint i;\r\nstruct node *pointer;\r\npointer = temp;\r\nif(pointer == NULL)\r\n{\r\nprintf(\"Stack underflow\\n\");\r\n}\r\nelse\r\n{\r\nprintf(\"The elements of the stack are:\\n\");\r\nwhile(pointer!= NULL)\r\n{\r\nprintf(\"%d\\n\",pointer -&gt; data);\r\npointer = pointer -&gt; next;\r\n}\r\n}\r\n}<\/pre>\n<p><strong>Output-<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-linked-list-implementation-of-arrays-in-C.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59952\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-linked-list-implementation-of-arrays-in-C.jpg\" alt=\"Output of linked list implementation of arrays in C\" width=\"1304\" height=\"745\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-linked-list-implementation-of-arrays-in-C.jpg 1304w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-linked-list-implementation-of-arrays-in-C-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-linked-list-implementation-of-arrays-in-C-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-linked-list-implementation-of-arrays-in-C-768x439.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-linked-list-implementation-of-arrays-in-C-1024x585.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-linked-list-implementation-of-arrays-in-C-520x297.jpg 520w\" sizes=\"auto, (max-width: 1304px) 100vw, 1304px\" \/><\/a><\/p>\n<h3><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/linked-list-implementation-of-arrays-in-C-with-Output.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59953\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/linked-list-implementation-of-arrays-in-C-with-Output.jpg\" alt=\"Linked list implementation of arrays in C with Output\" width=\"1304\" height=\"740\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/linked-list-implementation-of-arrays-in-C-with-Output.jpg 1304w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/linked-list-implementation-of-arrays-in-C-with-Output-150x85.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/linked-list-implementation-of-arrays-in-C-with-Output-300x170.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/linked-list-implementation-of-arrays-in-C-with-Output-768x436.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/linked-list-implementation-of-arrays-in-C-with-Output-1024x581.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/linked-list-implementation-of-arrays-in-C-with-Output-520x295.jpg 520w\" sizes=\"auto, (max-width: 1304px) 100vw, 1304px\" \/><\/a><\/h3>\n<h3>1.3 Application of Stack in C<\/h3>\n<p>The principle<a href=\"https:\/\/en.wikipedia.org\/wiki\/LIFO\"> LIFO<\/a> followed by stacks gives birth to the various applications of stacks. Some of the most popular applications of stacks are:<\/p>\n<ul>\n<li><strong>Number reversal:<\/strong> A stack helps you reverse a number or a word entered as a sequence of digits or characters respectively.<\/li>\n<li><strong>Undo operation:<\/strong> Implementation of a stack helps you perform the \u201cundo\u201d operation in text editors or word processors. Here, all the changes done in the text editor are stored in a stack.<\/li>\n<li><strong>Infix to postfix conversion:<\/strong> Using stacks, you can perform the conversion of an infix expression to a postfix expression.<\/li>\n<li><strong>Backtracking:\u00a0<\/strong>Stacks are finding applications in puzzle or maze problem-solving.<\/li>\n<li><strong>Depth-first search (DFS):<\/strong> Stacks allow you to perform a searching algorithm called the depth-first search.<\/li>\n<\/ul>\n<h2>2. What is a Queue in C?<\/h2>\n<p>In contrast to a stack, a <em>queue in C is nothing but a linear data structure that follows the <strong>FIFO<\/strong> rule (First In First Out<\/em>). Insertion is done from the back (the rear end) and deletion is done from the front.<\/p>\n<p><em><strong>Learn More about <a href=\"https:\/\/data-flair.training\/blogs\/structures-in-c\/\">Structures in C Language<\/a><\/strong><\/em><\/p>\n<p>In order to better understand the concept of queues in C, we can say that it follows the rule of \u201cFirst Come First Serve\u201d. Let us consider a simple scenario to help you get a clear picture of queues.\u00a0Suppose you want to purchase a movie ticket. For that, you need to stand in a queue and wait for your turn, that is, you have to stand at the rear end of the queue. You can\u2019t simply stand in the middle of the queue or occupy the front position.<\/p>\n<p>From the above discussion, it is pretty obvious that insertion in a queue takes place from the back and deletion is taking place from the front as the first person to enter the queue would be the first to get his job done and leave.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Queue-insertion-and-deletion.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59984\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Queue-insertion-and-deletion.jpg\" alt=\"C Queue insertion and deletion\" width=\"800\" height=\"600\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Queue-insertion-and-deletion.jpg 800w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Queue-insertion-and-deletion-150x113.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Queue-insertion-and-deletion-300x225.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Queue-insertion-and-deletion-768x576.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Queue-insertion-and-deletion-520x390.jpg 520w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/a><\/p>\n<h4>We can implement a queue in 2 ways:<\/h4>\n<ol>\n<li><strong>Statically:<\/strong> Array implementation of queues allows the static memory allocation of its data elements. It is important to note that in this method, the queue acquires all the features of an array.<\/li>\n<li><strong>Dynamically:<\/strong> Linked list implementation of queues follow the dynamic memory allocation of its data elements. It is important to note that in this method, the queue inherits all the characteristics of a linked list.<\/li>\n<\/ol>\n<p><strong>Key takeaway:<\/strong> Both stacks and queues in C can be static or dynamic according to the way they are implemented.<\/p>\n<h3>2.1 Array Implementation of Queue in C<\/h3>\n<p>As we already discussed, arrays support the static memory allocation of the data elements of the queue. Therefore, it is important to determine the size of the queue prior to the program run.<\/p>\n<p><em><strong>It&#8217;s the right time to uncover the secrete of <a href=\"https:\/\/data-flair.training\/blogs\/multi-dimensional-arrays-in-c\/\">Multi-dimensional Arrays in C<\/a><\/strong><\/em><\/p>\n<p><strong>The queue functions basically include:<\/strong><\/p>\n<h4>2.1.1 Insertion<\/h4>\n<p>Insertion of elements into the queue takes place from the rear end. Inserting an element into the queue is also called enqueue.<\/p>\n<p><strong>Here is a diagrammatic representation of how elements are inserted into a queue:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Insertion-in-C-queue.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59985\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Insertion-in-C-queue.jpg\" alt=\"Insertion in C queue\" width=\"600\" height=\"400\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Insertion-in-C-queue.jpg 600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Insertion-in-C-queue-150x100.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Insertion-in-C-queue-300x200.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Insertion-in-C-queue-520x347.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Insertion-in-C-queue-272x182.jpg 272w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p><strong>This is how can insert elements into the queue in C:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">void insert()\r\n{\r\nint element;\r\nif (rear == LIMIT - 1)\r\nprintf(\"Queue Overflow\\n\");\r\nelse\r\n{\r\nif (front == - 1)\r\nfront = 0;\r\nprintf(\"Enter the element to be inserted in the queue: \");\r\nscanf(\"%d\", &amp;element);\r\nrear++;\r\nqueue[rear] = element;\r\n}\r\n}<\/pre>\n<h4>2.1.2 Deletion<\/h4>\n<p>In a queue, the removal of data elements is done from the front. Removing the element from the queue is also called dequeue.<\/p>\n<p><strong>Here is a diagrammatic representation of how elements are deleted from a queue in C:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Deletion-in-C-queue.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59986\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Deletion-in-C-queue.jpg\" alt=\"Deletion in C queue\" width=\"600\" height=\"400\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Deletion-in-C-queue.jpg 600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Deletion-in-C-queue-150x100.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Deletion-in-C-queue-300x200.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Deletion-in-C-queue-520x347.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Deletion-in-C-queue-272x182.jpg 272w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p><strong>This is how we can delete elements from the queue:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">void delet()\r\n{\r\nif (front == - 1 || front &gt; rear)\r\n{\r\nprintf(\"Queue Underflow \\n\");\r\n}\r\nelse\r\n{\r\nprintf(\"The deleted element in the queue is: %d\\n\", queue[front]);\r\nfront++;\r\n}\r\n}\r\n<\/pre>\n<p><strong>Key takeaway:<\/strong> It is important to note that we used the function name \u201cdelet\u201d instead of \u201cdelete\u201d because \u201cdelete\u201d is a keyword.<\/p>\n<h4>2.1.3 Display<\/h4>\n<p>The stack data elements are displayed in the queue according to the FIFO rule.<\/p>\n<p><strong>This is how we display all the elements in the queue in C:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">void display()\r\n{\r\nint i;\r\nif (front == - 1)\r\n{\r\nprintf(\"Queue underflow\\n\");\r\n}\r\nelse\r\n{\r\nprintf(\"The elements of the queue are:\\n\");\r\nfor (i = front; i &lt;= rear; i++)\r\nprintf(\"%d\\n\", queue[i]);\r\n}\r\n}<\/pre>\n<p>Similarly, in queues, apart from these 3 main functions, it is necessary to check the overflow and underflow conditions to avoid unfavorable situations.<\/p>\n<p><strong>Here is a code in C that illustrates the array implementation of queues in C:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#define LIMIT 100 \/\/ Specifying the maximum limit of the queue\r\n\r\n\/* Global declaration of variables *\/\r\n\r\nint queue[LIMIT]; \/\/ Array implementation of queue\r\nint front, rear; \/\/ To insert and delete the data elements in the queue respectively\r\nint i; \/\/ To traverse the loop to while displaying the stack\r\nint choice; \/\/ To choose either of the 3 stack operations\r\n\r\nvoid insert(); \/\/ Function used to insert the element into the queue\r\nvoid delet(); \/\/ Function used to delete the elememt from the queue\r\nvoid display(); \/\/ Function used to display all the elements in the queue according to FIFO rule\r\n\r\nint main()\r\n{\r\n\r\nprintf(\"Welcome to DataFlair tutorials!\\n\\n\");\r\n\r\nprintf (\"ARRAY IMPLEMENTATION OF QUEUES\\n\\n\");\r\nfront = rear = -1; \/\/ Initialzing front and rear to -1 indicates that it is empty\r\ndo\r\n{\r\n\r\nprintf(\"1. Insert\\n2. Delete\\n3. Display\\n4. Exit\\n\\n\");\r\nprintf(\"Enter your choice:\");\r\nscanf(\"%d\",&amp;choice);\r\n\r\nswitch(choice)\r\n{\r\ncase 1:\r\ninsert();\r\nbreak;\r\ncase 2:\r\ndelet();\r\nbreak;\r\ncase 3:\r\ndisplay();\r\nbreak;\r\ncase 4:\r\nexit(0);\r\nbreak;\r\ndefault:\r\nprintf(\"Sorry, invalid choice!\\n\");\r\nbreak;\r\n}\r\n} while(choice!=4);\r\nreturn 0;\r\n}\r\n\r\nvoid insert()\r\n{\r\nint element;\r\nif (rear == LIMIT - 1)\r\nprintf(\"Queue Overflow\\n\");\r\nelse\r\n{\r\nif (front == - 1)\r\nfront = 0;\r\nprintf(\"Enter the element to be inserted in the queue: \");\r\nscanf(\"%d\", &amp;element);\r\nrear++;\r\nqueue[rear] = element;\r\n}\r\n}\r\n\r\nvoid delet()\r\n{\r\nif (front == - 1 || front &gt; rear)\r\n{\r\nprintf(\"Queue Underflow \\n\");\r\n}\r\nelse\r\n{\r\nprintf(\"The deleted element in the queue is: %d\\n\", queue[front]);\r\nfront++;\r\n}\r\n}\r\n\r\nvoid display()\r\n{\r\nint i;\r\nif (front == - 1)\r\n{\r\nprintf(\"Queue underflow\\n\");\r\n}\r\nelse\r\n{\r\nprintf(\"The elements of the queue are:\\n\");\r\nfor (i = front; i &lt;= rear; i++)\r\nprintf(\"%d\\n\", queue[i]);\r\n}\r\n}\r\n<\/pre>\n<p><strong>Output-<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/array-implementation-of-queues-in-C.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59954\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/array-implementation-of-queues-in-C.jpg\" alt=\"array implementation of queues in C \" width=\"1304\" height=\"739\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/array-implementation-of-queues-in-C.jpg 1304w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/array-implementation-of-queues-in-C-150x85.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/array-implementation-of-queues-in-C-300x170.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/array-implementation-of-queues-in-C-768x435.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/array-implementation-of-queues-in-C-1024x580.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/array-implementation-of-queues-in-C-520x295.jpg 520w\" sizes=\"auto, (max-width: 1304px) 100vw, 1304px\" \/><\/a><\/p>\n<h3><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-array-implementation-of-queues-in-C.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59955\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-array-implementation-of-queues-in-C.jpg\" alt=\"Output of array implementation of queues in C\" width=\"1304\" height=\"743\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-array-implementation-of-queues-in-C.jpg 1304w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-array-implementation-of-queues-in-C-150x85.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-array-implementation-of-queues-in-C-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-array-implementation-of-queues-in-C-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-array-implementation-of-queues-in-C-1024x583.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-array-implementation-of-queues-in-C-520x296.jpg 520w\" sizes=\"auto, (max-width: 1304px) 100vw, 1304px\" \/><\/a><\/h3>\n<h3><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/array-implementation-of-queues-in-C-in-C-with-results.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59956\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/array-implementation-of-queues-in-C-in-C-with-results.jpg\" alt=\"array implementation of queues in C in C with results\" width=\"1300\" height=\"744\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/array-implementation-of-queues-in-C-in-C-with-results.jpg 1300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/array-implementation-of-queues-in-C-in-C-with-results-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/array-implementation-of-queues-in-C-in-C-with-results-300x172.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/array-implementation-of-queues-in-C-in-C-with-results-768x440.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/array-implementation-of-queues-in-C-in-C-with-results-1024x586.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/array-implementation-of-queues-in-C-in-C-with-results-520x298.jpg 520w\" sizes=\"auto, (max-width: 1300px) 100vw, 1300px\" \/><\/a><\/h3>\n<h3>2.2 Linked List Implementation of Queue in C<\/h3>\n<p>As we already discussed, linked lists support the dynamic memory allocation of the data elements of the queue. Therefore, the size of the queue is allocated during the program run and needn\u2019t be specified beforehand.<\/p>\n<p>The 3 basic operations of Insertion, Deletion, and Display follow a similar trend as we saw in the array implementation of queues.<\/p>\n<p>It is important to note that the condition of queue overflow does not exist in the linked list implementation of queues and the size of the stack is not pre-determined. But, the queue underflow condition still holds true.<\/p>\n<h4>2.2.1 Insertion<\/h4>\n<p><strong>This is how we can insert elements into the queue:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">void insert()\r\n{\r\nstruct node *temp;\r\n\r\ntemp = (struct node*)malloc(sizeof(struct node));\r\nprintf(\"Enter the element to be inserted in the queue: \");\r\nscanf(\"%d\", &amp;temp-&gt;data);\r\ntemp-&gt;link = NULL;\r\nif (rear == NULL)\r\n{\r\nfront = rear = temp;\r\n}\r\nelse\r\n{\r\nrear-&gt;link = temp;\r\nrear = temp;\r\n}\r\n}\r\n<\/pre>\n<h4>2.2.2 Deletion<\/h4>\n<p>This is how the deletion is done in a queue:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">void delet()\r\n{\r\nstruct node *temp;\r\ntemp = front;\r\nif (front == NULL)\r\n{\r\nprintf(\"Queue underflow\\n\");\r\nfront = rear = NULL;\r\n}\r\nelse\r\n{\r\nprintf(\"The deleted element from the queue is: %d\\n\", front-&gt;data);\r\nfront = front-&gt;link;\r\nfree(temp);\r\n}\r\n}<\/pre>\n<h4>2.2.3 Display<\/h4>\n<p>This is how we display the elements in the queue<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">void display()\r\n{\r\nstruct node *temp;\r\ntemp = front;\r\nint cnt = 0;\r\nif (front == NULL)\r\n{\r\nprintf(\"Queue underflow\\n\");\r\n}\r\nelse\r\n{\r\nprintf(\"The elements of the stack are:\\n\");\r\nwhile (temp)\r\n{\r\nprintf(\"%d\\n\", temp-&gt;data);\r\ntemp = temp-&gt;link;\r\ncnt++;\r\n}\r\n}\r\n}<\/pre>\n<p><strong>Here is a code in C that illustrates the linked list implementation of queues in C:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nstruct node\r\n{\r\nint data;\r\nstruct node *link;\r\n}*front, *rear;\r\n\r\nvoid insert(); \/\/ Function used to insert the element into the queue\r\nvoid delet(); \/\/ Function used to delete the elememt from the queue\r\nvoid display(); \/\/ Function used to display all the elements in the queue according to FIFO rule\r\n\r\nint main()\r\n{\r\n\r\nprintf(\"Welcome to DataFlair tutorials!\\n\\n\");\r\n\r\nint choice;\r\nprintf (\"LINKED LIST IMPLEMENTATION OF QUEUES\\n\\n\");\r\ndo\r\n{\r\n\r\nprintf(\"1. Insert\\n2. Delete\\n3. Display\\n4. Exit\\n\\n\");\r\nprintf(\"Enter your choice:\");\r\nscanf(\"%d\",&amp;choice);\r\n\r\nswitch(choice)\r\n{\r\ncase 1:\r\ninsert();\r\nbreak;\r\ncase 2:\r\ndelet();\r\nbreak;\r\ncase 3:\r\ndisplay();\r\nbreak;\r\ncase 4:\r\nexit(0);\r\nbreak;\r\ndefault:\r\nprintf(\"Sorry, invalid choice!\\n\");\r\nbreak;\r\n}\r\n} while(choice!=4);\r\nreturn 0;\r\n}\r\n\r\nvoid insert()\r\n{\r\nstruct node *temp;\r\n\r\ntemp = (struct node*)malloc(sizeof(struct node));\r\nprintf(\"Enter the element to be inserted in the queue: \");\r\nscanf(\"%d\", &amp;temp-&gt;data);\r\ntemp-&gt;link = NULL;\r\nif (rear == NULL)\r\n{\r\nfront = rear = temp;\r\n}\r\nelse\r\n{\r\nrear-&gt;link = temp;\r\nrear = temp;\r\n}\r\n}\r\n\r\nvoid delet()\r\n{\r\nstruct node *temp;\r\ntemp = front;\r\nif (front == NULL)\r\n{\r\nprintf(\"Queue underflow\\n\");\r\nfront = rear = NULL;\r\n}\r\nelse\r\n{\r\nprintf(\"The deleted element from the queue is: %d\\n\", front-&gt;data);\r\nfront = front-&gt;link;\r\nfree(temp);\r\n}\r\n}\r\n\r\nvoid display()\r\n{\r\nstruct node *temp;\r\ntemp = front;\r\nint cnt = 0;\r\nif (front == NULL)\r\n{\r\nprintf(\"Queue underflow\\n\");\r\n}\r\nelse\r\n{\r\nprintf(\"The elements of the stack are:\\n\");\r\nwhile (temp)\r\n{\r\nprintf(\"%d\\n\", temp-&gt;data);\r\ntemp = temp-&gt;link;\r\ncnt++;\r\n}\r\n}\r\n}<\/pre>\n<p><strong>Output-<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/linked-list-implementation-of-queues-in-C.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59957\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/linked-list-implementation-of-queues-in-C.jpg\" alt=\"linked list implementation of queues in C\" width=\"1303\" height=\"744\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/linked-list-implementation-of-queues-in-C.jpg 1303w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/linked-list-implementation-of-queues-in-C-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/linked-list-implementation-of-queues-in-C-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/linked-list-implementation-of-queues-in-C-768x439.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/linked-list-implementation-of-queues-in-C-1024x585.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/linked-list-implementation-of-queues-in-C-520x297.jpg 520w\" sizes=\"auto, (max-width: 1303px) 100vw, 1303px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-Linked-list-implementation-of-queues-in-C.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59958\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-Linked-list-implementation-of-queues-in-C.jpg\" alt=\"Output of Linked list implementation of queues in C\" width=\"1303\" height=\"738\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-Linked-list-implementation-of-queues-in-C.jpg 1303w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-Linked-list-implementation-of-queues-in-C-150x85.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-Linked-list-implementation-of-queues-in-C-300x170.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-Linked-list-implementation-of-queues-in-C-768x435.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-Linked-list-implementation-of-queues-in-C-1024x580.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-Linked-list-implementation-of-queues-in-C-520x295.jpg 520w\" sizes=\"auto, (max-width: 1303px) 100vw, 1303px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Linked-List-implementation-of-Queues-in-C-with-results.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59959\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Linked-List-implementation-of-Queues-in-C-with-results.jpg\" alt=\"Linked List implementation of Queues in C with results\" width=\"1302\" height=\"745\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Linked-List-implementation-of-Queues-in-C-with-results.jpg 1302w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Linked-List-implementation-of-Queues-in-C-with-results-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Linked-List-implementation-of-Queues-in-C-with-results-300x172.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Linked-List-implementation-of-Queues-in-C-with-results-768x439.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Linked-List-implementation-of-Queues-in-C-with-results-1024x586.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Linked-List-implementation-of-Queues-in-C-with-results-520x298.jpg 520w\" sizes=\"auto, (max-width: 1302px) 100vw, 1302px\" \/><\/a><\/p>\n<h3>2.3 Circular Queue in C<\/h3>\n<p>The role of circular queue comes into play when we wish to avoid the loss of computer memory using arrays. It is based on the principle that the rear end of the queue becomes equal to its front end.<\/p>\n<p><strong>Here is a diagrammatic representation of how a circular queue looks like:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Circular-queue-in-C.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59988\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Circular-queue-in-C.jpg\" alt=\"Circular Queue in C\" width=\"500\" height=\"500\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Circular-queue-in-C.jpg 500w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Circular-queue-in-C-150x150.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Circular-queue-in-C-300x300.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Circular-queue-in-C-160x160.jpg 160w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Circular-queue-in-C-320x320.jpg 320w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>The <em>circular queue operations are the same as that of a linear queue<\/em>, that is, insertion, deletion and display and checking of boundary conditions such as overflow and underflow.<\/p>\n<p>We have discussed the 3 basic operations in detail:<\/p>\n<h4>2.3.1 Insertion<\/h4>\n<p><strong>This is how we insert elements in the circular queue:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">void insert()\r\n{\r\nif((front == 0 &amp;&amp; rear == LIMIT-1) || (front == rear+1))\r\n{\r\nprintf(\"Queue Overflow\\n\");\r\n}\r\nif (front == -1) \/\/If queue is empty\r\n{\r\nfront = rear = 0;\r\n}\r\nelse\r\n{\r\nprintf(\"Enter the element to be inserted in queue: \");\r\nscanf(\"%d\", &amp;item);\r\nif(rear == LIMIT-1) \/\/ When rear is at the last position of the queue\r\n{\r\nrear = 0;\r\n}\r\nelse\r\n{\r\nrear++;\r\n}\r\n}\r\ncqueue[rear] = item ;\r\n}<\/pre>\n<h4>2.3.2 Deletion<\/h4>\n<p><strong>This is how we delete elements in the circular queue in C:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">void delet()\r\n{\r\nif (front == -1)\r\n{\r\nprintf(\"Queue Underflow\\n\");\r\n}\r\nprintf(\"Element deleted from queue is : %d\\n\",cqueue[front]);\r\nif(front == rear) \/* queue has only one element *\/\r\n{\r\nfront = rear = -1;\r\n}\r\nelse\r\n{\r\nif(front == LIMIT-1)\r\n{\r\nfront = 0;\r\n}\r\nelse\r\nfront++;\r\n}\r\n}<\/pre>\n<h4>2.3.3 Display<\/h4>\n<p><strong>This is we display elements in the circular queue:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">void display()\r\n{\r\nint front_position = front;\r\nint rear_position = rear;\r\nif(front == -1)\r\n{\r\nprintf(\"Queue underflow\\n\");\r\n}\r\nprintf(\"The elements of the queue are:\\n\");\r\nif( front_position &lt;= rear_position )\r\nwhile(front_position &lt;= rear_position)\r\n{\r\nprintf(\"%d\\n\",cqueue[front_position]);\r\nfront_position++;\r\n}\r\nelse\r\n{\r\nwhile(front_position &lt;= LIMIT-1)\r\n{\r\nprintf(\"%d\\n\",cqueue[front_position]);\r\nfront_position++;\r\n}\r\nfront_position = 0;\r\nwhile(front_position &lt;= rear_position)\r\n{\r\nprintf(\"%d\\n\",cqueue[front_position]);\r\nfront_position++;\r\n}\r\n}\r\n}<\/pre>\n<p><strong>Here is a code in C that illustrates the implementation of a circular queue in C:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#include&lt;stdio.h&gt;\r\n#include&lt;stdlib.h&gt;\r\n#define LIMIT 10\r\n\r\n\/* Global declaration of variables *\/\r\nint cqueue[LIMIT];\r\nint choice, item;\r\nint front, rear; \r\n\r\nvoid insert(); \/\/ Function to insert the element in the queue\r\nvoid delet(); \/\/ Function to delete the element in the queue\r\nvoid display(); \/\/ Function to display the element in the queue\r\n\r\nint main()\r\n{\r\n\r\nprintf(\"Welcome to DataFlair tutorials!\\n\\n\");\r\n\r\nprintf (\"ARRAY IMPLEMENTATION OF QUEUES\\n\\n\");\r\n\r\nfront = rear = -1; \/\/ It indicates the queue is empty\r\ndo\r\n{\r\n\r\nprintf(\"1. Insert\\n2. Delete\\n3. Display\\n4. Exit\\n\\n\");\r\nprintf(\"Enter your choice:\");\r\nscanf(\"%d\",&amp;choice);\r\n\r\nswitch(choice)\r\n{\r\ncase 1:\r\ninsert();\r\nbreak;\r\ncase 2:\r\ndelet();\r\nbreak;\r\ncase 3:\r\ndisplay();\r\nbreak;\r\ncase 4:\r\nexit(0);\r\nbreak;\r\ndefault:\r\nprintf(\"Sorry, invalid choice!\\n\");\r\nbreak;\r\n}\r\n} while(choice!=4);\r\nreturn 0;\r\n}\r\n\r\nvoid insert()\r\n{\r\nif((front == 0 &amp;&amp; rear == LIMIT-1) || (front == rear+1))\r\n{\r\nprintf(\"Queue Overflow\\n\");\r\n}\r\nif (front == -1)  \/\/If the queue is empty \r\n{\r\nfront = rear = 0;\r\n}\r\nelse\r\n{\r\nprintf(\"Enter the element to be inserted in queue: \");\r\nscanf(\"%d\", &amp;item);\r\nif(rear == LIMIT-1) \/\/ When rear is at the last position of the queue \r\n{\r\nrear = 0;\r\n}\r\nelse\r\n{\r\nrear++;\r\n}\r\n}\r\ncqueue[rear] = item ;\r\n}\r\n\r\nvoid delet()\r\n{\r\nif (front == -1)\r\n{\r\nprintf(\"Queue Underflow\\n\");\r\n}\r\nif(front!= -1)\r\nprintf(\"Element deleted from queue is : %d\\n\",cqueue[front]);\r\nif(front == rear) \/* queue has only one element *\/\r\n{\r\nfront = rear = -1;\r\n}\r\nelse\r\n{\t\r\nif(front == LIMIT-1)\r\n{\r\nfront = 0;\r\n}\r\nelse\r\nfront++;\r\n}\r\n}\r\n\r\nvoid display()\r\n{\r\nint front_position = front;\r\nint rear_position = rear;\r\nif(front == -1)\r\n{\r\nprintf(\"Queue underflow\\n\");\r\n}\r\nif(front!= -1)\r\nprintf(\"The elements of the queue are:\\n\");\r\nif( front_position &lt;= rear_position )\r\nwhile(front_position &lt;= rear_position)\r\n{\r\nprintf(\"%d\\n\",cqueue[front_position]);\r\nfront_position++;\r\n}\r\nelse\r\n{\r\nwhile(front_position &lt;= LIMIT-1)\r\n{\r\nprintf(\"%d\\n\",cqueue[front_position]);\r\nfront_position++;\r\n}\r\nfront_position = 0;\r\nwhile(front_position &lt;= rear_position)\r\n{\r\nprintf(\"%d\\n\",cqueue[front_position]);\r\nfront_position++;\r\n}\r\n}\r\n}<\/pre>\n<p><strong>Output-<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/implementation-of-a-circular-queue-in-C.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59975\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/implementation-of-a-circular-queue-in-C.jpg\" alt=\" implementation of a circular queue in C\" width=\"1304\" height=\"736\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/implementation-of-a-circular-queue-in-C.jpg 1304w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/implementation-of-a-circular-queue-in-C-150x85.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/implementation-of-a-circular-queue-in-C-300x169.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/implementation-of-a-circular-queue-in-C-768x433.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/implementation-of-a-circular-queue-in-C-1024x578.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/implementation-of-a-circular-queue-in-C-520x293.jpg 520w\" sizes=\"auto, (max-width: 1304px) 100vw, 1304px\" \/><\/a><\/p>\n<h3><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/implementation-of-a-circular-queue-in-C-with-Output.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59976\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/implementation-of-a-circular-queue-in-C-with-Output.jpg\" alt=\" implementation of a circular queue in C with Output\" width=\"1304\" height=\"734\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/implementation-of-a-circular-queue-in-C-with-Output.jpg 1304w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/implementation-of-a-circular-queue-in-C-with-Output-150x84.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/implementation-of-a-circular-queue-in-C-with-Output-300x169.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/implementation-of-a-circular-queue-in-C-with-Output-768x432.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/implementation-of-a-circular-queue-in-C-with-Output-1024x576.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/implementation-of-a-circular-queue-in-C-with-Output-520x293.jpg 520w\" sizes=\"auto, (max-width: 1304px) 100vw, 1304px\" \/><\/a><\/h3>\n<h3><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Result-of-implementation-of-a-circular-queue-in-C.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59977\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Result-of-implementation-of-a-circular-queue-in-C.jpg\" alt=\"Result of implementation of a circular queue in C\" width=\"1303\" height=\"736\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Result-of-implementation-of-a-circular-queue-in-C.jpg 1303w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Result-of-implementation-of-a-circular-queue-in-C-150x85.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Result-of-implementation-of-a-circular-queue-in-C-300x169.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Result-of-implementation-of-a-circular-queue-in-C-768x434.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Result-of-implementation-of-a-circular-queue-in-C-1024x578.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Result-of-implementation-of-a-circular-queue-in-C-520x294.jpg 520w\" sizes=\"auto, (max-width: 1303px) 100vw, 1303px\" \/><\/a><\/h3>\n<h3>2.4 Applications of Queue in C<\/h3>\n<p>The principle FIFO followed by queues gives birth to the various applications of queues. Some of the most popular applications of queues are:<\/p>\n<ul>\n<li><strong>Round robin algorithm:<\/strong> The concept of queues finds a striking application in the round robin algorithm done in MBA.<\/li>\n<li><strong>CPU Scheduling:<\/strong> In a queue, the data is not processed instantly, but processed according to the FIFO rule. Therefore, this feature of queues helps in the sharing of resources among multiple users at the same time.<\/li>\n<li><strong>Input-Output Buffers:<\/strong> It helps in the transmission of asynchronous data (A condition where the retrieval of multiple data takes place at the user end at different rates) by converting it into synchronous data.<\/li>\n<\/ul>\n<h2>Summary<\/h2>\n<p>In this tutorial, we discussed the meaning of stacks and queues in C in detail. We saw how they are implemented using arrays and linked lists.\u00a0Further, we discussed the three basic operations of both stacks and queues in C with the help of illustrative programs.\u00a0We even introduced the concept of a circular queue. Finally, we concluded our discussion by stating the various real-world applications of stacks and queues in C. Stacks and queues are important concepts in every programming languages.<\/p>\n<p>If you found this tutorial satisfying or if you have any queries regarding this topic, feel free to let us know your responses in the comment section below!<\/p>\n<p><em><strong>Build your Coding skills with these<a href=\"https:\/\/data-flair.training\/blogs\/basic-c-programs\/\"> basic C Programs<\/a>.<\/strong><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>After getting well-versed with linked lists and arrays in C, you are now ready to explore a new concept, that is stacks and queues. Both stacks and queues in C are data structures that&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":59995,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19488],"tags":[20214,20210,20213,20212,20215,20211,20216],"class_list":["post-59894","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-c-queue-tutorial","tag-c-stack-tutorial","tag-fifo","tag-lifo","tag-queue-in-c-with-example","tag-stack-in-c-with-example","tag-stacks-and-queues"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Stacks and Queues in C &#8211; Master the Concepts of LIFO &amp; FIFO - DataFlair<\/title>\n<meta name=\"description\" content=\"Stacks and queues in C is are the linear data structure that follows the LIFO and FIFO rules. Learn the concept with Implementation, example.Circular queues\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/stacks-and-queues-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Stacks and Queues in C &#8211; Master the Concepts of LIFO &amp; FIFO - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Stacks and queues in C is are the linear data structure that follows the LIFO and FIFO rules. Learn the concept with Implementation, example.Circular queues\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/stacks-and-queues-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-06-19T10:45:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-12-30T04:07:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Stacks-and-queues-in-C.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"23 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Stacks and Queues in C &#8211; Master the Concepts of LIFO &amp; FIFO - DataFlair","description":"Stacks and queues in C is are the linear data structure that follows the LIFO and FIFO rules. Learn the concept with Implementation, example.Circular queues","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/stacks-and-queues-in-c\/","og_locale":"en_US","og_type":"article","og_title":"Stacks and Queues in C &#8211; Master the Concepts of LIFO &amp; FIFO - DataFlair","og_description":"Stacks and queues in C is are the linear data structure that follows the LIFO and FIFO rules. Learn the concept with Implementation, example.Circular queues","og_url":"https:\/\/data-flair.training\/blogs\/stacks-and-queues-in-c\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2019-06-19T10:45:34+00:00","article_modified_time":"2022-12-30T04:07:18+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Stacks-and-queues-in-C.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"23 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/stacks-and-queues-in-c\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/stacks-and-queues-in-c\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Stacks and Queues in C &#8211; Master the Concepts of LIFO &amp; FIFO","datePublished":"2019-06-19T10:45:34+00:00","dateModified":"2022-12-30T04:07:18+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/stacks-and-queues-in-c\/"},"wordCount":2175,"commentCount":8,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/stacks-and-queues-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Stacks-and-queues-in-C.jpg","keywords":["C Queue tutorial","C Stack Tutorial","Fifo","Lifo","Queue in C with Example","Stack in C with Example","Stacks and queues"],"articleSection":["C Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/stacks-and-queues-in-c\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/stacks-and-queues-in-c\/","url":"https:\/\/data-flair.training\/blogs\/stacks-and-queues-in-c\/","name":"Stacks and Queues in C &#8211; Master the Concepts of LIFO &amp; FIFO - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/stacks-and-queues-in-c\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/stacks-and-queues-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Stacks-and-queues-in-C.jpg","datePublished":"2019-06-19T10:45:34+00:00","dateModified":"2022-12-30T04:07:18+00:00","description":"Stacks and queues in C is are the linear data structure that follows the LIFO and FIFO rules. Learn the concept with Implementation, example.Circular queues","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/stacks-and-queues-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/stacks-and-queues-in-c\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/stacks-and-queues-in-c\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Stacks-and-queues-in-C.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Stacks-and-queues-in-C.jpg","width":802,"height":420,"caption":"Stacks and queues in C"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/stacks-and-queues-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"C Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/c-programming\/"},{"@type":"ListItem","position":3,"name":"Stacks and Queues in C &#8211; Master the Concepts of LIFO &amp; FIFO"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/59894","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=59894"}],"version-history":[{"count":12,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/59894\/revisions"}],"predecessor-version":[{"id":111294,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/59894\/revisions\/111294"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/59995"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=59894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=59894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=59894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}