algo·visual

Queue

Data Structures
avgO(1)worstO(1)spaceO(n)
← out
7front
19rear
← in

A queue of 2; 7 is at the front

1 / 12
Starting contents2 values
Operations7 / 40
  1. 1. enqueue(31)
  2. 2. enqueue(4)
  3. 3. dequeue()
  4. 4. enqueue(56)
  5. 5. dequeue()
  6. 6. dequeue()
  7. 7. enqueue(12)

How it works

First in, first out: enqueue joins at the rear and dequeue leaves from the front. Keeping a moving head index instead of shifting the array makes both operations O(1). Breadth-first search, schedulers and buffers all use queues.

Complexity

Best
O(1)
Average
O(1)
Worst
O(1)
Space
O(n)

Search algorithms

Jump to any algorithm or page