First in. First out.
Join at the rear. Leave from the front. The first value to arrive is the first one served.
THE QUEUE
Enqueue · Dequeue · PeekTwo ends. One fair order.
← DEQUEUE
FRONT / OLDESTENQUEUE ←
REAR / NEWEST
FRONT / OLDESTENQUEUE ←
REAR / NEWEST
∅No one is in line yet
Enqueue a value to start the queue.
Ready
Enqueue a value or play the example to see FIFO in action.
New arrivals never skip the line.
Each value keeps its arrival number, even after earlier values leave. Position 0 is always the current front.
SIZE0 items
FRONT VALUE—
REAR VALUE—
STATEEmpty
WHY A QUEUE?
Think of a line at a café.
People join at the back, and the person who arrived first is served next. A queue follows the same rule: first in, first out.
Enqueue(value)Add a value at the rear.
Dequeue()Remove and return the front value.
Peek()Read the front without changing the order.
Used in BFS, print jobs, and task scheduling. This array-backed implementation uses shift() for dequeue, which takes O(n). A queue with head/tail pointers can avoid shifting items.