Stacks & Queues
The problem
Many processes need to track pending work in a specific order — undo the most recent action first, or process requests in the order they arrived — and need a structure that enforces that order automatically instead of relying on discipline.
Why now
Arrays and lists give general-purpose storage, but say nothing about *which end* you're allowed to touch. Stacks and queues are arrays/lists with a deliberately restricted interface — and that restriction is what gives them predictable, provable ordering guarantees.
Mental model
A stack is a pile of plates: you can only add or remove from the top (LIFO). A queue is a line at a store: you add at the back, remove from the front (FIFO). The restriction isn't a limitation — it's the entire point, because it's what makes 'undo' and 'process in arrival order' automatic instead of something you have to enforce by hand.
Requires
- Arrays & ListsProgramming Fundamentals
A stack or queue is an array or linked list with a restricted interface (push/pop or enqueue/dequeue); you need the underlying sequence structure before restricting access to it.
Unlocks
- Background Jobs & QueuesBackend Engineering
- Graph AlgorithmsComputer Science Fundamentals
- Memory, the Stack & the HeapComputer Science Fundamentals
- Message Queues & Event-Driven ArchitectureDistributed Systems
- The Event LoopJavaScript Runtime
- CPU SchedulingOperating Systems
- Interprocess CommunicationOperating Systems
Used in
- The call stack itself — every function call is a stack push, every return a pop
- Undo/redo functionality in editors
- Breadth-first search (queue) and depth-first search (stack) traversal
- Task queues and message queues in backend systems (RabbitMQ, SQS)
Projects
- Implement a stack and a queue from scratch using only an array, including full/empty edge cases
- Use a stack to check whether a string of brackets ((), [], {}) is balanced
Examples
- The browser back button is a stack of visited pages — most recent first
- A print queue processes jobs in the order they were submitted — first in, first out
Resources
Mastery checklist
- I can implement a stack and queue using only array operations
- I can identify which one (stack or queue) a given ordering problem calls for
- I can explain the call stack as a literal application of the stack data structure
- I can use a stack to solve a matching/balancing problem