1
+ // Node class to store each state of the text
2
+ class TextState {
3
+ String text ;
4
+ TextState next , prev ;
5
+
6
+ public TextState (String text ) {
7
+ this .text = text ;
8
+ this .next = this .prev = null ;
9
+ }
10
+ }
11
+
12
+
13
+ class TextEditorFunctionality {
14
+ private TextState head , tail , current ;
15
+ private int historyLimit = 10 ;
16
+ private int count = 0 ;
17
+
18
+ public TextEditorFunctionality () {
19
+ head = tail = current = null ;
20
+ }
21
+
22
+ // Add a new text state (when user types or performs an action)
23
+ public void addTextState (String newText ) {
24
+ TextState newState = new TextState (newText );
25
+
26
+ // If the list is empty, set as the first state
27
+ if (head == null ) {
28
+ head = tail = current = newState ;
29
+ } else {
30
+ // Remove forward history
31
+ current .next = null ;
32
+ tail = current ;
33
+
34
+ // Add the new state at the end
35
+ tail .next = newState ;
36
+ newState .prev = tail ;
37
+ tail = newState ;
38
+ current = newState ;
39
+
40
+ // If history exceeds the limit, remove the oldest state
41
+ if (count >= historyLimit ) {
42
+ head = head .next ;
43
+ head .prev = null ;
44
+ } else {
45
+ count ++;
46
+ }
47
+ }
48
+ }
49
+
50
+ // Undo: Move to the previous state
51
+ public void undo () {
52
+ if (current == null || current .prev == null ) {
53
+ System .out .println ("No more undo actions available." );
54
+ return ;
55
+ }
56
+ current = current .prev ;
57
+ System .out .println ("Undo performed. Current text: " + current .text );
58
+ }
59
+
60
+ // Redo: Move to the next state
61
+ public void redo () {
62
+ if (current == null || current .next == null ) {
63
+ System .out .println ("No more redo actions available." );
64
+ return ;
65
+ }
66
+ current = current .next ;
67
+ System .out .println ("Redo performed. Current text: " + current .text );
68
+ }
69
+
70
+ // Display current text state
71
+ public void displayCurrentText () {
72
+ if (current == null ) {
73
+ System .out .println ("No text available." );
74
+ } else {
75
+ System .out .println ("Current Text: " + current .text );
76
+ }
77
+ }
78
+
79
+ public void displayAllStates () {
80
+ TextState temp = head ;
81
+ if (temp == null ) {
82
+ System .out .println ("No history available." );
83
+ return ;
84
+ }
85
+ System .out .println ("Text History:" );
86
+ while (temp != null ) {
87
+ System .out .println ("- " + temp .text );
88
+ temp = temp .next ;
89
+ }
90
+ }
91
+
92
+ // Main method to test the Undo/Redo functionality
93
+ public static void main (String [] args ) {
94
+ TextEditorFunctionality editor = new TextEditorFunctionality ();
95
+
96
+ // Adding text states
97
+ editor .addTextState ("Hello" );
98
+ editor .addTextState ("Hello World" );
99
+ editor .addTextState ("Hello World! How are you?" );
100
+ editor .addTextState ("Hello World! How are you doing today?" );
101
+
102
+
103
+ editor .displayCurrentText ();
104
+
105
+
106
+ editor .undo ();
107
+ editor .undo ();
108
+
109
+ editor .redo ();
110
+
111
+ System .out .println ("After Undo and Redo" );
112
+ editor .displayCurrentText ();
113
+
114
+ // Display all states in the history
115
+ editor .displayAllStates ();
116
+ }
117
+ }
118
+
119
+ //SampleOutput
120
+ //Current Text: Hello World! How are you doing today?
121
+ //Undo performed. Current text: Hello World! How are you?
122
+ //Undo performed. Current text: Hello World
123
+ //Redo performed. Current text: Hello World! How are you?
124
+ //After Undo and Redo
125
+ //Current Text: Hello World! How are you?
126
+ //Text History:
127
+ //- Hello
128
+ //- Hello World
129
+ //- Hello World! How are you?
130
+ //- Hello World! How are you doing today?
0 commit comments