Stack
Also called: LIFO
A collection where the last element added is the first one removed (last in, first out).
Definition
A stack supports push (add on top), pop (remove from the top) and peek, all in O(1). It is the natural structure for undo, the back button, matching brackets and depth-first traversal. Function calls also use a stack, the call stack, which has a fixed size and can overflow with very deep recursion. Swift uses an Array as a stack; Kotlin and Java use ArrayDeque.
In practice
Checking brackets: push every opening bracket, and pop when a closing one arrives to check it matches.
Learn it in context
Lessons that use this term.