Notes – Queue in DSA

What is a Queue?

  • A Queue is a linear data structure that follows the FIFO principle:
    First In, First Out
  • The first element added is the first one to be removed.

Real-Life Examples

  • Waiting line at a ticket counter: First person in line gets served first.
  • Printer queue: Documents are printed in the order they were added.
  • Task scheduling: Tasks are processed in the order they arrive.

Basic Queue Operations


OperationDescription
enqueue()Add an element to the rear of the queue
dequeue()Remove and return the front element
peek() / front()View the front element without removing it
isEmpty()Check if the queue is empty

Queue Implementation

Queues can be implemented using:

  • Arrays
  • Linked Lists
  • Two Stacks (for advanced scenarios)

Types of Queues


TypeDescription
Simple QueueBasic FIFO structure
Circular QueueLast position is connected back to the first
Deque (Double Ended Queue)Insertion/deletion from both ends
Priority QueueElements are processed based on priority

Time Complexity


OperationTime Complexity
enqueueO(1)
dequeueO(1)
peekO(1)

Like Stack, Queue operations are also highly efficient.


Applications of Queue

  • CPU and disk scheduling
  • Breadth-First Search (BFS) in graphs
  • Data buffering (e.g., IO Buffers)
  • Handling requests in web servers