0
Challenge · 35Structures You Build Yourself
Stack
Structuresused in ~95% of apps
A structure you already have. JavaScript has no Stack class and does not need one. An array with push and pop is a stack, and knowing when to reach for it is worth far more than knowing how to build it.
Core idea. Last in, first out. Only the most recent item is reachable, and that restriction is the feature: it is exactly the right shape for anything that nests, pairs up, or has to be undone in reverse order.
poke it, drag it,
try to break it
try to break it
no slides here.
you drive.
you drive.
Playground, this isn't a screenshot
matching "( [ ] )"
(
(
opener. Push
[
(
[
opener, push
]
(
closer, top is '[', matches, pop
)
empty
closer. Top is '(', matches, pop
,
empty
stack empty. Balanced
The stack grows upward. Only the top is ever examined, and the top is always the thing that opened most recently.
↑ play a little first, then dig in