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 methods we will implement. These methods will manage a document's edit history, allowing us to apply changes, undo them, and redo them effectively.
ApplyChange(string change)
: This method applies a change to the document. The change, 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.Undo()
: This method undoes the most recent change and allows us to store it for a possible redo. It returns the change that was undone. If there are no changes available to undo, it returns null
.Redo()
: This method redoes the most recent undone change, making it active again. It returns the change that was redone. If there are no changes available to redo, it returns null
.GetChanges()
: This method returns an array of all applied changes in the order they were applied.To implement these methods efficiently, we'll use two stacks. We use stacks to keep track of applied changes and undone changes because of their LIFO (Last In, First Out) property. The most recent change is always at the top, making it easy to undo and redo.
Let's break down each method's implementation and study how they interact with the stacks.
Let's implement the solution step-by-step. First, we define our class and set up the initial state.
C#1using System; 2using System.Collections.Generic; 3 4public class DocumentHistory 5{ 6 private Stack<string> changesStack; 7 private Stack<string> redoStack; 8 9 public DocumentHistory() 10 { 11 changesStack = new Stack<string>(); 12 redoStack = new Stack<string>(); 13 } 14} 15 16public class Program 17{ 18 public static void Main(string[] args) 19 { 20 DocumentHistory documentHistory = new DocumentHistory(); 21 } 22}
Here, DocumentHistory
is the class, and we initialize a Stack<string>
named changesStack
to act as our stack of applied changes and another Stack<string>
named redoStack
to act as our stack for undone changes.
Next, we put into effect the method to apply changes.
C#1using System; 2using System.Collections.Generic; 3 4public class DocumentHistory 5{ 6 private Stack<string> changesStack; 7 private Stack<string> redoStack; 8 9 public DocumentHistory() 10 { 11 changesStack = new Stack<string>(); 12 redoStack = new Stack<string>(); 13 } 14 15 public void ApplyChange(string change) 16 { 17 changesStack.Push(change); 18 redoStack.Clear(); 19 } 20} 21 22public class Program 23{ 24 public static void Main(string[] args) 25 { 26 DocumentHistory documentHistory = new DocumentHistory(); 27 documentHistory.ApplyChange("Initial text"); 28 Console.WriteLine("Change applied."); 29 } 30}
With ApplyChange
, we take the string change
and push it to the changesStack
stack. Additionally, we clear the redoStack
to ensure that once a new change is applied, any previously undone changes cannot be redone.
Now, we will implement the method to undo the most recent change.
C#1using System; 2using System.Collections.Generic; 3 4public class DocumentHistory 5{ 6 private Stack<string> changesStack; 7 private Stack<string> redoStack; 8 9 public DocumentHistory() 10 { 11 changesStack = new Stack<string>(); 12 redoStack = new Stack<string>(); 13 } 14 15 public void ApplyChange(string change) 16 { 17 changesStack.Push(change); 18 redoStack.Clear(); 19 } 20 21 public string Undo() 22 { 23 if (changesStack.Count == 0) 24 { 25 return null!; 26 } 27 var change = changesStack.Pop(); 28 redoStack.Push(change); 29 return change; 30 } 31} 32 33public class Program 34{ 35 public static void Main(string[] args) 36 { 37 DocumentHistory documentHistory = new DocumentHistory(); 38 documentHistory.ApplyChange("Initial text"); 39 documentHistory.ApplyChange("Added more text"); 40 string undoneChange = documentHistory.Undo(); 41 Console.WriteLine(undoneChange != null ? $"Undone Change: {undoneChange}" : "No change to undo."); 42 } 43}
Here, Undo
checks if the changesStack
stack is empty. If it is, it returns null
. Otherwise, it pops the last item from the stack, simulating a LIFO operation, pushes it to the redoStack
, and returns the undone change.
Now we'll create the method to redo the most recent undone change.
C#1using System; 2using System.Collections.Generic; 3 4public class DocumentHistory 5{ 6 private Stack<string> changesStack; 7 private Stack<string> redoStack; 8 9 public DocumentHistory() 10 { 11 changesStack = new Stack<string>(); 12 redoStack = new Stack<string>(); 13 } 14 15 public void ApplyChange(string change) 16 { 17 changesStack.Push(change); 18 redoStack.Clear(); 19 } 20 21 public string Undo() 22 { 23 if (changesStack.Count == 0) 24 { 25 return null!; 26 } 27 var change = changesStack.Pop(); 28 redoStack.Push(change); 29 return change; 30 } 31 32 public string Redo() 33 { 34 if (redoStack.Count == 0) 35 { 36 return null!; 37 } 38 var change = redoStack.Pop(); 39 changesStack.Push(change); 40 return change; 41 } 42} 43 44public class Program 45{ 46 public static void Main(string[] args) 47 { 48 DocumentHistory documentHistory = new DocumentHistory(); 49 documentHistory.ApplyChange("Initial text"); 50 documentHistory.ApplyChange("Added more text"); 51 string undoneChange = documentHistory.Undo(); 52 Console.WriteLine(undoneChange != null ? $"Undone Change: {undoneChange}" : "No change to undo."); 53 54 string redoneChange = documentHistory.Redo(); 55 Console.WriteLine(redoneChange != null ? $"Redone Change: {redoneChange}" : "No change to redo."); 56 } 57}
The Redo
method checks if the redoStack
is empty. If it is, it returns null
. Otherwise, it pops the last item from the stack, simulating a LIFO operation, pushes it back to the changesStack
, and returns the redone change.
Lastly, we implement the method to retrieve all applied changes.
C#1using System; 2using System.Collections.Generic; 3 4public class DocumentHistory 5{ 6 private Stack<string> changesStack; 7 private Stack<string> redoStack; 8 9 public DocumentHistory() 10 { 11 changesStack = new Stack<string>(); 12 redoStack = new Stack<string>(); 13 } 14 15 public void ApplyChange(string change) 16 { 17 changesStack.Push(change); 18 redoStack.Clear(); 19 } 20 21 public string Undo() 22 { 23 if (changesStack.Count == 0) 24 { 25 return null!; 26 } 27 var change = changesStack.Pop(); 28 redoStack.Push(change); 29 return change; 30 } 31 32 public string Redo() 33 { 34 if (redoStack.Count == 0) 35 { 36 return null!; 37 } 38 var change = redoStack.Pop(); 39 changesStack.Push(change); 40 return change; 41 } 42 43 public string[] GetChanges() 44 { 45 return changesStack.ToArray(); 46 } 47} 48 49public class Program 50{ 51 public static void Main(string[] args) 52 { 53 DocumentHistory documentHistory = new DocumentHistory(); 54 documentHistory.ApplyChange("Initial text"); 55 documentHistory.ApplyChange("Added more text"); 56 57 documentHistory.Undo(); 58 documentHistory.Redo(); 59 60 string[] changes = documentHistory.GetChanges(); 61 Console.WriteLine($"Changes: {string.Join(", ", changes)}"); 62 } 63}
The GetChanges
method simply returns an array of all elements in the changesStack
, which includes all changes applied to the document in the order they were applied.
In today's lesson, we learned how to manage a document's editing history using stacks. We implemented methods to apply changes, undo the last change, redo the most recent undone change, and retrieve all applied changes. This lesson provided us with practical experience in using stacks
to efficiently track and revert operations in a real-life scenario. Keep practicing similar challenges to deepen your understanding of data structures in various applications. Fantastic job today, and keep up the good work!