Stacks: Last In, First Out and Undo
Undo the most recent report edit first
A teacher changes a student name, then changes a mark. Undo should reverse the mark change before the earlier name change. The most recent action needs to be accessible first.
A stack stores and removes items at one end, called the top. Its rule is last in, first out, or LIFO. An array can implement a small stack with push and pop.
Trace order
Predict which stored action is removed first.
Handle empty state
Avoid pretending an empty stack has a meaningful action.
Define undo data
Distinguish removing an action label from reversing an actual change.
The latest action is the top of the stack
First edit
Store the earlier name correction.
Name editSecond edit
The later mark correction becomes the top.
Mark editPop
Remove the newest action first.
Mark edit → Name edit
A stack is an access rule, not just an array name
push adds an item to the top; pop removes and returns the top. Peeking reads the top without removing it. Restricting access to one end creates the LIFO behavior.
A stack suits undo history, pending nested work and expression processing. A queue has a different rule: it removes the oldest arrival first. Choose by the behavior, not by the container’s implementation.
Before starting, review array append and removal. These examples use JavaScript; other languages can use different syntax and rules.
Read the report action order
The mark edit was pushed last, so it is popped first. The next pop returns the earlier name edit. The stack is then empty.
These strings model action order only. Popping a label does not reverse the student’s actual data. A real undo entry needs enough information to restore the previous state.
JavaScript · local teaching example
const edits = [];
edits.push('Name edit');
edits.push('Mark edit');
console.log(edits[edits.length - 1]);
console.log(edits.pop());
console.log(edits.pop());
console.log(edits.length);Expected output
Mark edit
Mark edit
Name edit
0Store the old value to reverse a simple change
This small numeric example saves the old mark before changing it. Popping the saved value restores 35.
It demonstrates one local undo action. Multiple fields, simultaneous edits and persistence require a fuller action contract.
JavaScript · local teaching example
let science = 35;
const previousMarks = [];
previousMarks.push(science);
science = 40;
science = previousMarks.pop();
console.log(science);Expected output
35Apply the rule to a small change
Push edits A, B and C, then undo two actions. Print the removed actions and the remaining top.
Worked answer
C and B are removed, leaving A at the top. The stack preserves the reverse order of the actions.
JavaScript · local teaching example
const edits = ['A', 'B', 'C'];
console.log(edits.pop());
console.log(edits.pop());
console.log(edits[edits.length - 1]);Expected output
C
B
ARecognize mistakes and limits
JavaScript pop on an empty array returns undefined. Check availability before using a popped value to restore data, otherwise a failed undo could overwrite a valid mark.
An undo stack must define what is reversible and preserve old state correctly. A shallow saved object can still change through shared references. Bound history size for long-lived applications and do not promise crash recovery from an in-memory stack.
For the language or tool rules used here, see JavaScript indexed collections.
Check the core idea
JavaScript · code for this question
const edits = [];
edits.push('Name edit');
edits.push('Mark edit');
console.log(edits[edits.length - 1]);
console.log(edits.pop());
console.log(edits.pop());
console.log(edits.length);Apply the idea to a new situation
Reverse the newest pending action
Stacks provide LIFO order. Peek reads the top, push adds and pop removes. Handle empty states and store enough data for the operation the popped entry represents.
Connect this idea with arrival-first order and pending recursive calls.
Reaching the recap and completing the quizzes are separate milestones.