← back to stream

Stacks

A stack is LIFO — last in, first out — with O(1) push and pop on one end. The mental hook: anywhere you need to "remember where I was before going deeper, and come back in reverse order." The call stack in every programming language works this way; that's why deep recursion blows it up. Other cases: undo/redo (push each action, pop to reverse), browser back button, expression evaluation (shunting yard for infix→postfix, then a stack-based evaluator), matching brackets/tags in parsers, DFS traversal (a stack instead of a queue turns BFS into DFS). Implementation is trivial — an array with push and pop at the end — which is why it's rarely its own data structure in practice, just a usage pattern on an array.