{"id":20239,"date":"2026-04-07T10:15:55","date_gmt":"2026-04-07T04:45:55","guid":{"rendered":"https:\/\/www.placementpreparation.io\/blog\/?p=20239"},"modified":"2026-04-10T17:44:18","modified_gmt":"2026-04-10T12:14:18","slug":"queue-in-data-structure","status":"publish","type":"post","link":"https:\/\/www.placementpreparation.io\/blog\/queue-in-data-structure\/","title":{"rendered":"Queue in Data Structure"},"content":{"rendered":"<?xml encoding=\"utf-8\" ?><p>A queue is a linear data structure that follows the FIFO (First In First Out) principle, meaning the first element inserted is the first one to be removed. This behavior makes queues ideal for systems where tasks must be processed in the exact order they arrive.<\/p><p>Queues are widely used in operating systems, scheduling systems, buffering, and graph algorithms. Learning queues helps you understand data flow control, order-based processing, and algorithm design patterns like Breadth First Search (BFS).<\/p><p>In this guide, you will learn how queues work internally, their types, operations with coding examples, real-world applications, and why queues are important for DSA and technical interviews.<\/p><h2>What is a Queue in Data Structure<\/h2><p>A <a href=\"https:\/\/www.placementpreparation.io\/dsa\/stacks-and-queues\/\" target=\"_blank\" rel=\"noopener\">queue is a linear data structure<\/a> where:<\/p><ul>\n<li>Elements are inserted from the rear<\/li>\n<li>Elements are removed from the front<\/li>\n<li>Processing follows FIFO order<\/li>\n<\/ul><p><strong>Basic representation:<\/strong><\/p><div class=\"su-note\" style=\"border-color:#dddfde;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#f7f9f8;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>Front &rarr; 10 &rarr; 20 &rarr; 30 &rarr; Rear<\/p>\n<\/div><\/div><p><strong>Example scenario:<\/strong><\/p><div class=\"su-note\" style=\"border-color:#dddfde;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#f7f9f8;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>If three tasks arrive in order A, B, C:<\/p>\n<p>Processing order will be: A &rarr; B &rarr; C<\/p>\n<\/div><\/div><p><strong>Key characteristics:<\/strong><\/p><ul>\n<li>FIFO structure<\/li>\n<li>Ordered processing<\/li>\n<li>Two pointers (front and rear)<\/li>\n<li>No random access<\/li>\n<li>Used for scheduling problems<\/li>\n<\/ul><p><strong>Basic array declaration:<\/strong><\/p><div class=\"su-note\" style=\"border-color:#dddfde;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#f7f9f8;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>int queue[5];<br>\nint front = -1;<br>\nint rear = -1;<\/p>\n<\/div><\/div><h2>How Queue Works Internally<\/h2><p>Queues maintain two important pointers:<\/p><ul>\n<li><strong>Front pointer:<\/strong> Points to the first element.<\/li>\n<li><strong>Rear pointer:<\/strong> Points to the last inserted element.<\/li>\n<\/ul><p><strong>Example operations:<\/strong><\/p><div class=\"su-note\" style=\"border-color:#dddfde;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#f7f9f8;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>Enqueue 10<br>\nQueue: 10<br>\nFront = 0<br>\nRear = 0<\/p>\n<p>Enqueue 20<br>\nQueue: 10 20<br>\nRear = 1<\/p>\n<p>Enqueue 30<br>\nQueue: 10 20 30<br>\nRear = 2<\/p>\n<p>Dequeue<\/p>\n<p>Queue becomes:<br>\n20 30<br>\nFront moves to index 1.<\/p>\n<p>Important observation:<br>\nInsertion moves rear forward<br>\nDeletion moves front forward<\/p>\n<\/div><\/div><p>This is why queue insertion and deletion take O(1) time.<\/p><h2>Types of Queues in Data Structure<\/h2><h3>1. Linear Queue<\/h3><p>Basic queue implementation using arrays or linked lists.<\/p><p><strong>Problem:<\/strong> After multiple deletions, empty spaces cannot be reused.<\/p><p><strong>Example issue:<\/strong><\/p><div class=\"su-note\" style=\"border-color:#dddfde;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#f7f9f8;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\nQueue size = 5<br>\nAfter deletions:<br>\n_ _ 30 40 50\n<\/div><\/div><p>Front moved, but space was wasted.<\/p><p><strong>Used in:<\/strong><\/p><ul>\n<li>Simple scheduling systems<\/li>\n<li>Basic buffering<\/li>\n<\/ul><h3>2. Circular Queue<\/h3><p>A circular queue solves memory waste by connecting the last position to the first.<\/p><p><strong>Concept:<\/strong> Rear wraps around when space is available.<\/p><p><strong>Condition:<\/strong><\/p><div class=\"su-note\" style=\"border-color:#dddfde;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#f7f9f8;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>rear = (rear + 1) % size;<\/p>\n<\/div><\/div><p><strong>Used in:<\/strong><\/p><ul>\n<li>CPU scheduling<\/li>\n<li>Streaming buffers<\/li>\n<li>Network data handling<\/li>\n<\/ul><p><strong>Advantage:<\/strong> Efficient memory usage.<\/p><h3>3. Priority Queue<\/h3><p>In a priority queue, elements are processed based on priority instead of arrival order.<\/p><div class=\"su-note\" style=\"border-color:#dddfde;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#f7f9f8;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p><strong>Example:<\/strong> Priority 1 processed before Priority 5.<\/p>\n<\/div><\/div><p><strong>Used in:<\/strong><\/p><ul>\n<li>Operating systems scheduling<\/li>\n<li>Dijkstra algorithm<\/li>\n<li>Network routing<\/li>\n<\/ul><h3>4. Deque (Double-Ended Queue)<\/h3><p>Deque allows insertion and deletion from both ends.<\/p><p><strong>Types:<\/strong> Input restricted deque<br>\nOutput restricted deque<\/p><p><strong>Used in:<\/strong><\/p><ul>\n<li>Sliding window problems<\/li>\n<li>Cache implementation<\/li>\n<li>Palindrome checking<\/li>\n<\/ul><h2>Basic Queue Operations with Coding Examples<\/h2><h3>1. Enqueue (Insertion)<\/h3><p>Adds an element at the rear.<\/p><div class=\"su-note\" style=\"border-color:#dddfde;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#f7f9f8;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>rear++;<br>\nqueue[rear] = value;<br>\nIf queue was empty:<br>\nfront = rear = 0;<\/p>\n<\/div><\/div><p>Time complexity: O(1)<\/p><h3>2. Dequeue (Deletion)<\/h3><p>Removes the element from the front.<\/p><div class=\"su-note\" style=\"border-color:#dddfde;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#f7f9f8;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>front++;<\/p>\n<\/div><\/div><p><strong>If front passes rear:<\/strong> Queue becomes empty.<\/p><p>Time complexity: O(1)<\/p><h3>3. Peek Operation<\/h3><p>Returns the first element without removing it.<\/p><div class=\"su-note\" style=\"border-color:#dddfde;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#f7f9f8;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>printf(&ldquo;%d&rdquo;, queue[front]);<\/p>\n<\/div><\/div><h3>4. Queue Underflow Condition<\/h3><p>Occurs when deleting from an empty queue.<\/p><p><strong>Condition:<\/strong><\/p><div class=\"su-note\" style=\"border-color:#dddfde;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#f7f9f8;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>if(front == -1 || front &gt; rear)<\/p>\n<\/div><\/div><p>Queue is empty<\/p><h3>5. Queue Overflow Condition<\/h3><p>Occurs when inserting into the full queue.<\/p><p><strong>Condition:<\/strong><\/p><div class=\"su-note\" style=\"border-color:#dddfde;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#f7f9f8;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>if(rear == size-1)<\/p>\n<\/div><\/div><p>The queue is full<\/p><p><a href=\"https:\/\/www.guvi.in\/mlp\/fsd-student-program-wp?utm_source=placement_preparation&amp;utm_medium=blog_banner&amp;utm_campaign=queue_in_data_structure_horizontal\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" class=\"alignnone wp-image-15830 size-full\" src=\"https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2025\/06\/fsd-image-web-horizontal.webp\" alt=\"fsd zen lite free trial banner horizontal\" width=\"1920\" height=\"507\" srcset=\"https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2025\/06\/fsd-image-web-horizontal.webp 1920w, https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2025\/06\/fsd-image-web-horizontal-300x79.webp 300w, https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2025\/06\/fsd-image-web-horizontal-1024x270.webp 1024w, https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2025\/06\/fsd-image-web-horizontal-768x203.webp 768w, https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2025\/06\/fsd-image-web-horizontal-1536x406.webp 1536w, https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2025\/06\/fsd-image-web-horizontal-150x40.webp 150w\" sizes=\"(max-width: 1920px) 100vw, 1920px\"><\/a><\/p><h2>When Should You Use a Queue in DSA<\/h2><p>Understanding when to use a queue is important for <a href=\"https:\/\/www.placementpreparation.io\/dsa\/\" target=\"_blank\" rel=\"noopener\">problem-solving<\/a>.<\/p><p><strong>Use Queue when:<\/strong><\/p><ul>\n<li>Order of processing matters<\/li>\n<li>Tasks arrive continuously<\/li>\n<li>BFS traversal required<\/li>\n<li>Streaming data processing<\/li>\n<li>Scheduling systems<\/li>\n<\/ul><p><strong>Avoid the queue when:<\/strong><\/p><ul>\n<li>Random access needed<\/li>\n<li>Priority-based processing required (use a priority queue)<\/li>\n<li>Stack behavior needed (LIFO)<\/li>\n<\/ul><p>This decision-making helps choose the correct data structure in interviews.<\/p><h2>Why Queues Are Important in DSA Problem Solving<\/h2><p>Queues are important because they model real-world task processing and enable important algorithms.<\/p><ul>\n<li><strong>Used in BFS traversal:<\/strong> Graph and tree level order traversal uses queues.<\/li>\n<li><strong>Helps in scheduling logic:<\/strong> Many process scheduling problems use queues.<\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/programming-interview-questions\/\" target=\"_blank\" rel=\"noopener\"><strong>Common interview questions:<\/strong><\/a> Queue using stacks, circular queue implementation.<\/li>\n<li><strong>Foundation for advanced problems:<\/strong> Used in producer-consumer problems and streaming logic.<\/li>\n<\/ul><p><strong>Queues also introduce techniques like:<\/strong><\/p><ul>\n<li>Level order traversal<\/li>\n<li>Sliding window technique<\/li>\n<li>BFS shortest path<\/li>\n<\/ul><h2>Real World Applications of Queues<\/h2><p>Queues are used in systems where tasks must be processed in arrival order.<\/p><ul>\n<li>Operating system scheduling: Processes handled in queue order.<\/li>\n<li>Printer spooler systems: Print jobs are processed sequentially.<\/li>\n<li>Call center management: Calls are handled based on arrival.<\/li>\n<li>Network packet buffering: Data packets are processed in order.<\/li>\n<li>Task scheduling systems: Job queues manage execution order.<\/li>\n<\/ul><p><strong>Example:<\/strong> When multiple print commands are given, the printer processes jobs in queue order.<\/p><h2>Advantages and Limitations of Queues<\/h2><h3>Advantages<\/h3><ul>\n<li>Maintains processing order<\/li>\n<li>Fast insertion and deletion<\/li>\n<li>Simple implementation<\/li>\n<li>Useful in scheduling algorithms<\/li>\n<\/ul><h3>Limitations<\/h3><ul>\n<li>Fixed size in array implementation<\/li>\n<li>Memory waste in a linear queue<\/li>\n<li>No direct access<\/li>\n<li>Requires pointer management<\/li>\n<\/ul><h2>Time Complexity of Queue Operations<\/h2><table style=\"height: 317px;\" width=\"470\" class=\"tablepress\">\n<thead><tr>\n<td>\n<p style=\"text-align: center;\"><b>Operation<\/b><\/p>\n<\/td>\n<td style=\"text-align: center;\"><b>Time Complexity<\/b><\/td>\n<\/tr><\/thead><tbody class=\"row-striping row-hover\">\n\n<tr>\n<td>\n<p style=\"text-align: center;\"><span style=\"font-weight: 400;\">Enqueue<\/span><\/p>\n<\/td>\n<td>\n<p style=\"text-align: center;\"><span style=\"font-weight: 400;\">O(1)<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p style=\"text-align: center;\"><span style=\"font-weight: 400;\">Dequeue<\/span><\/p>\n<\/td>\n<td>\n<p style=\"text-align: center;\"><span style=\"font-weight: 400;\">O(1)<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p style=\"text-align: center;\"><span style=\"font-weight: 400;\">Peek<\/span><\/p>\n<\/td>\n<td>\n<p style=\"text-align: center;\"><span style=\"font-weight: 400;\">O(1)<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><span style=\"font-weight: 400;\">Search<\/span><\/td>\n<td>\n<p style=\"text-align: center;\"><span style=\"font-weight: 400;\">O(n)<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table><p>This shows that queues are optimized for insertion and deletion, but not searching.<\/p><h2>Why Learning Queues is Important Before Advanced DSA<\/h2><p><strong>Queues help in understanding many advanced DSA concepts.<\/strong><\/p><ul>\n<li>Graph traversal algorithms<\/li>\n<li>Scheduling algorithms<\/li>\n<li>Streaming data problems<\/li>\n<li>System design basics<\/li>\n<\/ul><p><strong>Learning queues improves:<\/strong><\/p><ul>\n<li>Algorithm thinking<\/li>\n<li>Data flow understanding<\/li>\n<li>Interview preparation<\/li>\n<li>Coding confidence<\/li>\n<\/ul><h2>How to Practice Queue Problems for Placements<\/h2><p>Start with basic implementations and move toward algorithm problems.<\/p><p><strong>Beginner:<\/strong><\/p><ul>\n<li>Implement a queue using an array<\/li>\n<li>Implement a queue using a linked list<\/li>\n<li>Basic enqueue dequeue<\/li>\n<\/ul><p><strong>Intermediate:<\/strong><\/p><ul>\n<li>Circular queue<\/li>\n<li>Queue using stacks<\/li>\n<li>First non-repeating character<\/li>\n<\/ul><p><strong>Advanced:<\/strong><\/p><ul>\n<li>BFS traversal<\/li>\n<li>Sliding window maximum<\/li>\n<li>LRU cache logic<\/li>\n<\/ul><p>Practicing queues builds strong fundamentals for graph and system design problems.<\/p><h2>Final Words<\/h2><p>Queues are a fundamental data structure used in systems that require the ordered processing of tasks. They help developers understand FIFO processing, scheduling logic, and graph traversal techniques.<\/p><p>Mastering queues improves your ability to design algorithms and prepares you for technical interviews. Strong queue fundamentals also make it easier to understand advanced data structures and system design concepts.<\/p><h2>Frequently Asked Questions<\/h2><h3>1. What is a queue in a data structure?<\/h3><p>A queue is a linear data structure that follows FIFO order, where insertion happens at the rear and deletion happens at the front.<\/p><h3>2. What are the types of queues?<\/h3><p>Common types include linear queue, circular queue, priority queue, and deque.<\/p><h3>3. Why is a queue used in BFS?<\/h3><p>The queue ensures nodes are processed in the correct order during level-by-level traversal.<\/p><h3>4. What is the time complexity of queue operations?<\/h3><p>Enqueue and dequeue operations take O(1) time.<\/p><h3>5. Where are queues used in real life?<\/h3><p>Queues are used in scheduling systems, printer management, call handling systems, and network buffering.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A queue is a linear data structure that follows the FIFO (First In First Out) principle, meaning the first element inserted is the first one to be removed. This behavior makes queues ideal for systems where tasks must be processed in the exact order they arrive.Queues are widely used in operating systems, scheduling systems, buffering, [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":20254,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[],"class_list":["post-20239","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dsa"],"_links":{"self":[{"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/posts\/20239","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/comments?post=20239"}],"version-history":[{"count":2,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/posts\/20239\/revisions"}],"predecessor-version":[{"id":20253,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/posts\/20239\/revisions\/20253"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/media\/20254"}],"wp:attachment":[{"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/media?parent=20239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/categories?post=20239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/tags?post=20239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}