Queue (FIFO)

First-In, First-Out sequence with front & rear boundary indicators

Capacity:4 / 8
FRONT (0)
10idx: 0
20idx: 1
30idx: 2
REAR (3)
40idx: 3
Step 1 of 1Action: idle

Queue initialized with 4 items.

Speed: 1x

Overview & Concept

A Queue is a linear data structure operating under the First-In, First-Out (FIFO) principle. Items are added at the rear (enqueue) and removed from the front (dequeue). Queues model real-world waiting lines, asynchronous message brokers, operating system I/O buffers, and breadth-first search graph explorations.

How It Works

FIFO Discipline: The element that has been in the queue the longest is the first one dequeued.

Dual Pointer Architecture: Maintained via a front pointer for read/dequeue and a rear pointer for write/enqueue.

O(1) Enqueue: Appending to the rear pointer executes in constant time.

O(1) Dequeue: Extracting from the front pointer executes in constant time.

Step-by-Step Execution

1. Underflow / Overflow CheckCheck if queue is empty before dequeue, or at capacity before enqueue.
2. EnqueuePlace incoming element at rear index; advance rear pointer; increment count.
3. DequeueRetrieve element from front index; advance front pointer; decrement count.
4. Empty State ResetWhen queue becomes empty (front == rear), reset pointers to initial baseline.

Asymptotic Complexity Analysis

MetricBest CaseAverage CaseWorst CaseSpace Complexity
Complexity BoundO(1) enqueue/dequeueO(1) all opsO(1) deterministicO(n)
ExplanationOptimal input arrangement minimizing operations.Expected time for uniformly distributed randomized inputs.Adversarial degenerate conditions requiring full traversal.Auxiliary memory allocated beyond original input size.

Pseudocode & Invariants

Algorithm Logic
// Queue FIFO Operations
operation enqueue(value):
  if isFull(): return error "Overflow"
  rear = rear + 1
  data[rear] = value

operation dequeue():
  if isEmpty(): return error "Underflow"
  val = data[front]
  front = front + 1
  return val
Algorithmic Invariants
  • FIFO Invariant: Elements exit the queue in the exact sequence they arrived.
  • Ordering Invariant: Front pointer monotonically precedes or equals rear pointer in linear queues.
Correctness Guarantee: Invariants hold true prior to loop entry, are preserved during every iteration, and guarantee correct post-condition upon termination.

Advantages & Disadvantages

Key Advantages

  • •Deterministic O(1) enqueue and dequeue operations.
  • •Fair, predictable ordering for batch processing and asynchronous tasks.
  • •Essential building block for concurrency and producer-consumer architectures.

Trade-offs & Limitations

  • •Naive array implementation wastes memory as front advances (addressed by Circular Queue).
  • •No random access to internal elements without dequeuing preceding items.

Real-World Applications

Asynchronous Task Queues

Message brokers (RabbitMQ, Kafka, AWS SQS) dispatching background jobs to workers.

Breadth-First Search (BFS)

Graph and tree level-order traversals finding shortest path in unweighted graphs.

Printer Spoolers & Hardware I/O

Queuing print jobs and network packet buffers in chronological arrival order.

Decision Guide: When to Use

Ideal Scenarios

  • ✓When requests must be handled fairly in order of arrival.
  • ✓When decoupling producers generating work from consumers processing work.
  • ✓When performing level-by-level breadth-first search.

Consider Alternatives When

  • ✕When high-priority items need to jump to the front of the line (use Priority Queue).
  • ✕When LIFO stack reversal is required.

Compare trade-offs and complexity across complementary tools in AlgoViz.