A queue is a data structure where the first item you add is the first item you remove — first-in, first-out, or FIFO. Picture a line at a coffee shop: whoever joins first gets served first, and new arrivals wait at the back. That single rule is what makes a queue a queue, and it turns out to be exactly the behavior you want in a huge number of real programs, from task schedulers to message systems.
How a queue works
A queue has two main operations:
- Enqueue — add an item to the back of the queue.
- Dequeue — remove and return the item at the front.
Both run in constant time in a well-built queue, meaning they take the same amount of time no matter how many items are waiting. Some queues add a peek operation to look at the front item without removing it.
| Operation |
What it does |
Typical cost |
| Enqueue |
Add to the back |
Constant time |
| Dequeue |
Remove from the front |
Constant time |
| Peek |
Read the front without removing |
Constant time |
| Is empty |
Check if anything is waiting |
Constant time |
Where queues are used
Queues show up wherever order and fairness matter:
- Task and job scheduling — work is processed in the order it arrives.
- Print spoolers — documents print in the order you send them.
- Message brokers (like the patterns behind many distributed systems) — events are consumed in order.
- Request handling — web servers queue incoming requests under load.
- Breadth-first search — exploring a graph level by level uses a queue.
Queue vs stack
The clearest way to understand a queue is to contrast it with a stack, its mirror image.
|
Queue (FIFO) |
Stack (LIFO) |
| Order |
First in, first out |
Last in, first out |
| Real-world analogy |
A checkout line |
A stack of plates |
| Add / remove |
Back / front |
Top / top |
| Used for |
Fair, in-order processing |
Undo, recursion, backtracking |
If you need things handled in the order they arrived, use a queue. If you need the most recent thing first, use a stack.
Common variations
- Priority queue — items come out by priority, not arrival order.
- Circular queue — reuses a fixed-size buffer efficiently.
- Deque (double-ended queue) — add or remove from both ends.
- Message queue — a system-level queue that decouples producers from consumers across services.
Common mistakes
- Confusing it with a stack. The single most common beginner mix-up. Queue = line; stack = pile.
- Reaching for the wrong tool. If order does not matter, a plain list may be simpler.
- Obsessing over implementation first. Understand FIFO behavior before worrying about arrays versus linked lists under the hood.
FAQ
What does FIFO mean?
First-in, first-out — the first item added to the queue is the first one removed, exactly like a fair waiting line.
What is the difference between a queue and a stack?
A queue is FIFO (first in, first out); a stack is LIFO (last in, first out). Use a queue for in-order processing and a stack for undo or backtracking.
Where are queues used in real software?
Task schedulers, print spoolers, message brokers, web request handling, and breadth-first graph search all rely on queues.
Is a message queue the same thing?
It is the same FIFO idea applied at the system level, letting one service hand work to another reliably and in order.
Where to go next
What is a data structure in 2026, what is a linked list in 2026, and what is a hash table in 2026.