Welcome! Today, we will delve into managing a document's editing history using stacks. Imagine building a text editor; you would need to handle actions like adding text, undoing, and redoing those changes. We will see how these features can be efficiently implemented using stacks. By the end of this lesson, you will possess an in-depth understanding of applying stacks in practical scenarios.
Before starting the coding portion, let's dissect the functions we will implement. These functions will manage a document's edit history, allowing us to apply changes, undo them, and redo them effectively.
-
func (d *DocumentHistory) ApplyChange(change string)
: This function applies a change to the document. Thechange
, represented as a string, is stored in a way that allows us to remember the order of applied changes. Any previously undone changes are discarded. -
func (d *DocumentHistory) Undo() (string, bool)
: This function undoes the most recent change and allows us to store it for a possible redo. It returns the change that was undone and a boolean indicating success. If there are no changes available to undo, it returns an empty string andfalse
. -
func (d *DocumentHistory) Redo() (string, bool)
: This function redoes the most recent undone change, making it active again. It returns the change that was redone and a boolean indicating success. If there are no changes available to redo, it returns an empty string andfalse
.
