4Stack
What is a stack?
A stack is a structure where elements are added and removed at one end only. There is a single rule: the last one in is the first one out. A rule this simple is useful in many places, from function calls to checking parentheses. This lesson covers what a stack is, which operations it offers and how it is built.
- The LIFO order and how to spot it in everyday examples
- The five stack operations and what each one returns
- How a stack is built with an array or a linked list, and where it is used
1Last in, first out
Think of a pile of plates. You put a new plate on top, and when you need one you take the top plate. To reach the bottom plate you have to lift every plate above it. A stack works exactly like that. This order is called LIFO: Last In, First Out.
You use the same order on a computer every day:
- The browser's back button: each new page goes onto a stack. When you press back you return to the page you opened last.
- Undo: a text editor puts every change onto a stack. Ctrl+Z undoes your latest change, then the one before it.
- Function calls: when one function calls another, the inner one finishes first, then the outer one carries on.
The end where elements go in and come out is called the top. You cannot reach an element in the middle or at the bottom directly; only the top one is visible.
2A stack is an abstract data type
A stack is defined by the operations it offers, not by how it is coded. Such a definition is called an abstract data type (ADT). Code that uses a stack knows only the operations below; it does not know whether an array or a linked list sits inside.
| Operation | What it does | What it returns |
|---|---|---|
| push(x) | Puts x on top | Nothing (in C, success: true or false) |
| pop() | Removes the top element | The removed element |
| peek() | Looks at the top element without removing it | The top element |
| isEmpty() | Is the stack empty? | true or false |
| isFull() | Is the stack full? (fixed-size stacks only) | true or false |
All of these take O(1) time: however many elements the stack holds, only the top is touched. Some books call peek top.
3Let's trace a sequence of operations
Start with an empty stack and run these operations in order. In the stack column the elements are written from bottom to top; the rightmost one is the top.
| Operation | Stack (bottom → top) | Returned value |
|---|---|---|
| push(3) | 3 | |
| push(8) | 3 8 | |
| push(5) | 3 8 5 | |
| pop() | 3 8 | 5 |
| peek() | 3 8 | 8 |
| push(1) | 3 8 1 | |
| pop() | 3 8 | 1 |
| pop() | 3 | 8 |
| isEmpty() | 3 | false |
Look at the values the pops returned: 5, 1, 8. Each time, the element added most recently came out. Peek did not change the stack.
4Overflow and underflow
Two operations cannot always be done. Pushing onto a full stack is a stack overflow; it happens with a fixed-size array. Popping or peeking an empty stack is a stack underflow. That is why isFull is checked before push, and isEmpty before pop and peek.
You also see the "stack overflow" error when a recursive function goes too deep. What overflows there is the program's call stack.
5Two ways to build a stack
With an array: the elements sit in an array and top is the index of the top element. An empty stack has top = -1. It is fast and simple, but the capacity is fixed up front. Details in Creating an array stack.
With a linked list: each element is a node and top points to the first node. Push inserts at the head and pop deletes from the head. There is no size limit, but each node carries an extra pointer. Details in Creating a linked-list stack.
Both versions offer the same operations under the same names. Code that uses the stack cannot tell which one was chosen. When to pick which is covered in Array or linked list?.
6Where are stacks used?
- The call stack: the program puts each function call on the call stack and removes it when the function returns. This is what makes recursion work.
- Checking parentheses: every opening bracket goes onto a stack, and each closing bracket must match the one on top. See Balanced parentheses.
- Evaluating expressions: an infix expression is converted to postfix with a stack, and the postfix expression is evaluated with another stack. See infix to postfix and postfix evaluation.
- Undo and the back button: the last action is the first one undone.
- Depth-first search (DFS): while moving through a graph or a maze, the points to come back to are kept on a stack.
7What this module covers
- Stack with an array: creating, push, pop and peek.
- Stack with a linked list: the same four operations and a comparison of the two versions.
- Applications: balanced parentheses, converting infix to postfix and evaluating postfix.
8Try it
On an empty stack you run push(4), push(7), pop(), push(2). What does peek() return now?
The answer is C. Pop removes 7, leaving only 4. Then 2 is pushed and sits on top. Peek returns the top element, 2.
Show the answer
The answer is C. Pop removes 7, leaving only 4. Then 2 is pushed and sits on top. Peek returns the top element, 2.
An array stack with capacity 5 holds 5 elements. What happens if you call push(9)?
The answer is B. The fixed-size array has no room left. Push checks isFull first and reports an overflow. For a stack that grows by itself you need a linked list or a dynamic array.
Show the answer
The answer is B. The fixed-size array has no room left. Push checks isFull first and reports an overflow. For a stack that grows by itself you need a linked list or a dynamic array.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.