tablenotfound.com
Data Structures

How Ctrl+Z works internally

Command Pattern + Two Stacks — interactive demo. The data structure is a Stack (LIFO — Last In, First Out).

Step 1 — do some actions to push plates onto the Undo Stack
Undo Stack
0
Do an action above
Top plate = most recent action
Redo Stack
0
Press Ctrl+Z to fill this
Ctrl+Y pops from here
Do an action above — watch the plate get pushed onto the Undo Stack.

The 3 flows — that's the entire system
Do action push() to Undo Stack Redo Stack gets wiped
Ctrl+Z pop() from Undo push() to Redo Stack
Ctrl+Y pop() from Redo push() back to Undo Stack
The only 3 operations Ctrl+Z needs
Push
Add on top. Happens every time you do an action.
Pop
Remove from top. Happens on every Ctrl+Z.
LIFO
Last In, First Out. Like a stack of plates.
In code:  undoStack.push(action) when you act  |  undoStack.pop() on Ctrl+Z  |  Implemented using an Array or LinkedList

The classic analogy — a real plate stack

This is how every CS textbook explains a Stack. You can only add or remove from the top. No touching the bottom or middle.

Stack of plates
Push here
Stack is empty
Bottom — cannot access directly
Add a plate
Remove a plate
State
Plates in stack 0
Top plate
Last operation
Push a plate to start. You can only ever touch the top.
LIFO rule: The plate you added last is the one you must remove first. Push A → Push B → Push C → Pop gives you C, not A.