Programming Fundamentals · Linear Structures: You Use Them Every Day

Stacks: The Secret Behind Cmd+Z and Stack Overflow

The second way of organizing looks like a stack of plates: you can only put on from the top, and only take from the top — that's a stack. Sounds very limiting? But the undo key you hit every day, and every function call in a program, both rely on it. This lesson starts by letting you wreck a document and undo it in one tap, then watch a “stack overflow” accident — the world's biggest programmer Q&A site, Stack Overflow, got its name from that accident.

Play first · the undo key's real form

On the left is a mini document — mess with it using the four buttons. Watch the right: every step you take is pushed as a card onto the “operation stack” — later steps sit on top. When you've had enough, mash “⌘Z Undo” a few times and see the order it undoes.

📄 Mini document

Mess around — don't hold back

🥞 Operation stack

Each step pushes a card; top = most recent

(no operations yet)
Try: type two lines → mark red → undo three times, and guess the restore order
See it? Undo always undoes the most recent step first. That isn't a product manager's choice — it's the nature of a stack: last in, first out (LIFO, Last In First Out). You only take from the top, so the first thing you get is whatever was put on last. If undo didn't follow that order — if it undid something from ten steps ago first — the document would instantly turn into mush. Anything that “backs out the way it came in” is born to be organized with a stack.
How does a program remember “where to return”?

You ask AI to “make dinner.” Halfway through it needs to chop veggies; halfway through chopping the knife is dull and it needs to sharpen… Every time it “pauses what's in hand to do something else,” the program pushes the current progress onto a stack. Watch the animation below (it auto-plays once when you scroll here), and notice how, after each task finishes, the program automatically finds its way back to the previous one.

ReadyHit “Play” or scroll here to watch a full round of calls and returns
This stack is the function call stack; each card is a “stack frame,” holding “how far this task got, and where to return when done.” The program needs no global dispatcher — pop the top, and you're naturally back at the paused spot one level up. The Agents you've learned work the same way: halfway through a main task it needs to look something up first, so it pushes the main task, runs the subtask, pops when done, and continues the main task.
Stack overflow live · how Stack Overflow happens

A stack has limited capacity. The function below calls itself (that's recursion). Code on the left, call stack on the right. First run without checking the base case and see what happens; then check it and run again to compare.

function 倒数(n) { if (n === 0) return; // 终止条件(保险丝) 打印(n); 倒数(n - 1); // 自己调用自己 } 倒数(5);
💥 Stack OverflowStack overflow — the program crashes
Run it as-is first — see what recursion without a fuse looks like
What does this have to do with AI? A lot. Agents also “push the main task, run the subtask first.” What if the subtask spawns more subtasks, layer after layer with no end? Same as recursion with no base case — so every serious Agent framework sets a max steps / max depth and stops hard when it's exceeded. That fuse is insurance against “infinite recursion”. Next time you see recursive code AI wrote, check-list item one: where's the base case?

✅ What this lesson wants to share