A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Stacks

College Depth 80 in the knowledge graph I know this Set as goal
452topics build on this
335prerequisites beneath it
See this on the map →
Arrays and ListsLinked ListsDepth-First Search (DFS)LL Parsing and Predictive Parsing+4 more
stack LIFO data-structures push-pop

Core Idea

A stack is a last-in, first-out (LIFO) data structure that supports two core operations: push (add to top) and pop (remove from top), both in O(1) time. Stacks naturally model function call frames, undo/redo history, and expression parsing. They can be implemented using a dynamic array (with a top pointer) or a linked list (with insertions at the head). A peek operation returns the top element without removing it.

How It's Best Learned

Implement a stack from both an array and a linked list. Then solve classic stack problems: balanced parentheses checking, postfix expression evaluation, and the next-greater-element problem.

Common Misconceptions

Explainer

You already know arrays and linked lists as linear collections where you can access or insert elements at various positions. A stack restricts this flexibility on purpose: you can only add and remove elements from one end, called the top. This last-in, first-out (LIFO) constraint is not a limitation — it is the entire point. By restricting access, a stack gives you a data structure that naturally tracks "what was I doing most recently?" and provides O(1) push, pop, and peek operations with zero bookkeeping overhead.

The simplest way to implement a stack is with a dynamic array and a top index. You maintain a variable `top` that tracks the index of the most recent element. Push increments `top` and writes the new element; pop reads the element at `top` and decrements it. If you reach the array's capacity, you resize (typically doubling), which gives amortized O(1) push. Alternatively, you can implement a stack using a linked list where each push prepends a new node at the head and each pop removes the head node. The linked-list version never needs resizing but uses extra memory for pointers and has worse cache locality. In practice, the array-based approach is faster for most use cases.

The power of stacks becomes clear when you see them in action. Consider balanced parentheses checking: scan a string left to right, push every opening bracket onto the stack, and when you encounter a closing bracket, pop and verify it matches. If the stack is empty when you try to pop, or non-empty when the string ends, the brackets are unbalanced. The stack naturally handles nesting of any depth because the most recently opened bracket is always on top — exactly the one that the next closing bracket should match. Another classic application is postfix (reverse Polish) expression evaluation: given an expression like `3 4 + 2 *`, push operands onto the stack; when you encounter an operator, pop two operands, apply the operator, and push the result. The stack manages operator precedence implicitly without needing parentheses.

The most pervasive stack in computing is one you use every time you call a function: the call stack. Each function call pushes a new frame containing local variables and the return address. When the function returns, its frame is popped, restoring the caller's state. This is why recursion works — each recursive call gets its own frame on the stack, and they unwind in reverse order. It is also why deep recursion can cause a stack overflow: the call stack has a fixed size (typically 1-8 MB), and exceeding it crashes the program. Understanding stacks gives you direct insight into how recursion, undo/redo systems, browser back-buttons, and depth-first search all work — each is fundamentally tracking a history of decisions that must be reversed in LIFO order.

Practice Questions 5 questions

Prerequisite Chain

Understanding ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesBoolean AlgebraBoolean Type and Truth ValuesComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraConditional StatementsDefining and Calling FunctionsFunctions: Decomposing ProblemsFunction Parameters and Argument PassingReturn ValuesVariable ScopeIntroduction to ClassesLinked ListsStacks

Longest path: 81 steps · 335 total prerequisite topics

Prerequisites (2)

Leads To (6)