Skip to main content

Queue

Overview

A Queue is an abstract data type (ADT) that follows the First In, First Out (FIFO) principle. The element that is added first is the first one to be removed.

Queues model orderly processing and fair scheduling.


What Is a Queue?

A queue:

  • Allows insertion at the rear
  • Allows removal from the front
  • Maintains arrival order
  • Restricts random access

Core Queue Operations

OperationDescription
EnqueueAdd an element to the rear
DequeueRemove an element from the front
Front / PeekView the front element
IsEmptyCheck if queue is empty
SizeNumber of elements

How It Works

Queues are commonly implemented using:

  • Arrays (circular buffer)
  • Linked lists
  • Deques

The ADT defines behavior, not the internal structure.


Example

Pseudocode

queue.enqueue(10)
queue.enqueue(20)

queue.dequeue() // 10
queue.front() // 20

Real-world Analogy

A queue is like a line at a ticket counter. Whoever arrives first gets served first 🎟️🚶‍♀️🚶‍♂️.


Time Complexity

OperationTime
EnqueueO(1)
DequeueO(1)
PeekO(1)

Use Cases

  • Task scheduling
  • Print spooling
  • Breadth-first search
  • Producer–consumer systems
  • Message queues

Advantages

  • Fair processing order
  • Simple abstraction
  • Efficient operations

Limitations

  • No random access
  • Limited flexibility
  • Fixed access pattern

Variants

  • Circular queue
  • Priority queue
  • Double-ended queue (Deque)