Beginner Topic 6 of 52 Data Structures
Stacks
Last-in first-out collection with push and pop.
How it works
A stack allows insertion and removal only at one end, the top, following last-in first-out order. Push adds an item, pop removes the most recent one and peek reads it, all in O(1) time. It can be backed by an array or a linked list, and the call stack of a program is itself a stack of function frames.
Where it is used
Undo and redo, expression evaluation and parentheses matching, depth-first search, backtracking, browser back buttons and converting recursion into iteration use stacks. Compilers use them for parsing.
Common pitfall
Popping from an empty stack causes an underflow error, and pushing onto a full fixed-size stack causes an overflow. Forgetting that the order is reversed leads to bugs when reading results. Guard operations with emptiness checks and pick a growable backing store.
Where it fits in the course
Stacks is topic 6 of 52 in Data Structures, rated Beginner. It follows Circular Linked Lists. The next topic is Queues.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Data Structures quiz.
- Explore the Stack, Queue & Linked List visualizer: Push, pop, enqueue and dequeue interactively.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Data Structures quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Stacks?
Where do people use Stacks?
What is the most common mistake with Stacks?
What should I learn before this topic?
How can I practice it?
Is Stacks hard to learn?
How long does it take to learn Stacks?
Which topics are related to Stacks?
What should I study after Stacks?
Related topics
- Circular Linked Lists (Data Structures)
- Queues (Data Structures)
- Doubly Linked Lists (Data Structures)
- Deque (Data Structures)