

{"id":96369,"date":"2021-06-09T09:00:57","date_gmt":"2021-06-09T03:30:57","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=96369"},"modified":"2021-06-01T20:47:11","modified_gmt":"2021-06-01T15:17:11","slug":"queue-in-data-structure","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/queue-in-data-structure\/","title":{"rendered":"Queue in Data Structure"},"content":{"rendered":"<p>Imagine yourself in the same example as we discussed in the last article on stacks. You are standing in a line for buffet dinner and many people are standing ahead of and behind you. People ahead of you in the line get there early. They get the food before you and they can leave before you.<\/p>\n<p>On the other hand, people behind you get served after you and leave after you. This line for buffet dinner is a queue. The first object to enter the queue is the first one to exit and the last object to enter in the queue is the last one to exit.<\/p>\n<p>In this article, we will learn about queues, types of queues, and array and linked list implementation of each type of queue. We will also see the advantages and disadvantages of different types of queues.<\/p>\n<h3>Queue in Data Structure<\/h3>\n<p>A queue is an abstract data type data structure that allows operations on both ends. Just like in the above example, people exit from the front and enter from behind. Similarly, in a queue, you can add elements at one end and remove elements from the other. This makes the queue a FIFO structure.<\/p>\n<p>FIFO stands for First In First Out. The first object to enter the queue will be the first that can be accessed and removed. Elements after that will follow the same order.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Queue-Operations.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96416\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Queue-Operations.png\" alt=\"Queue Operations\" width=\"624\" height=\"117\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Queue-Operations.png 624w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Queue-Operations-300x56.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Queue-Operations-150x28.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Queue-Operations-520x98.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Queue-Operations-320x60.png 320w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><b>Complexity of Queue<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td colspan=\"2\"><\/td>\n<td colspan=\"8\"><span style=\"font-weight: 400;\">TIME COMPLEXITY<\/span><\/td>\n<td colspan=\"2\"><span style=\"font-weight: 400;\">Space<\/span><\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\"><\/td>\n<td colspan=\"4\"><span style=\"font-weight: 400;\">Average<\/span><\/td>\n<td colspan=\"4\"><span style=\"font-weight: 400;\">worst<\/span><\/td>\n<td colspan=\"2\"><span style=\"font-weight: 400;\">worst<\/span><\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\"><\/td>\n<td><span style=\"font-weight: 400;\">access<\/span><\/td>\n<td><span style=\"font-weight: 400;\">search<\/span><\/td>\n<td><span style=\"font-weight: 400;\">insert<\/span><\/td>\n<td><span style=\"font-weight: 400;\">delete<\/span><\/td>\n<td><span style=\"font-weight: 400;\">access<\/span><\/td>\n<td><span style=\"font-weight: 400;\">search<\/span><\/td>\n<td><span style=\"font-weight: 400;\">insert<\/span><\/td>\n<td><span style=\"font-weight: 400;\">delete<\/span><\/td>\n<td colspan=\"2\"><\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\"><span style=\"font-weight: 400;\">QUEUE<\/span><\/td>\n<td><span style=\"font-weight: 400;\">O(n)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">O(n)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">O(1)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">O(1)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">O(n)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">O(n)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">O(1)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">O(1)<\/span><\/td>\n<td colspan=\"2\"><span style=\"font-weight: 400;\">O(n)<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Applications of Queues<\/h3>\n<p><span style=\"font-weight: 400;\">Queues are used:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">1. As waiting buffered in printers, Disk, and CPU. The printer takes on the first request in the waiting queue and executes it. The new instructions are appended in the last of the waiting queue.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2. In the asynchronous transfer of data in PIPES, Sockets, etc.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">3. In operating systems for handling interrupts and scheduling, semaphores.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">4. As Buffer in many applications like CD Player etc.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">5. In networking as a buffer queue in routers and switches.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">6. In Breadth-first search. A queue is maintained for the adjacent nodes of the current node that need to be traversed before going to the next level.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">7. To maintain the playlists in the media players.<\/span><\/p>\n<h3>Basic operations on Queue<\/h3>\n<p><b>1. Enqueue():<\/b><span style=\"font-weight: 400;\"> insert a new element at the rear end of the queue.\u00a0<\/span><\/p>\n<p><b>2. Dequeue():<\/b><span style=\"font-weight: 400;\"> delete the element at the front end of the queue and return the deleted element.<\/span><\/p>\n<p><b>3. Peek():<\/b><span style=\"font-weight: 400;\"> returns the front element of the queue<\/span><\/p>\n<p><b>4. Isfull():<\/b><span style=\"font-weight: 400;\"> check if the queue is full or not<\/span><\/p>\n<p><b>5. Isempty():<\/b><span style=\"font-weight: 400;\"> check if the queue is empty or not.<\/span><\/p>\n<p><b>6. size(): <\/b><span style=\"font-weight: 400;\">returns the current size of the queue.<\/span><\/p>\n<h3><b>Implementation of Queue in Data Structure<\/b><\/h3>\n<h4><b>1. Using Array<\/b><\/h4>\n<p><span style=\"font-weight: 400;\"> An array is used for the implementation of a queue. Implementation and its operation are all carried out on an array.<\/span><\/p>\n<h4><b>2. Using Linked list<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">This is the linked list implementation of a queue. Implementation and its operations are all carried out on a linked list.<\/span><\/p>\n<h3>Types of queue in Data Structure<\/h3>\n<p><span style=\"font-weight: 400;\">1. Linear queue\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2. Circular queue<\/span><\/p>\n<p><span style=\"font-weight: 400;\">3. Priority queue<\/span><\/p>\n<p><span style=\"font-weight: 400;\">4. Deque<\/span><\/p>\n<p>Let&#8217;s learn each of these types of queues now:<\/p>\n<h4>1. Linear queue<\/h4>\n<p><span style=\"font-weight: 400;\">In a linear queue, insertion of an element takes place at one end, known as the rear end and deletion takes place on the other end which is the front end. This strictly is a FIFO structure that stands for first in first out.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">A major disadvantage of a linear linked list is that the elements are inserted on a rear end only. So once the rear pointer points to the last element of the queue no element can be added in a queue even if there is a space and it will return to an overflow condition.\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Queue.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96415\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Queue.png\" alt=\"Queue in data Structure\" width=\"232\" height=\"118\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Queue.png 232w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Queue-150x76.png 150w\" sizes=\"auto, (max-width: 232px) 100vw, 232px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96417\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque.png\" alt=\"Deque\" width=\"232\" height=\"122\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque.png 232w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque-150x79.png 150w\" sizes=\"auto, (max-width: 232px) 100vw, 232px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">In the above example, we can see that every time addition of data is at the rear end and deletion is at the front end.<\/span><\/p>\n<h4><\/h4>\n<h4>Array implementation<\/h4>\n<p><span style=\"font-weight: 400;\">Array implementation is very easy for a queue. Variables \u2018front\u2019 and \u2018rear\u2019 point to the two positions from where deletion and insertion of elements take place respectively. For an empty queue, front point to index -1.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Array-implementation-of-Queue.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96418\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Array-implementation-of-Queue.png\" alt=\"Array implementation of Queue\" width=\"620\" height=\"164\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Array-implementation-of-Queue.png 620w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Array-implementation-of-Queue-300x79.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Array-implementation-of-Queue-150x40.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Array-implementation-of-Queue-520x138.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Array-implementation-of-Queue-320x85.png 320w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">In the above figure, we can see the queue forming a word. The value of rear increases by 1 for every insertion of the new element in the above queue. After inserting \u2018r\u2019 in the last position, the queue will look something like below:<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Add-Element-in-Queue.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96419\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Add-Element-in-Queue.png\" alt=\"Add Element in Queue\" width=\"620\" height=\"164\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Add-Element-in-Queue.png 620w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Add-Element-in-Queue-300x79.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Add-Element-in-Queue-150x40.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Add-Element-in-Queue-520x138.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Add-Element-in-Queue-320x85.png 320w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">After deleting an element the value of the front will increase by 1 every time and the queue will look like something below.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Delete-in-Queue.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96420\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Delete-in-Queue.png\" alt=\"Delete in Queue\" width=\"620\" height=\"166\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Delete-in-Queue.png 620w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Delete-in-Queue-300x80.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Delete-in-Queue-150x40.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Delete-in-Queue-520x139.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Delete-in-Queue-320x86.png 320w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<h5>a. Insert a new element in Queue<\/h5>\n<p><span style=\"font-weight: 400;\">1. Check if the queue is already full. If yes then return an overflow condition.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2. If insertion is at the first position then set the value of the front and rear as 0 and insert the element at the rear end.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">3. Otherwise, keep increasing the value of the rear after inserting each element at the rear end<\/span><\/p>\n<p><strong>Algorithm of Inserting Element in Queue<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Step 1: If Rear = MaxSize - 1\r\nDisplay \u201cOverflow\u201d\r\nGo to step 4\r\nStep 2: If Front = -1 and Rear = -1\r\nset Front = Rear = 0\r\nelse\r\nset Rear = Rear + 1\r\n\r\nStep 3: set queue[Rear] = new\r\n\r\nStep 4: Stop\r\n\r\n<\/pre>\n<p><strong>Queue in C<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">void insert (int queue[], int max, int front, int rear, int item)   \r\n       {\r\n            rear = rear + 1;   \r\n            queue[rear]=item;  \r\n    }  \r\n<\/pre>\n<h5>b. Delete element in Queue<\/h5>\n<p>1. If the value of the front is -1 or greater than the rear, return \u201cqueue is empty\u201d<br \/>\n2. Increment the value of front by one and return the items stored at the front end each time.<\/p>\n<p><strong>Algorithm to delete element in queue<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Step 1: if Front = -1 or Front &gt; Rear\r\nDisplay \u201cUnderflow\u201d\r\nelse\r\nset Value = queue[front]\r\nset front = front + 1\r\n\r\nStep 2: Stop\r\n\r\n<\/pre>\n<p><strong>Delete element in queue in C Language<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int delete (int queue[], int max, int front, int rear)  \r\n{  \r\n    int y;   \r\n     y = queue[front]; \r\n     front = front + 1;      \r\n     return y;  \r\n    } \r\n<\/pre>\n<h5>c. peek() in queue<\/h5>\n<p>This returns the data at the front of the queue.<\/p>\n<p><strong>Algorithm:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">start procedure peek\r\n   return queue[front]\r\nstop procedure\r\n\r\n<\/pre>\n<p><strong>Function in C:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int peek() \r\n{\r\n   return queue[front];\r\n}\r\n\r\n<\/pre>\n<h5>d. isfull() in queue<\/h5>\n<p>1. Check if the rear is at the last position of the array.<\/p>\n<p><strong>Algorithm:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Start procedure isfull\r\n\r\n   if rear equals to MAXSIZE - 1\r\n      return true\r\n   else\r\n      return false\r\n   endif\r\n   \r\nStop procedure\r\n\r\n<\/pre>\n<p><strong>Function in C:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">bool isfull() \r\n{\r\n   if(rear == MAXSIZE - 1)\r\n      return true;\r\n   else\r\n      return false;\r\n}\r\n\r\n<\/pre>\n<h5>e. isEmpty() in queue<\/h5>\n<p>1.Check if the front is 0 or the front is greater than the rear.<\/p>\n<p><strong>Algorithm of isEmpty()<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Start procedure isempty\r\n\r\n   if front is less than MIN  OR front &gt; rear\r\n      return true\r\n   else\r\n      return false\r\n   endif\r\n   \r\nStop procedure\r\n\r\n<\/pre>\n<p><strong>Function in C:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">bool isempty() \r\n{\r\n   if(front &lt; 0 || front &gt; rear) \r\n      return true;\r\n   else\r\n      return false;\r\n}\r\n<\/pre>\n<h5>f. size() in queue<\/h5>\n<p>This function returns the current size of the queue.<\/p>\n<p><strong>Algorithm:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Start procedure size\r\n        Return rear - front\r\nStop procedure\r\n\r\n<\/pre>\n<p><strong>Function in C:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int size(int front, int rear) \r\n{\r\n    return (rear - front);\r\n}\r\n\r\n<\/pre>\n<h3>Disadvantages of array implementation<\/h3>\n<h4>1. Memory wastage<\/h4>\n<p>Memory that is used to store elements of queue, can never be reused, once it\u2019s free because elements can be inserted at the rear position only. So all the space before the front end is useless and a wastage of memory.<\/p>\n<h4><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Memory-Wastage-in-Queue.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96421\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Memory-Wastage-in-Queue.png\" alt=\"Memory Wastage in Queue\" width=\"232\" height=\"87\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Memory-Wastage-in-Queue.png 232w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Memory-Wastage-in-Queue-150x56.png 150w\" sizes=\"auto, (max-width: 232px) 100vw, 232px\" \/><\/a><\/h4>\n<h4>2. Array size<\/h4>\n<p>One major disadvantage of array implementation is that the user needs to define the array size in advance. Since the queue might be required to change the size during the run time, this may lead to problems like memory wastage or memory shortage.<\/p>\n<h4>Linked list implementation of queue<\/h4>\n<p>We know the advantages of a linked list over an array. An alternative to implementing a queue other than the array is a linked list. Worst-case time complexity is o(1) for linked list implementation of a queue.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Linked-List-Implementation.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96426\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Linked-List-Implementation.png\" alt=\"Linked List Implementation of Queue\" width=\"624\" height=\"121\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Linked-List-Implementation.png 624w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Linked-List-Implementation-300x58.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Linked-List-Implementation-150x29.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Linked-List-Implementation-520x101.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Linked-List-Implementation-320x62.png 320w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/a><\/p>\n<h5>1. Insert a new element in queue<\/h5>\n<p>If the queue is empty then the front is null. A new element will be added as the only element of the linked list and will point to null.<\/p>\n<p>If the queue is not empty, increment the rear pointer so that it points to the new node. This new node points to null.<\/p>\n<p><strong>Algorithm:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Step 1: set newnode -&gt; data = value\r\nStep 2: if front = null\r\nset front = rear = newnode\r\nset front -&gt; next = rear -&gt; next = null\r\nelse\r\nset rear -&gt; next = newnode\r\nset rear = newnode\r\nset rear -&gt; next = null\r\nStep 3: Stop\r\n\r\n<\/pre>\n<p><strong>Function in C:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">void insert(Node newNode)\r\n{\r\nnewNode -&gt; data = item;  \r\n            rear -&gt; next = newNode;  \r\n            rear = newNode;  \r\n            rear-&gt;next = NULL;  \r\n   }     \r\n\r\n<\/pre>\n<h5>2. Delete an element in queue<\/h5>\n<p>If the queue is empty then the value of the front is null and returns underflow condition.<br \/>\nElse delete the element from the front of the list and increment the front pointer to the next node.<\/p>\n<p><strong>Algorithm:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Step 1: if front = null\r\ndisplay \" underflow \"\r\ngo to step 5\r\nStep 2: set deletenode = front\r\nStep 3: set front = front -&gt; next\r\nStep 4: free deletenode\r\nStep 5: end\r\n\r\n<\/pre>\n<p><strong>Function in C:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int delete()\r\n{\r\ndeletedNode = front;  \r\n        front = front -&gt; next;  \r\n        free(DeletedNode);  \r\n}\r\n<\/pre>\n<h3>Circular queue<\/h3>\n<p>In a circular queue, all nodes are present in a circular order. This concept is very similar to the circular linked list. Also known as a ring buffer that connects all ends to another end.<\/p>\n<p><strong>Representation of circular queue:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-queue.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96427\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-queue.jpg\" alt=\"Circular queue\" width=\"700\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-queue.jpg 700w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-queue-300x269.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-queue-150x135.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-queue-520x467.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-queue-320x287.jpg 320w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p>The major objective behind the concept of the circular queue was to overcome the drawbacks of the linear queue. If space is available in a circular queue, a new element can be added by incrementing the value of the rear.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Linear-Queue.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96428\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Linear-Queue.jpg\" alt=\"Linear Queue\" width=\"600\" height=\"450\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Linear-Queue.jpg 600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Linear-Queue-300x225.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Linear-Queue-150x113.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Linear-Queue-520x390.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Linear-Queue-320x240.jpg 320w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>In the above example, you can see that although the space is available in the linear queue the new element will not be inserted. The rear is equal to the maximum size of the queue and it will return overflow condition.<\/p>\n<p>But in the circular queue, as represented below, the addition of elements is possible and memory is used more efficiently.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-queue-in-DS.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96429\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-queue-in-DS.jpg\" alt=\"Circular queue in DS\" width=\"700\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-queue-in-DS.jpg 700w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-queue-in-DS-300x269.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-queue-in-DS-150x135.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-queue-in-DS-520x467.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-queue-in-DS-320x287.jpg 320w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<h3>Operations on Circular Queue<\/h3>\n<p><strong>1. Front()<\/strong>: returns the front element.<br \/>\n<strong>2. Rear():<\/strong> return the rear element.<br \/>\n<strong>3. Enqueue():<\/strong> insert a new element at the rear end.<br \/>\n<strong>4. Dequeue():<\/strong> delete element from the front end.<\/p>\n<h4>Applications of Circular Queue<\/h4>\n<p><strong>1. Memory management:<\/strong> Circular queue performs better memory management than a linear queue. Memory is efficiently utilized by placing the elements in empty locations.<\/p>\n<p><strong>2. CPU scheduling:<\/strong> Operating systems use a circular queue to insert the processes and execute them.<\/p>\n<p><strong>3. Traffic Light:<\/strong> Computer-controlled traffic light system follows the circular queue.<\/p>\n<h4>Array implementation of the circular queue<\/h4>\n<h4>1. Enqueue()<\/h4>\n<p>1. Check if the queue is full.<br \/>\n2. If the queue is empty, set front and rear to -1. Insert the first element and set both front and rear to 0<br \/>\n3. Increment the rear by one every time you insert a new element.<\/p>\n<p><strong>Algorithm:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Step 1: if (rear+1)%max = front\r\n             Display \" overflow \"\r\n             Goto step 4\r\nStep 2: if front = -1 and rear = -1\r\n             Set front = rear = 0\r\n             Else if rear = max - 1 and front ! = 0\r\n             Set rear = 0\r\n             Else\r\n             Set rear = (rear + 1) % max\r\nStep 3: set circularqueue[rear] = value\r\nStep 4: stop\r\n<\/pre>\n<p><strong>Function in C:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">void enqueue(int new)  \r\n{  \r\n    if(front==-1 &amp;&amp; rear==-1)  \r\n    {  \r\n        front=rear=0;  \r\n        count = 1;\r\n        circularqueue[rear]=new;  \r\n    }  \r\n    else if((rear+1)%max==front)   \r\n    {  \r\n        printf(\"OverFlow\");  \r\n    }  \r\n    else  \r\n    {  \r\n        rear=(rear+1)%max;       \r\n        circularqueue[rear]=new; \r\n       count++;   \r\n    }  \r\n} \r\n<\/pre>\n<h4>2. Delete element in Circular Queue<\/h4>\n<p>1. Check if the queue is empty or not. If empty, return underflow.<br \/>\n2. Value of the front decreases by one, every time an element is deleted.<br \/>\n3. On deleting the only element left, set both front and rear to -1.<\/p>\n<p><strong>Algorithm:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Step 1: if front = -1\r\n             Display \" underflow \"\r\n             Goto step 4\r\nStep 2: set value = circularqueue[front]\r\nStep 3: if front = rear\r\n             Set front = rear = -1\r\n             Else\r\n             If front = max -1\r\n             Set front = 0\r\n             Else\r\n             Set front = front + 1\r\nStep 4: Stop\r\n\r\n\r\n\r\n<\/pre>\n<p><strong>Function in C:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int dequeue()  \r\n{  \r\n    if((front==-1) &amp;&amp; (rear==-1))  \r\n        printf(\"Underflow\");  \r\n      \r\n else if(front==rear)  \r\n{    \r\n   front=-1;  \r\n   rear=-1; \r\n   count=0; \r\n}   \r\nelse    \r\n   front=(front+1)%max;   \r\nreturn queue[front];\r\ncount--;\r\n}\r\n<\/pre>\n<h4>3. size ()<\/h4>\n<p>Returns the size of the current circular queue.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int size(int count) \r\n{\r\n    return count;\r\n}\r\n<\/pre>\n<h3>Linked list implementation of Circluar Queue<\/h3>\n<p>Linked list implementation of any data structure is always more efficient than array implementation. Inserting and deleting elements in a circular queue via a linked list has a worst-case time complexity of o(n).<\/p>\n<h4>1. Insert element<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">void enqueue(Node newNode , Int datanew)  \r\n{  \r\n    newNode -&gt; data=datanew;  \r\n    newnode-&gt;next=0;  \r\n    if(rear==-1)  \r\n    {  \r\n        front=rear=newNode;  \r\n        rear-&gt;next=front;  \r\n    }  \r\n    else  \r\n    {  \r\n        rear-&gt;next=newNode;  \r\n        rear=newNode;  \r\n        rear-&gt;next=front;  \r\n    }  \r\n}\r\n\r\n<\/pre>\n<h4>2. Delete Element<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">void dequeue()  \r\n{  \r\n    if((front==-1)&amp;&amp;(rear==-1))     {  \r\n        printf(\"Underflow\");  \r\n    }  \r\n    else if(front==rear) \r\n        front=rear=-1;   \r\n    else  \r\n    {  \r\n        front=front-&gt;next;  \r\n        rear-&gt;next=front;  \r\n    }  \r\n} \r\n\r\n<\/pre>\n<h4>3. Peek()<\/h4>\n<p>This function returns the element at the front position.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int peek()  \r\n{  \r\n    if((front==-1) &amp;&amp;(rear==-1))   \r\n        printf(\"Empty Queue\");   \r\n    else   \r\n        printf(front-&gt;data);  \r\n}  \r\n<\/pre>\n<h3>Deque<\/h3>\n<p><span style=\"font-weight: 400;\">Deque stands for double-ended queue. It does not follow the FIFO principle and insertion and deletion of elements can take from both ends. It is also said to be the generalized version of the queue.\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96430\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque.jpg\" alt=\"Deque\" width=\"700\" height=\"300\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque.jpg 700w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque-300x129.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque-150x64.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque-520x223.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque-320x137.jpg 320w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">Deque can act as both stack and queue by limiting insert or delete of conditions on either end.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque-as-Stack.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96431\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque-as-Stack.jpg\" alt=\"Deque as Stack\" width=\"700\" height=\"300\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque-as-Stack.jpg 700w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque-as-Stack-300x129.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque-as-Stack-150x64.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque-as-Stack-520x223.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque-as-Stack-320x137.jpg 320w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque-as-Queue.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96432\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque-as-Queue.jpg\" alt=\"Deque as Queue\" width=\"700\" height=\"300\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque-as-Queue.jpg 700w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque-as-Queue-300x129.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque-as-Queue-150x64.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque-as-Queue-520x223.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Deque-as-Queue-320x137.jpg 320w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p><b>Input restricted queue:<\/b><span style=\"font-weight: 400;\"> In input restricted queue we can insert data at only one end deletion is possible on both ends.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Input-Restricted-Queue.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96433\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Input-Restricted-Queue.jpg\" alt=\"Input Restricted Queue\" width=\"700\" height=\"300\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Input-Restricted-Queue.jpg 700w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Input-Restricted-Queue-300x129.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Input-Restricted-Queue-150x64.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Input-Restricted-Queue-520x223.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Input-Restricted-Queue-320x137.jpg 320w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p><b>Output restricted queue:<\/b><span style=\"font-weight: 400;\"> In output restricted queue we can delete data from only one end but insertion can be done on both ends.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Output-Restricted-Queue.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96434\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Output-Restricted-Queue.jpg\" alt=\"Output Restricted Queue\" width=\"700\" height=\"300\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Output-Restricted-Queue.jpg 700w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Output-Restricted-Queue-300x129.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Output-Restricted-Queue-150x64.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Output-Restricted-Queue-520x223.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Output-Restricted-Queue-320x137.jpg 320w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<h4>Operations on Deque<\/h4>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Insert at front\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Delete from end<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Insert at rear<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Delete from rear<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Isfull()<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Isempty ()<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Deque is implemented using two data structures: circular array and circular linked list.<\/span><\/p>\n<h5>1. Circular array<\/h5>\n<p><span style=\"font-weight: 400;\">In a circular array, the last element is connected to the first element of the array. If we want to insert a new element but the rear is already at the last position, and the first position of the circular array is empty, then it will not show any error, since the last and first elements of the circular array are connected.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-Array.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96435\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-Array.jpg\" alt=\"Circular Array\" width=\"700\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-Array.jpg 700w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-Array-300x269.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-Array-150x135.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-Array-520x467.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Circular-Array-320x287.jpg 320w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<h4>Applications of dequeue<\/h4>\n<p><span style=\"font-weight: 400;\">1. Deque can be used as a stack or queue.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2. It is used as a palindrome checker since a string can be read from both ends.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">3. Deque is used in multiprocessor scheduling. Multiprocessor scheduling using deque is done with the help of an <\/span><b>A-steal algorithm<\/b><span style=\"font-weight: 400;\">.<\/span><\/p>\n<h3>Array implementation<span style=\"font-weight: 400;\"> of Deque<\/span><\/h3>\n<h4>Enqueue<\/h4>\n<p><span style=\"font-weight: 400;\">1. If the array is empty, set both front and rear to -1.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2. Insert the first element and set front and rear to 0.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">3. Insert the next element and increase the value of the rear by one every time.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">4. If you want to insert a new element at the front, decrease the value of the front by one. If the front is at zero, after deletion the front is set to the last index of the circular array.<\/span><\/p>\n<h4>Dequeue<\/h4>\n<p><span style=\"font-weight: 400;\">1. Delete an element from the front and increase the value of the front by one.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2. Delete an element from the rear and decrease the value of the rear by one.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">3. If the rear is pointing to the first element, after deleting, set the rear to the last index of the circular array.<\/span><\/p>\n<p><b>Working of Deque<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Let us consider a queue of max size 9:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">1: Deque is empty. Front = Rear = -1<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Empty-Deque.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96436\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Empty-Deque.png\" alt=\"Empty Deque\" width=\"340\" height=\"36\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Empty-Deque.png 340w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Empty-Deque-300x32.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Empty-Deque-150x16.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Empty-Deque-320x34.png 320w\" sizes=\"auto, (max-width: 340px) 100vw, 340px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">2: Insert \u201cD\u2019 at the rear.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insertion-in-Deque.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96437\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insertion-in-Deque.png\" alt=\"Insertion in Deque\" width=\"340\" height=\"175\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insertion-in-Deque.png 340w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insertion-in-Deque-300x154.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insertion-in-Deque-150x77.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insertion-in-Deque-320x165.png 320w\" sizes=\"auto, (max-width: 340px) 100vw, 340px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">3: Insert \u2018A\u2019 and \u2018T\u2019 at the rear.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insertion-at-rear-end.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96438\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insertion-at-rear-end.png\" alt=\"Insertion at rear end\" width=\"340\" height=\"107\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insertion-at-rear-end.png 340w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insertion-at-rear-end-300x94.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insertion-at-rear-end-150x47.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insertion-at-rear-end-320x101.png 320w\" sizes=\"auto, (max-width: 340px) 100vw, 340px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">4: Insert \u2018R\u2019 at the front.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-at-front-.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96439\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-at-front-.png\" alt=\"Insert at front\" width=\"340\" height=\"107\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-at-front-.png 340w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-at-front--300x94.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-at-front--150x47.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-at-front--320x101.png 320w\" sizes=\"auto, (max-width: 340px) 100vw, 340px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">5: Insert \u2018I\u2019 at the front and \u2018A\u2019 at the rear.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-at-Front-and-rear.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96440\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-at-Front-and-rear.png\" alt=\"Insert at Front and rear\" width=\"340\" height=\"112\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-at-Front-and-rear.png 340w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-at-Front-and-rear-300x99.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-at-Front-and-rear-150x49.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-at-Front-and-rear-320x105.png 320w\" sizes=\"auto, (max-width: 340px) 100vw, 340px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">6: Delete at rear and front.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-at-front-.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96439\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-at-front-.png\" alt=\"Delete at front\" width=\"340\" height=\"107\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-at-front-.png 340w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-at-front--300x94.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-at-front--150x47.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-at-front--320x101.png 320w\" sizes=\"auto, (max-width: 340px) 100vw, 340px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">7: Insert \u2018A\u2019, \u2018F\u2019, \u2018L\u2019 at the rear and \u2018I\u2019, \u2018A\u2019 at front.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-Elements-in-Queue.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96441\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-Elements-in-Queue.png\" alt=\"Insert Elements in Queue\" width=\"340\" height=\"179\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-Elements-in-Queue.png 340w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-Elements-in-Queue-300x158.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-Elements-in-Queue-150x79.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-Elements-in-Queue-320x168.png 320w\" sizes=\"auto, (max-width: 340px) 100vw, 340px\" \/><\/a><\/p>\n<h4>Insert at front<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">void enqueue_front(int new)  \r\n{  \r\n  \r\n    if((front==-1) &amp;&amp; (rear==-1))  \r\n    {  \r\n        front=rear=0;  \r\n        deque[front]=new;  \r\n    }  \r\n    else if(front==0)  \r\n    {  \r\n        front=size-1;  \r\n        deque[front]=new;  \r\n    }  \r\n    else  \r\n    {  \r\n        front=front-1;  \r\n        deque[front]=new;  \r\n    }  \r\n} \r\n<\/pre>\n<h4>Insert at rear<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">void enqueue_rear(int new)  \r\n{  \r\n      else if((front==-1) &amp;&amp; (rear==-1))  \r\n    {  \r\n        rear=0;  \r\n        deque[rear]=new;  \r\n    }  \r\n    else if(rear==size-1)  \r\n    {  \r\n        rear=0;  \r\n        deque[rear]=new;  \r\n    }  \r\n    else  \r\n    {  \r\n        rear++;  \r\n        deque[rear]=new;  \r\n    }  \r\n}\r\n<\/pre>\n<h4>Delete at front<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">void dequeue_front()  \r\n{  \r\n    if((front==-1) &amp;&amp; (rear==-1))  \r\n    {  \r\n        printf(\"Underflow\");  \r\n    }  \r\n    else if(front==rear)  \r\n    {  \r\n        printf( deque[front]);  \r\n        front=-1;  \r\n        rear=-1;      \r\n    }  \r\n     else if(front==(size-1))  \r\n     {  \r\n         printf(deque[front]);  \r\n         front=0;  \r\n     }  \r\n     else  \r\n     {  \r\n          printf(deque[front]);  \r\n          front=front+1;  \r\n     }  \r\n} \r\n<\/pre>\n<h4>Delete at end<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">void dequeue_rear()  \r\n{  \r\n    if((front==-1) &amp;&amp; (rear==-1))  \r\n    {  \r\n        printf(\"Underflow\");  \r\n    }  \r\n    else if(front==rear)  \r\n    {  \r\n        printf(deque[rear]);  \r\n        front=-1;  \r\n        rear=-1;  \r\n          \r\n    }  \r\n     else if(rear==0)  \r\n     {  \r\n         printf(deque[rear]);  \r\n         rear=size-1;  \r\n     }  \r\n     else  \r\n     {  \r\n          printf(deque[rear]);  \r\n          rear=rear-1;  \r\n     }  \r\n}  \r\n\r\n<\/pre>\n<h4>Get front<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int getfront()  \r\n{  \r\n    if((front==-1) &amp;&amp; (rear==-1))  \r\n    {  \r\n        printf(\"Underflow\");  \r\n        return -1;\r\n    }  \r\n    else  \r\n        return deque[front];  \r\n} \r\n\r\n<\/pre>\n<h4>Get end<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int getrear()  \r\n{  \r\n    if((front==-1) &amp;&amp; (rear==-1))  \r\n    {  \r\n        printf(\"Underflow\"); \r\n        return -1; \r\n    }  \r\n    else  \r\n    {  \r\n       return deque[rear];  \r\n    }   \r\n} \r\n\r\n<\/pre>\n<h3>Priority queue<\/h3>\n<p><span style=\"font-weight: 400;\">A priority queue is also an abstract data type similar to a linear queue. The only thing that makes it different from other types of queues is that each element has a priority assigned to it. Element with the highest priority comes first in a priority queue. Priority determines the order in which elements are accessed and removed from the queue.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In the priority queue, elements are either arranged in ascending or descending order.<\/span><\/p>\n<p><b>Characteristics of priority queue<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Every element in the queue has some priority.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Elements with higher priority are accessed and removed first.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">In case there are two elements with the same priority, the FIFO principle is followed.<\/span><\/li>\n<\/ul>\n<h3>Types of priority queue<\/h3>\n<h4>1. Ascending order priority queue:<\/h4>\n<p><span style=\"font-weight: 400;\">Here, a low-value number is given a higher priority than a high-value number. For example:\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Ascending-Priority-in-queue.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96442\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Ascending-Priority-in-queue.jpg\" alt=\"Ascending Priority in queue\" width=\"600\" height=\"300\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Ascending-Priority-in-queue.jpg 600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Ascending-Priority-in-queue-300x150.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Ascending-Priority-in-queue-150x75.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Ascending-Priority-in-queue-520x260.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Ascending-Priority-in-queue-320x160.jpg 320w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<h4>2. Descending order priority queue:<\/h4>\n<p><span style=\"font-weight: 400;\">Here, a high-value number is given a higher priority than a low-value number. For example:<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Descending-Priority.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96443\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Descending-Priority.jpg\" alt=\"Descending Priority\" width=\"600\" height=\"300\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Descending-Priority.jpg 600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Descending-Priority-300x150.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Descending-Priority-150x75.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Descending-Priority-520x260.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Descending-Priority-320x160.jpg 320w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<h3>Implementation of priority queue<\/h3>\n<p><span style=\"font-weight: 400;\">There are four ways to implement priority queues:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Array<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Linked list<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Heap data structure<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Binary search tree<\/span><\/li>\n<\/ul>\n<p><b>1. Heap Data structure: <\/b><span style=\"font-weight: 400;\">\u00a0A tree-based Data Structure that forms a complete binary tree and satisfies heap property. <\/span><span style=\"font-weight: 400;\">If A is the parent of B, then A is ordered with respect to B for all A and B in a heap. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">There are two types of heaps :<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><b>MAX Heap:<\/b><span style=\"font-weight: 400;\"> The value of the parent node is greater than the value of the child node.<\/span><\/li>\n<\/ul>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Max-Heap.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96444\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Max-Heap.jpg\" alt=\"Max Heap\" width=\"700\" height=\"600\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Max-Heap.jpg 700w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Max-Heap-300x257.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Max-Heap-150x129.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Max-Heap-520x446.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Max-Heap-320x274.jpg 320w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><b>MIN Heap:<\/b><span style=\"font-weight: 400;\"> The value of the parent node is less than the value of the child node.<\/span><\/li>\n<\/ul>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Min-Heap.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96445\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Min-Heap.jpg\" alt=\"Min Heap\" width=\"700\" height=\"600\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Min-Heap.jpg 700w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Min-Heap-300x257.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Min-Heap-150x129.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Min-Heap-520x446.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Min-Heap-320x274.jpg 320w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<h3>Operations on a priority queue<\/h3>\n<h4>Inserting the element in a priority queue (MAX Heap)<\/h4>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">When we insert an element in a MAX heap priority queue, it is added to the first empty slot by looking from top to bottom and left to right.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">If the element is not in the correct position, it is compared with the parent\u2019s node. The position of the new element and parent node is interchanged if they are found to be not in order. This process is called heapify.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">This process is repeated until the complete correct order is achieved.<\/span><\/li>\n<\/ol>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-Element-in-heap.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96446\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-Element-in-heap.jpg\" alt=\"Insert Element in heap\" width=\"900\" height=\"600\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-Element-in-heap.jpg 900w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-Element-in-heap-300x200.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-Element-in-heap-150x100.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-Element-in-heap-768x512.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-Element-in-heap-720x480.jpg 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-Element-in-heap-520x347.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-Element-in-heap-320x213.jpg 320w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Insert-Element-in-heap-272x182.jpg 272w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Heap-in-Queue.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96447\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Heap-in-Queue.jpg\" alt=\"Heap in Queue\" width=\"700\" height=\"600\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Heap-in-Queue.jpg 700w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Heap-in-Queue-300x257.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Heap-in-Queue-150x129.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Heap-in-Queue-520x446.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Heap-in-Queue-320x274.jpg 320w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<h4>Time Complexity<\/h4>\n<table>\n<tbody>\n<tr>\n<td><b>Implementation<\/b><\/td>\n<td><b>insert<\/b><\/td>\n<td><b>delete<\/b><\/td>\n<td><b>peek<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Linked list<\/span><\/td>\n<td><span style=\"font-weight: 400;\">O(1)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">O(n)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">O(n)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Binary heap<\/span><\/td>\n<td><span style=\"font-weight: 400;\">O(log n)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">O(log n)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">O(1)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Binary search tree<\/span><\/td>\n<td><span style=\"font-weight: 400;\">O(log n)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">O(log n)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">O(1)<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Applications of priority queue<\/h3>\n<p>1. Used for finding the shortest path in Dijkstra\u2019s algorithm.<br \/>\n2. Used in Huffman Code and Heap sort.<br \/>\n3. Used by operation system for priority scheduling, interrupt handling, and load balancing<br \/>\n4. Used in prim\u2019s algorithm.<br \/>\n5. Operating systems use a priority queue for maintaining the processes that are to be pulled for execution from their waiting states.<\/p>\n<h4>Linear queue implementation in C<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\r\n#define MAXSIZE 5\r\n\r\nvoid Enqueue(int);\r\nvoid Dequeue();\r\nvoid Display();\r\n\r\nint items[MAXSIZE], front = -1, rear = -1;\r\n\r\nint main() {\r\n\r\n  Enqueue(10);\r\n  Enqueue(29);\r\n  Enqueue(31);\r\n  Enqueue(44);\r\n  Enqueue(57);\r\n  Enqueue(63);\r\n\r\n  Display();\r\n\r\n  Dequeue();\r\n  Display();\r\n\r\n  return 0;\r\n}\r\n\r\nvoid Enqueue(int val) {\r\n  if (rear == MAXSIZE - 1)\r\n    printf(\"Overflow\");\r\n  else {\r\n    if (front == -1)\r\n      front = 0;\r\n    rear++;\r\n    items[rear] = val;\r\n    printf(\"\\nInserted Value -&gt; %d\", val);\r\n  }\r\n}\r\n\r\nvoid Dequeue() {\r\n  if (front == -1)\r\n    printf(\"Underflow\");\r\n  else {\r\n    printf(\"\\nDeleted Value : %d\", items[front]);\r\n    front++;\r\n    if (front &gt; rear)\r\n      front = rear = -1;\r\n  }\r\n}\r\n\r\nvoid Display() {\r\n  if (rear == -1)\r\n    printf(\"Underflow\");\r\n  else {\r\n    int i;\r\n    printf(\"\\nCurrent elements in Queue :\\n\");\r\n    for (i = front; i &lt;= rear; i++)\r\n      printf(\"%d  \", items[i]);\r\n  }\r\n  printf(\"\\n\");\r\n}\r\n\r\n\r\n\r\n<\/pre>\n<h4>Linear queue implementation in C++<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\r\n#define MAXSIZE 5\r\n\r\nusing namespace std;\r\n\r\nclass Queue {\r\n   private:\r\n  int items[MAXSIZE], front, rear;\r\n\r\n   public:\r\n  Queue() {\r\n    front = -1;\r\n    rear = -1;\r\n  }\r\n\r\n  bool isFull() {\r\n    if (front == 0 &amp;&amp; rear == MAXSIZE - 1) {\r\n      return true;\r\n    }\r\n    return false;\r\n  }\r\n\r\n  bool isEmpty() {\r\n    if (front == -1)\r\n      return true;\r\n    else\r\n      return false;\r\n  }\r\n\r\n  void Enqueue(int val) {\r\n    if (isFull()) {\r\n      cout &lt;&lt; \"Overflow\";\r\n    } else {\r\n      if (front == -1) front = 0;\r\n      rear++;\r\n      items[rear] = val;\r\n      cout &lt;&lt; endl\r\n         &lt;&lt; \"added \" &lt;&lt; val &lt;&lt; endl;\r\n    }\r\n  }\r\n\r\n  int Dequeue() {\r\n    int element;\r\n    if (isEmpty()) {\r\n      cout &lt;&lt; \"Underflow\" &lt;&lt; endl;\r\n      return (-1);\r\n    } else {\r\n      element = items[front];\r\n      if (front &gt;= rear) {\r\n        front = -1;\r\n        rear = -1;\r\n      } \r\n      else {\r\n        front++;\r\n      }\r\n      cout &lt;&lt; endl\r\n         &lt;&lt; \"Deleted -&gt; \" &lt;&lt; element &lt;&lt; endl;\r\n      return (element);\r\n    }\r\n  }\r\n\r\n  void Display() {\r\n    \r\n    int i;\r\n    if (isEmpty()) {\r\n      cout &lt;&lt; endl\r\n         &lt;&lt; \"Queue Empty\" &lt;&lt; endl;\r\n    } else {\r\n      cout &lt;&lt; endl\r\n         &lt;&lt; \"Front -&gt; \" &lt;&lt; front;\r\n      cout &lt;&lt; endl\r\n         &lt;&lt; \"Items -&gt; \";\r\n      for (i = front; i &lt;= rear; i++)\r\n        cout &lt;&lt; items[i] &lt;&lt; \"  \";\r\n      cout &lt;&lt; endl\r\n         &lt;&lt; \"Rear -&gt; \" &lt;&lt; rear &lt;&lt; endl;\r\n    }\r\n  }\r\n};\r\n\r\nint main() {\r\n  Queue q;\r\n\r\n\r\n  q.Dequeue();\r\n\r\n  \r\n  q.Enqueue(1);\r\n  q.Enqueue(2);\r\n  q.Enqueue(3);\r\n  q.Enqueue(4);\r\n  q.Enqueue(5);\r\n  q.Enqueue(6);\r\n\r\n  q.Display();\r\n  q.Dequeue();\r\n  q.Display();\r\n\r\n  return 0;\r\n}\r\n\r\n\r\n\r\n<\/pre>\n<h4>Implementation of Linear queue in JAVA<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class Queue {\r\n  int MAXSIZE = 5;\r\n  int items[] = new int[MAXSIZE];\r\n  int front, rear;\r\n\r\n  Queue() {\r\n    front = -1;\r\n    rear = -1;\r\n  }\r\n\r\n  boolean isFull() {\r\n    if (front == 0 &amp;&amp; rear == MAXSIZE - 1) \r\n{\r\n      return true;\r\n    }\r\n    return false;\r\n  }\r\n\r\n  boolean isEmpty() \r\n{\r\n    if (front == -1)\r\n      return true;\r\n    else\r\n      return false;\r\n  }\r\n\r\n  void Enqueue(int Val) \r\n{\r\n    if (isFull()) \r\n{\r\n      System.out.println(\"Overflow\");\r\n    } \r\nelse \r\n{\r\n      if (front == -1)\r\n        front = 0;\r\n      rear++;\r\n      items[rear] = Valt;\r\n      System.out.println(\"Inserted \" + Val);\r\n    }\r\n  }\r\n\r\n  int Dequeue() {\r\n    int Val;\r\n    if (isEmpty()) {\r\n      System.out.println(\"Underflow\");\r\n      return (-1);\r\n    } \r\nelse \r\n{\r\n     Val = items[front];\r\n      if (front &gt;= rear)\r\n {\r\n        front = -1;\r\n        rear = -1;\r\n      } \r\n\r\n      else\r\n {\r\n        front++;\r\n      }\r\n      System.out.println(\"Deleted -&gt; \" + Val);\r\n      return (Val);\r\n    }\r\n  }\r\n\r\n  void display() {\r\n    \r\n    int i;\r\n    if (isEmpty()) {\r\n      System.out.println(\"Empty Queue\");\r\n    } else {\r\n      System.out.println(\"\\nFront -&gt; \" + front);\r\n      System.out.println(\"Items -&gt; \");\r\n      for (i = front; i &lt;= rear; i++)\r\n        System.out.print(items[i] + \"  \");\r\n\r\n      System.out.println(\"\\nRear -&gt; \" + rear);\r\n    }\r\n  }\r\n\r\n  public static void main(String[] args) {\r\n    Queue q = new Queue();\r\n\r\n    q.Enqueue();\r\n\r\n    q.Enqueue(1);\r\n    q.Enqueue(2);\r\n    q.Enqueue(3);\r\n    q.Enqueue(4);\r\n    q.Enqueue(5);\r\n\r\n    q.Enqueue(6);\r\n\r\n    q.display();\r\n    q.Dequeue();\r\n    q.display();\r\n  }\r\n}\r\n<\/pre>\n<h4>Linear queue implementation in Python<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class Queue:\r\n\r\n    def __init__(self):\r\n        self.queue = []\r\n\r\n   \r\n    def Enqueue(self, item):\r\n        self.queue.append(item)\r\n\r\n  \r\n    def Dequeue(self):\r\n        if len(self.queue) &lt; 1:\r\n            return None\r\n        return self.queue.pop(0)\r\n\r\n    \r\n    def display(self):\r\n        print(self.queue)\r\n\r\n    def size(self):\r\n        return len(self.queue)\r\n\r\n\r\nq = Queue()\r\nq.Enqueue(1)\r\nq.Enqueue(2)\r\nq.Enqueue(3)\r\nq.Enqueue(4)\r\nq.Enqueue(5)\r\n\r\nq.display()\r\n\r\nq.Dequeue()\r\n\r\nprint(\"After removing an element\")\r\nq.display()\r\n\r\n<\/pre>\n<h3>Conclusion<\/h3>\n<p>Queues are one of the major data structures used widely due to their multiple forms and their ability to act at both stack and queue. Multiple variations of queues make them suitable for different kinds of tasks.<\/p>\n<p>In this article, we learned about different forms of queues, the different operations of each of them, and the implementation of a linear queue.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Imagine yourself in the same example as we discussed in the last article on stacks. You are standing in a line for buffet dinner and many people are standing ahead of and behind you.&#46;&#46;&#46;<\/p>\n","protected":false},"author":7,"featured_media":96414,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24020],"tags":[24477,24475,24476],"class_list":["post-96369","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-structure-tutorials","tag-applications-of-queue","tag-queue-in-data-structure","tag-types-of-queue"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Queue in Data Structure - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn what is queue in data structure and various operations that can be performed on it. Check the types of queue and their applications.\" \/>\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\/queue-in-data-structure\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Queue in Data Structure - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn what is queue in data structure and various operations that can be performed on it. Check the types of queue and their applications.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/queue-in-data-structure\/\" \/>\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=\"2021-06-09T03:30:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Queue-in-DS.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\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":"Queue in Data Structure - DataFlair","description":"Learn what is queue in data structure and various operations that can be performed on it. Check the types of queue and their applications.","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\/queue-in-data-structure\/","og_locale":"en_US","og_type":"article","og_title":"Queue in Data Structure - DataFlair","og_description":"Learn what is queue in data structure and various operations that can be performed on it. Check the types of queue and their applications.","og_url":"https:\/\/data-flair.training\/blogs\/queue-in-data-structure\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2021-06-09T03:30:57+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Queue-in-DS.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\/queue-in-data-structure\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/queue-in-data-structure\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd"},"headline":"Queue in Data Structure","datePublished":"2021-06-09T03:30:57+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/queue-in-data-structure\/"},"wordCount":2556,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/queue-in-data-structure\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Queue-in-DS.jpg","keywords":["Applications of queue","Queue in Data Structure","Types of queue"],"articleSection":["Data Structure Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/queue-in-data-structure\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/queue-in-data-structure\/","url":"https:\/\/data-flair.training\/blogs\/queue-in-data-structure\/","name":"Queue in Data Structure - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/queue-in-data-structure\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/queue-in-data-structure\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Queue-in-DS.jpg","datePublished":"2021-06-09T03:30:57+00:00","description":"Learn what is queue in data structure and various operations that can be performed on it. Check the types of queue and their applications.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/queue-in-data-structure\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/queue-in-data-structure\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/queue-in-data-structure\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Queue-in-DS.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Queue-in-DS.jpg","width":1200,"height":628,"caption":"Queue in Data Structure"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/queue-in-data-structure\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Data Structure Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/data-structure-tutorials\/"},{"@type":"ListItem","position":3,"name":"Queue in Data Structure"}]},{"@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\/beb0cab24b7aa54423a3b50e669a9dcd","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team specializes in creating clear, actionable content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Backed by industry expertise, we make learning easy and career-oriented for beginners and pros alike.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam3\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/96369","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\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=96369"}],"version-history":[{"count":6,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/96369\/revisions"}],"predecessor-version":[{"id":96449,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/96369\/revisions\/96449"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/96414"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=96369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=96369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=96369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}