← Course home

Linear structures · Lesson 14 of 52

Stack basics

Trace last-in, first-out operations and maintain a bounded stack's size invariant.

Full lessonAbout 9 min

You will learn to trace LIFO order and state bounded stack size invariants.

Recommended first: Arrays and Python sequence costs.

On this page 6 sections

The idea

Push 1, push 2, push 3. The first pop returns 3.

A stack is a last-in, first-out sequence. push adds an item at the top. pop removes that same item. The size invariant is 0 ≤ size ≤ limit for a bounded stack, and size changes by exactly 1 on every successful push or pop. The teaching Stack in this repository stores items in a Python list and rejects a push that would pass the limit. After push 1, push 2, push 3, the first pop returns 3 and the second returns 2. That order is the definition, not an accident of the example. A Python list is the same stack when you append to push and call pop with no argument.

A worked trace

StepStateWhat changed
Pushtop = 1Pushed 1. The top is 1.
Pushtop = 2Pushed 2. The top is 2.
Pushtop = 3Pushed 3. The top is 3.
Poptop = 2The first pop returns 3. The top is now 2.

Why it is correct

Each push writes one reference at the end of the list, which is the top. Each pop reads and removes that end. No operation reaches under the top, so the item pushed most recently is the item removed first. The size invariant holds by induction. It is 0 on an empty stack. A successful push adds 1 and a successful pop subtracts 1, and both refuse to step outside 0 and limit. When the stack is empty, pop has nothing to return. When it is full, push has no slot that stays within the limit.

A common mistake

Pop removes the latest push, not the earliest one.

What it costs

push and pop at the end of a list are amortized Θ(1). On this bounded stack they are Θ(1) because the list does not need to grow past the items you actually pushed within the limit. Extra memory is Θ(n) for n stored items. A stack does not offer Θ(1) access to the bottom. If you need the other end, use a queue.

Run it

python
from bitsandbytes.stacks.stack import Stackstack = Stack(limit=4)stack.push(1)stack.push(2)stack.push(3)print(stack.pop(), stack.pop())# prints: 3 2 

Practice and next step

Open Monotonic stack patterns next. A monotonic stack keeps indexes that are still waiting for a later value.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.stacks.stack

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Push 1, 2, 3 and pop twice

The right end is the top. The library Stack used here has a fixed limit.

top
1

State

  1. 01chosen

Pushed 1. The top is 1.

Practice with feedback

Check your understanding

Try each checkpoint before revealing the feedback. Ask for a hint when you need one. Answers and hints stay in this browser when local storage is available.

1Push 1, push 2, pop, push 3, pop, pop. What remains, then what was last?
2What does this code print?
print([1, 2].pop())

Progress is never sent to an account or synced to another device. If local storage is blocked, choices last only until you leave this page.