← capítulo

Stacks

Last in, first out. You only touch the top. The restriction is the feature.

Three operations, all O(1)

Watch it match brackets

Push opens, pop on close, balanced iff the stack ends empty.

Scratch vs library

All O(1) per op. Our Stack ≈1.7× the list — it just wraps list.append.

Where you meet it

Takeaway

LIFO in one structure. Cheap, and exactly the ordering that "newest first, then unwind" work needs. Next: the mirror image — queues (FIFO).