The Stack ADT Definition
What you'll learn: You'll understand what a stack is, how it behaves, and what operations define it as an abstract data type.
What is a Stack?
A stack is an abstract data type that stores a collection of elements and follows a Last-In-First-Out (LIFO) principle. Think of it like a stack of plates in a cafeteria: you can only add a new plate to the top, and you can only take a plate from the top. The last plate you put on is the first one you'll remove.
This access pattern is what makes a stack unique—unlike an array or linked list where you can access any element, a stack restricts access to only one end.
Core Stack Operations
A stack ADT defines these essential operations:
- push: Add an element to the top of the stack
- pop: Remove and return the element from the top of the stack
- peek (or top): View the top element without removing it
- isEmpty: Check if the stack contains any elements
- size: Return the number of elements currently in the stack
These operations maintain the LIFO behavior. You cannot access elements in the middle of the stack without first removing everything above them.
Why LIFO Matters
The Last-In-First-Out property makes stacks perfect for scenarios where you need to reverse order or track nested relationships—like function calls, undo mechanisms, or matching parentheses in expressions.
Because it's an ADT, the stack definition focuses on what operations exist and how they behave, not how they're implemented. You could build a stack using an array or a linked list underneath.
Key Takeaway: A stack is a LIFO collection with restricted access—you can only add (push) and remove (pop) elements from the top, making it ideal for reversing order and tracking nested operations.