Stacks, Queues & Beyond
Some structures aren't about storage but about *order of access* โ stacks (last-in-first-out) and queues (first-in-first-out) model how work gets done, and show up everywhere.
Not every structure is about how you store data - some are about the order in which you take it out. Two classics: the stack (last-in, first-out, like a pile of plates) and the queue (first-in, first-out, like a line of people). They sound simple, but this ordering idea underlies undo systems, task scheduling, browser history, and much more.
Stacks and queues: order of access
The difference is which item you take next: a stack gives you the most recently added; a queue gives you the earliest added.
| Structure | Rule | Everyday example |
|---|---|---|
| Stack (LIFO) | Last in, first out | Undo, browser back, a pile of plates |
| Queue (FIFO) | First in, first out | A line, a print queue, task scheduling |
| Deque | Add/remove at both ends | A history you cap at both ends |
These aren't storage tricks - they're access disciplines. When you need 'most recent first', that's a stack; when you need 'fair, in order', that's a queue. Recognising the pattern is the skill.
They're everywhere
Once you know stacks and queues, you start seeing them in real systems - because they match how work naturally flows.
Use the right access order
- Stack (LIFO) - most recent first: undo, back-navigation, reversing.
- Queue (FIFO) - earliest first: fair processing, scheduling, buffering.
- Deque - efficient add/remove at both ends (great as a capped history).
- Recognise the pattern - which end do you take from? - to pick the structure.
๐ฏ Key takeaways
- Stacks and queues are about the order you take items out, not how you store them.
- Stack = last-in-first-out (undo, back button, call stack); Queue = first-in-first-out (a line, scheduling).
- A deque adds/removes efficiently at both ends - great for capped histories and buffers.
- Recognising LIFO vs FIFO in a problem often clarifies the whole solution.
Frequently asked
How do I know whether a problem needs a stack or a queue?
Ask which item you need to handle next: the most recently added, or the earliest added. If you want the most recent first - undoing the last action, backtracking, reversing something, processing nested structure - that's last-in-first-out, a stack. If you want to handle things in the order they arrived, fairly - a waiting line, tasks to process in submission order, buffering a stream - that's first-in-first-out, a queue. A quick test: 'undo' and 'go back' are stacks; 'wait your turn' and 'process in order' are queues. Once you spot the LIFO-vs-FIFO nature of the problem, the right structure - and often the whole approach - becomes clear.
Can't I just use a list for stacks and queues?
For a stack, yes - a Python list works perfectly: append() to push and pop() to remove from the end are both fast, so a list IS a good stack. For a queue, a plain list is a poor choice, because removing from the front (to get the first-in item) requires shifting every other element, which is slow. That's exactly what a deque (double-ended queue) is for - it adds and removes efficiently at both ends, making it the right tool for queues. So: a list is a fine stack, but use a deque for a queue. The concepts (LIFO/FIFO) are what matter; the implementation is just choosing a structure whose fast operations match the access pattern you need.
Related lessons
Dictionaries & Sets: Instant Lookup
Dictionaries and sets are the secret weapons of fast code. Thanks to hashing, they find thโฆ
Data Structures & AlgorithmsTrees & Graphs: Modelling Relationships
Some data isn't a flat list โ it's connected. Trees (hierarchies) and graphs (networks) moโฆ
Data Structures & AlgorithmsAlgorithms & Big-O Made Simple
An algorithm is just a recipe for solving a problem โ and Big-O is a simple way to talk abโฆ