// Online C# Editor for free
// Write, Edit and Run your C# code using C# Online Compiler
using System;
using System.Collections.Generic;
public class UndoRedoSimulator
{
private Stack<string> undoStack = new Stack<string>();
private Stack<string> redoStack = new Stack<string>();
public void DoAction(string action)
{
undoStack.Push(action);
redoStack.Clear();
Console.WriteLine($"Action performed: {action}");
}
public void Undo()
{
if(undoStack.Count == 0)
{
Console.WriteLine("Nothing to undo.");
return;
}
string action = undoStack.Pop();
redoStack.Push(action);
Console.WriteLine($"Undo: {action}");
}
public void Redo()
{
if(redoStack.Count == 0)
{
Console.WriteLine("Nothing to redo.");
return;
}
string action = redoStack.Pop();
undoStack.Push(action);
Console.WriteLine($"Redo: {action}");
}
// Print current state
public void PrintState()
{
Console.WriteLine("\n--- Current State ---");
Console.WriteLine("Undo Stack: " + string.Join(", ", undoStack));
Console.WriteLine("Redo Stack: " + string.Join(", ", redoStack));
Console.WriteLine("---------------------\n");
}
}
public class HelloWorld
{
public static void Main(string[] args)
{
UndoRedoSimulator editor = new UndoRedoSimulator();
editor.DoAction("Type 'Hello'");
editor.DoAction("Type 'World'");
editor.DoAction("Delete 'World'");
editor.PrintState();
editor.Undo();
editor.Undo();
editor.PrintState();
editor.Redo();
editor.PrintState();
editor.DoAction("Type '!'");
editor.PrintState();
}
}