-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathundoredo.cpp
172 lines (155 loc) · 3.96 KB
/
undoredo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// #include <iostream>
// #include <stack>
// #include <string>
// using namespace std;
// // User-defined stack class
// template <typename T>
// class Stack
// {
// private:
// struct Node
// {
// T data;
// Node* next;
// Node(T value) : data(value), next(nullptr) {}
// };
// Node* topNode;
// public:
// Stack() : topNode(nullptr) {}
// ~Stack()
// {
// while (!isEmpty())
// {
// pop();
// }
// }
// void push(T value)
// {
// Node* newNode = new Node(value);
// newNode->next = topNode;
// topNode = newNode;
// }
// void pop()
// {
// if (isEmpty())
// {
// return;
// }
// Node* temp = topNode;
// topNode = topNode->next;
// delete temp;
// }
// T top() const
// {
// if (!isEmpty())
// {
// return topNode->data;
// }
// throw runtime_error("Stack is empty");
// }
// bool isEmpty() const
// {
// return topNode == nullptr;
// }
// };
// // Undo/Redo System
// class UndoRedo
// {
// private:
// Stack<string> undoStack;
// Stack<string> redoStack;
// public:
// void performAction(const string& action)
// {
// undoStack.push(action);
// while (!redoStack.isEmpty())
// {
// redoStack.pop(); // Clear the redo stack when a new action is performed
// }
// cout << "Performed action: " << action << endl;
// }
// void undo()
// {
// if (undoStack.isEmpty())
// {
// cout << "Nothing to undo!" << endl;
// return;
// }
// string lastAction = undoStack.top();
// undoStack.pop();
// redoStack.push(lastAction);
// cout << "Undone action: " << lastAction << endl;
// }
// void redo()
// {
// if (redoStack.isEmpty())
// {
// cout << "Nothing to redo!" << endl;
// return;
// }
// string lastUndone = redoStack.top();
// redoStack.pop();
// undoStack.push(lastUndone);
// cout << "Redone action: " << lastUndone << endl;
// }
// void printState() const
// {
// cout << "\nCurrent State:\n";
// cout << "Undo Stack: ";
// Stack<string> tempUndo = undoStack; // Copy to display contents
// while (!tempUndo.isEmpty())
// {
// cout << tempUndo.top() << " ";
// tempUndo.pop();
// }
// cout << "\nRedo Stack: ";
// Stack<string> tempRedo = redoStack; // Copy to display contents
// while (!tempRedo.isEmpty())
// {
// cout << tempRedo.top() << " ";
// tempRedo.pop();
// }
// cout << "\n";
// }
// };
// int main()
// {
// UndoRedo urSystem;
// int choice;
// string action;
// do
// {
// cout << "\n========== UNDO/REDO SYSTEM ==========\n";
// cout << "1. Perform Action\n";
// cout << "2. Undo\n";
// cout << "3. Redo\n";
// cout << "4. Show Current State\n";
// cout << "0. Exit\n";
// cout << "Enter your choice: ";
// cin >> choice;
// cin.ignore(); // To consume the newline character after input
// switch (choice)
// {
// case 1:
// cout << "Enter action: ";
// getline(cin, action);
// urSystem.performAction(action);
// break;
// case 2:
// urSystem.undo();
// break;
// case 3:
// urSystem.redo();
// break;
// case 4:
// urSystem.printState();
// break;
// case 0:
// cout << "Exiting...\n";
// break;
// default:
// cout << "Invalid choice! Try again.\n";
// }
// } while (choice != 0);
// return 0;
// }