4.1.3.5 Undo Mechanism
Computational Structures
Stacks are used to implement undo functionality in applications such as Notepad and Microsoft Word. Each action you perform is saved on a stack. When you want to undo an action, the most recent action is removed from the stack and reversed. This allows you to go back to the previous state.
Example
Imagine you are using a text editor and you make several changes to a document, like typing new text or deleting old text. Here's how the undo mechanism works:
- Performing Actions: As you make changes, each action (such as typing or deleting text) is saved on a stack. For example, if you type “Hello”, this action is pushed onto the stack.
- Undoing an Action: When you press the undo button, the most recent action (like typing “Hello”) is removed from the stack. The text editor then reverses this action (e.g., it deletes “Hello” from the document).
- Multiple Undos: If you continue to press undo, each previous action is reversed in the order they were performed. If you typed “World” after “Hello”, pressing undo would first remove “World”, then “Hello”.