Think about the last time you waited in line. At a grocery store or at a coffee shop. The first person to arrive is the first person to be served. That’s a queue!
A queue is a collection of items with one rule: the first item in is the first item out. This is called FIFO — First In, First Out.
It’s the exact opposite of a stack. Where a stack serves the most recent item first, a queue serves the oldest.
Just like a stack, a queue only needs two things to work:
Enqueue — add an item to the back of the line.
Dequeue — remove the item from the front of the line.
Some implementations add:
Peek — look at the item at the front without removing it. The queue doesn’t change. You’re just checking who’s next.
Start with an empty queue. Enqueue four values one at a time.
enqueue(10) → [ 10 ]
enqueue(20) → [ 10 | 20 ]
enqueue(30) → [ 10 | 20 | 30 ]
enqueue(40) → [ 10 | 20 | 30 | 40 ]
↑ front ↑ back
Now dequeue twice.
dequeue() → returns 10 [ 20 | 30 | 40 ]
dequeue() → returns 20 [ 30 | 40 ]
Items leave from the front, not the back. The first one in is the first one out — exactly like the line at the grocery store.
Print jobs. When you send multiple documents to a printer, they don’t print in random order. They queue up. The first one sent is the first one printed.
Keyboard input. When you type faster than a program can process, the keystrokes don’t get lost or scrambled. They sit in a queue and get handled in the order you pressed them.
Network traffic. Data packets travelling across a network pass through queues at every router. The order they arrive in is the order they get forwarded.
Breadth-first search. One of the core graph traversal algorithms uses a queue to explore nodes level by level — visiting everything at the current depth before going deeper. The queue is what makes that orderly expansion possible.
Next up: linked lists — a different way of thinking about how items connect to each other in memory.