Data Structure
Queue
A First-In, First-Out (FIFO) structure — think of a checkout line. New items join at the back, and only the item at the front is ever served next.
EnqueueO(1)
DequeueO(1)*
PeekO(1)
OrderFIFO
Try It — Animated Flow
front
9
4
17
Peek returns 9 — the front of the line — without removing it.
Step 1 / 1
Pointer / current
Comparing
Swapping / removing
Newly inserted
Settled
* O(1) with a circular buffer or linked list. A naive array-shift implementation would be O(n).
Java Implementation
Real-Life Usage
Print job queues
Documents print in the order they were sent — the first job queued is the first one printed.
Customer service lines
Support tickets or call-center callers are served in arrival order — exactly FIFO.
Task / job scheduling
Background job workers pull tasks off a queue in the order they were submitted (e.g. message brokers like RabbitMQ or SQS).
Breadth-first search
BFS explores a graph level by level using a queue to track which node to visit next, in the order nodes were discovered.
Trade-offs
Reach for a queue when
- Fairness / arrival order matters (FIFO)
- You're doing a level-order or breadth-first traversal
- You're buffering work between a producer and consumer
Avoid it when
- You need last-in-first-out order (use a Stack)
- You need to access elements in the middle
- Priority — not arrival order — should decide who's next (use a Priority Queue)