Stacks
Announcements
Where we’re going
Stack ADT:
push(3)
push(8)
push(4)
pop()
pop()
push(6)
pop()
push(2)
pop()
pop()
Push 1, 2, 3 in order, with interleaved pops. What output is impossible?
Open PrairieLearn now: Class Activities → The Witch’s Potion Pantry
Click Open workspace — it takes a minute to start, so let it load while you watch the story.
Every one of you has your own pantry and your own recipe!
In potions.cpp, write part1 and part2 — using std::stack.
make example should print CMZ and MCD.
make input reveals your words!
| Defined | the Stack ADT: push, pop, empty. Last in, first out |
| Implemented | two ways: linked list and vector |
| Analyzed | push and pop are \(\Theta(1)\) for linked list. For the vector, doubling on resize keeps the functions \(\Theta(1)\) on average. |
| Applied | Potion Pantry: use a stack to reverse the pile! |
CPSC 221