Queues (FIFO)

A queue adds at the back and removes from the front — just like a line at a café. First in, first out.

Data structures Beginner FIFO ~5 min
The idea

First in, first out

Enqueue adds to the back of the line. Dequeue removes from the front. Nobody cuts ahead — the oldest waiting value leaves first.

Queues show up in printers, task schedulers, breadth-first search, and message buffers.

TipCompare with a stack: queues are FIFO; stacks are LIFO.
IdeaOperations
queue_demo.pyPython
from collections import deque
q = deque()
q.append(3)    # enqueue
q.append(8)
q.append(1)
q.popleft()       # → 3
q.append(5)
q.popleft()       # → 8

Python’s deque makes front removals efficient.

Interactive

Run the simulation

Left is the front (next out). Right is the back (new arrivals).

Step 0 / 0
demo.pyPython
Queue + variablesdemo
Ready
Shortcuts: Space play/pause · → step · ← back · R reset
Try it yourself

Enqueue and dequeue freely

Build your own queue. Enqueue adds at the back; dequeue removes from the front.

Ready
Queue is empty. Enter a value and press Enqueue.
Remember

Queue mental model

Two ends, one purpose: fair ordering.

FIFO

First in, first out

The earliest enqueued value is always the next to dequeue.

Ends

Front vs back

Remove from the front, add at the back — never the reverse for a basic queue.

Uses

Scheduling & BFS

Any “wait your turn” problem is a queue in disguise.