Lesson 11: Stacks & queues
Let's start in the kitchen: picture a stack of plates — you place on top and take from the top, so the last one in is the first out. That's a 'stack', LIFO. Now picture a checkout line: first to arrive, first served — that's a 'queue', FIFO. In Python a stack is a list with append/pop, and a queue i
A stack is like a pile of plates: you place on top and take from the top — the last one placed comes off first. A queue is like a line at a checkout: whoever arrived first is served first. That's the whole difference — which end you take from.
- stack
- A LIFO structure — last in, first out. In Python: a list with append and pop at the end, both O(1).
- queue
- A FIFO structure — first in, first out. In Python: collections.deque with append and popleft, both O(1).
- deque
- A double-ended queue from collections; allows O(1) add and remove at both ends, making it the efficient FIFO queue.