Queue (FIFO)

enqueue · dequeue O(1)

A queue is a first-in, first-out (FIFO) collection: items leave in the same order they arrived, like a line at a checkout. enqueue adds at the back and dequeue removes from the front, both in constant time. Queues drive scheduling, buffering, and breadth-first search.

Queue
Press ▶ to run
Edit the input and press Play

How it works

  1. enqueue adds a value at the back of the queue.
  2. dequeue removes and returns the value at the front.
  3. The front always holds the oldest item still waiting.

Pseudocode

1enqueue(value):               # O(1)2  items.append(value)         # add at the back34dequeue():                    # O(1)5  if items is empty: error6  return items.removeFirst()  # take from the front