A Stack and Last-In, First-Out Order
A stack exposes the newest retained element. Push adds to the top, and pop removes that element.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace an undo stack
After inserting a heading and then changing its size, the first undo should address the size change. Pushing those actions in execution order makes it available first. Sequence endpoints for stacks show where a Python list can perform both operations.
Handle the empty case
Undoing twice can leave the stack empty. The next command should report that there is no work or produce a specified empty result. Use a clear sentinel when an action may itself carry an empty value.[1]
Follow the history through a pop and a new push
A text editor can store completed edit commands on a stack. After inserting a word and then correcting its spelling, undo needs to reverse the correction first. Popping gives the last command with its reversal data. See tracing a recursive call stack for another situation where suspended work resumes in reverse order.
Bottom → top
[] → [insert] → [insert, correct]
Undo returns correct and leaves [insert]
A new edit pushes replace → [insert, replace]
Check which histories should survive a new action
If redo is available, a new edit after undo generally starts a different future history. Decide what happens to the old redo stack. Test empty undo and two undo steps with a redo before accepting the model.