DSA Queues


What is a Queue?

In a queue, items are lined up so that the one added earliest exits before all others. You can compare it to a real-life queue at a ticket Like a service counter, the one who enters first gets handled first — a rule known as First In, First Out (FIFO).


How Does a Queue Work?

A queue allows you to:

  • Enqueue – Add an item at the back.
  • Dequeue – Take out the element positioned at the front of the queue.
  • Peek/Front – View the first item without extracting it from the queue.
  • IsEmpty – Check if there are no items inside.

Each new element joins from the rear end and leaves from the front, maintaining a straight flow of data handling.

Example

class Queue:     
      def __init__(self):         
              self.items = []          

      def enqueue(self, item):         
              self.items.append(item)          

     def dequeue(self):         
              if not self.is_empty():             
                  return self.items.pop(0)         
              return None         

     def peek(self):         
              if not self.is_empty():             
                   return self.items[0]         
              return None          
 
     def is_empty(self):         
              return len(self.items) == 0  

# Usage 
q = Queue() 
q.enqueue("A") 
q.enqueue("B") 
q.enqueue("C") 
 
print(q.dequeue())  # Output: A 
Print(q.peek())     # Output: B 

Where is Queue Used?

Print Jobs: Documents are printed in the order they're added.

Task Scheduling: Processes handled in sequence.

Call Centers: Customers get connected in the order they arrive.

Breadth-First Search (BFS): A queue is used to explore nodes layer by layer.


Advantages of Queues:

  • Maintains clear and fair order of processing.
  • Useful in managing waiting lines in applications.
  • Supports smooth flow of tasks in operating systems and networks.

Limitations of Queues:

  • Cannot access middle elements directly.
  • In static queues (like array-based ones), space may be wasted if not managed well.
  • Slower removal (in some implementations) due to shifting items.

Types of Queues:

  • Simple Queue: Basic FIFO structure.
  • Circular Queue: Last position connects back to the front for efficient space usage.
  • Priority Queue: Elements are dequeued based on priority rather than order.
  • Deque (Double-Ended Queue): Insertion and deletion possible from both ends.

Conclusion

Queues provide an effective way to manage ordered tasks or data flow where the earliest item should be processed first. Whether implemented with arrays or linked lists, they are essential in many real-world and algorithmic applications.


Prefer Learning by Watching?

Watch these YouTube tutorials to understand DATA STRUCTURES ALGORITHMS Tutorial visually:

What You'll Learn:
  • 📌 4.1 Queue in Data Structure | Introduction to Queue | Data Structures Tutorials
  • 📌 Stacks And Queues In Data Structure | Data Structures And Algorithms Tutorial | Simplilearn
Previous Next