🧮Stack (Data Structure)
Also known as Stack, LIFO, Last In, First Out
A pile where the last item in is the first one out. You push and pop.
Think of it like
Think of it like a pile of plates. You add to the top, and you take from the top.
Example
Lucy types three words, then presses undo twice. The stack of changes gives back the last word, then the one before it.
Why it matters
Undo, the back button and each function call all use a stack.
Definition
Example code
python
>>> s = []
>>> s.append(1)
>>> s.append(2)
>>> s.pop()
2Common mistake
Key takeaways
- 1Last in, first out. Like a pile of plates.
- 2Push and pop. Add and remove at the top.
- 3Everywhere. Undo, back button, function calls.