3. Implementing a real-time undo/redo system for a text editing application using a Stack data structure.
The system should support the following operations:Make a Change: A new change to the document is
made , Undo Action: Revert the most recent change and store it for potential redo ,Redo Action: Reapply
the most recently undone action ,Display Document State: Show the current state of the document after
undoing or redoing an action. i want to write python code .
class TextEditor:
def __init__(self):
[Link] = "" # Current state of the document
self.undo_stack = [] # Stores previous states (for undo)
self.redo_stack = [] # Stores undone states (for redo)
def make_change(self, new_text):
"""Make a new change to the document."""
self.undo_stack.append([Link]) # Save current state before change
[Link] = new_text
self.redo_stack.clear() # Once a new change is made, redo history is cleared
print("Change made successfully.")
def undo(self):
"""Undo the most recent change."""
if not self.undo_stack:
print("Nothing to undo.")
return
self.redo_stack.append([Link]) # Save current state for redo
[Link] = self.undo_stack.pop() # Revert to last saved state
print("Undo successful.")
def redo(self):
"""Redo the most recently undone change."""
if not self.redo_stack:
print("Nothing to redo.")
return
self.undo_stack.append([Link]) # Save current state for undo
[Link] = self.redo_stack.pop() # Reapply the undone state
print("Redo successful.")
def display(self):
"""Display the current state of the document."""
print(f"Current Document: '{[Link]}'")
# Example usage
if __name__ == "__main__":
editor = TextEditor()
editor.make_change("Hello")
[Link]()
editor.make_change("Hello World")
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
editor.make_change("Hello World!!!")
[Link]()
[Link]()
[Link]()