using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Sockets;
using System.Reflection.Emit;
namespace UndoRedoFunctionality;
///****************************(Classes)********************************/
class Node
{
public string action;
public Node next;
public Node prev;
public Node(string action)
{
this.action = action;
next = null;
prev = null;
}
}
class UndoRedo
{
private Node curr;
public UndoRedo()
{
Console.WriteLine("# You can add actions and use undo/redo #");
curr = null;
}
public void addAction(string action)
{
Node newNode = new Node(action);
if (curr != null)
{
curr.next = null; // you can't undo the first action
newNode.prev = curr;
curr.next = newNode;
}
curr = newNode;
Console.Write("Action added: " + curr.action + " -> ");
display();
}
public void undo()
{
if (curr == null)
{
Console.WriteLine("No action to undo");
return;
}
Console.Write("Undo " + curr.action + " -> ");
curr = curr.prev;
display();
}
public void redo()
{
if (curr == null || curr.next == null)
{
Console.WriteLine("No action to redo");
return;
}
Console.Write("Redo " + curr.action + " -> ");
curr = curr.next;
display();
}
public void display()
{
if (curr == null)
{
Console.WriteLine("No actions to display");
return;
}
Node temp = curr;
while (temp.prev != null)
temp = temp.prev;
while (temp != curr)
{
Console.Write(temp.action + " ");
temp = temp.next;
}
Console.WriteLine(curr.action);
}
}
/********************************************************************/
internal class Program
{
///-----------------------(funcoins )--------------------------------/
//------------------------------------------------------------------------/
static void Main()
{
UndoRedo test = new UndoRedo();
test.addAction("A"); // Action added: A -> A
test.addAction("B"); // Action added: B -> A B
test.addAction("C"); // Action added: C -> A B C
test.addAction("D"); // Action added: D -> A B C D
test.display(); // A B C D
test.undo(); // Undo D -> A B C
test.undo(); // Undo C -> A B
test.redo(); // Redo C -> A B C
test.undo(); // Undo C -> A B
test.undo(); // Undo B -> A
test.undo(); // Undo A -> No actions to display
test.undo(); // No actions to undo
test.display(); // No actions to display
}
}