fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Net.Sockets;
  5. using System.Reflection.Emit;
  6.  
  7.  
  8. namespace UndoRedoFunctionality;
  9.  
  10. ///****************************(Classes)********************************/
  11. class Node
  12. {
  13. public string action;
  14. public Node next;
  15. public Node prev;
  16.  
  17. public Node(string action)
  18. {
  19. this.action = action;
  20. next = null;
  21. prev = null;
  22. }
  23. }
  24.  
  25. class UndoRedo
  26. {
  27. private Node curr;
  28.  
  29. public UndoRedo()
  30. {
  31. Console.WriteLine("# You can add actions and use undo/redo #");
  32. curr = null;
  33. }
  34.  
  35. public void addAction(string action)
  36. {
  37. Node newNode = new Node(action);
  38.  
  39. if (curr != null)
  40. {
  41. curr.next = null; // you can't undo the first action
  42.  
  43. newNode.prev = curr;
  44. curr.next = newNode;
  45. }
  46. curr = newNode;
  47. Console.Write("Action added: " + curr.action + " -> ");
  48. display();
  49. }
  50.  
  51. public void undo()
  52. {
  53. if (curr == null)
  54. {
  55. Console.WriteLine("No action to undo");
  56. return;
  57. }
  58. Console.Write("Undo " + curr.action + " -> ");
  59. curr = curr.prev;
  60. display();
  61.  
  62. }
  63.  
  64. public void redo()
  65. {
  66. if (curr == null || curr.next == null)
  67. {
  68. Console.WriteLine("No action to redo");
  69. return;
  70. }
  71. Console.Write("Redo " + curr.action + " -> ");
  72. curr = curr.next;
  73. display();
  74.  
  75. }
  76. public void display()
  77. {
  78. if (curr == null)
  79. {
  80. Console.WriteLine("No actions to display");
  81. return;
  82. }
  83.  
  84. Node temp = curr;
  85. while (temp.prev != null)
  86. temp = temp.prev;
  87.  
  88. while (temp != curr)
  89. {
  90. Console.Write(temp.action + " ");
  91. temp = temp.next;
  92. }
  93.  
  94. Console.WriteLine(curr.action);
  95. }
  96. }
  97.  
  98. /********************************************************************/
  99. internal class Program
  100. {
  101.  
  102. ///-----------------------(funcoins )--------------------------------/
  103.  
  104. //------------------------------------------------------------------------/
  105.  
  106. static void Main()
  107. {
  108. UndoRedo test = new UndoRedo();
  109. test.addAction("A"); // Action added: A -> A
  110. test.addAction("B"); // Action added: B -> A B
  111. test.addAction("C"); // Action added: C -> A B C
  112. test.addAction("D"); // Action added: D -> A B C D
  113. test.display(); // A B C D
  114. test.undo(); // Undo D -> A B C
  115. test.undo(); // Undo C -> A B
  116. test.redo(); // Redo C -> A B C
  117. test.undo(); // Undo C -> A B
  118. test.undo(); // Undo B -> A
  119. test.undo(); // Undo A -> No actions to display
  120. test.undo(); // No actions to undo
  121. test.display(); // No actions to display
  122. }
  123.  
  124. }
Success #stdin #stdout 0.07s 28708KB
stdin
Standard input is empty
stdout
#  You can add actions and use undo/redo  #
Action added: A -> A
Action added: B -> A   B
Action added: C -> A   B   C
Action added: D -> A   B   C   D
A   B   C   D
Undo D -> A   B   C
Undo C -> A   B
Redo B -> A   B   C
Undo C -> A   B
Undo B -> A
Undo A -> No actions to display
No action to undo
No actions to display