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 Lists→→Depth-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 Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Introduction to Classes → Linked Lists → Stacks

Longest path: 81 steps · 335 total prerequisite topics

Prerequisites (2)

Leads To (6)