-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToDoList.h
280 lines (241 loc) · 7.49 KB
/
ToDoList.h
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#pragma once
#ifndef TODOLIST_H
#define TODOLIST_H
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
class Task
{
public:
string description;
int priority;
Task(string data = "", int pri = 1)
{
description = data;
priority = pri;
}
};
class Node
{
public:
Task task;
Node* left;
Node* right;
Node(Task t)
{
task = t;
left = right = nullptr;
}
};
class ToDoList
{
private:
Node* root;
Node* insert(Node* node, Task task)
{
if (node == nullptr)
{
return new Node(task);
}
if (task.priority > node->task.priority)
{
node->left = insert(node->left, task);
}
else
{
node->right = insert(node->right, task);
}
return node;
}
void inOrderTraversal(Node* node, string*& tasks, int& size, int& capacity)
{
if (node == nullptr)
return;
inOrderTraversal(node->left, tasks, size, capacity);
// Resize the array if needed
if (size >= capacity)
{
capacity *= 2; // Double the capacity
string* newTasks = new string[capacity];
for (int i = 0; i < size; ++i)
{
newTasks[i] = tasks[i];
}
delete[] tasks; // Free old array
tasks = newTasks; // Point to new array
}
// Add the current task to the tasks array
tasks[size++] = "Priority: " + to_string(node->task.priority) + " " + node->task.description;
inOrderTraversal(node->right, tasks, size, capacity);
}
Node* deleteTask(Node* node, string data, int priority)
{
if (node == nullptr)
return node;
// Only delete if both description and priority match
if (data < node->task.description || (data == node->task.description && priority < node->task.priority))
{
node->left = deleteTask(node->left, data, priority);
}
else if (data > node->task.description || (data == node->task.description && priority > node->task.priority))
{
node->right = deleteTask(node->right, data, priority);
}
else // Found the node to delete
{
if (node->left == nullptr)
{
Node* temp = node->right;
delete node;
return temp;
}
else if (node->right == nullptr)
{
Node* temp = node->left;
delete node;
return temp;
}
Node* temp = findMin(node->right);
node->task = temp->task;
node->right = deleteTask(node->right, temp->task.description, temp->task.priority);
}
return node;
}
Node* findMin(Node* node)
{
while (node->left != nullptr)
node = node->left;
return node;
}
void clear(Node* node)
{
if (node == nullptr)
return;
clear(node->left);
clear(node->right);
delete node;
}
Node* search(Node* node, string data, int priority)
{
if (node == nullptr || (node->task.description == data && node->task.priority == priority))
return node;
if (data < node->task.description || (data == node->task.description && priority < node->task.priority))
return search(node->left, data, priority);
return search(node->right, data, priority);
}
public:
ToDoList() { root = nullptr; }
~ToDoList() { clear(root); }
void addTask(string data, int priority)
{
try {
if (priority < 1 || priority > 5 )
{
throw invalid_argument("Invalid priority! Must be between 1 and 5.");
}
Task newTask(data, priority);
root = insert(root, newTask);
} catch (const invalid_argument& e) {
cout << "Error adding task: " << e.what() << endl;
}
}
void deleteTask(string data, int priority)
{
try {
root = deleteTask(root, data, priority);
} catch (const exception& e) {
cout << "Error deleting task: " << e.what() << endl;
}
}
bool isFound(string data, int priority)
{
try {
return search(root, data, priority) != nullptr;
} catch (const exception& e) {
cout << "Error searching for task: " << e.what() << endl;
return false;
}
}
string* retrieveAllTasks(int& size)
{
try {
int capacity = 10; // Initial capacity
size = 0;
string* tasks = new string[capacity];
inOrderTraversal(root, tasks, size, capacity);
// Resize to fit the actual number of tasks
string* finalTasks = new string[size];
for (int i = 0; i < size; ++i)
{
finalTasks[i] = tasks[i];
}
delete[] tasks; // Free the dynamic array
return finalTasks; // Return the resized array
} catch (const exception& e) {
cout << "Error retrieving tasks: " << e.what() << endl;
return nullptr;
}
}
string retrieveTask(string data, int priority)
{
try {
Node* node = search(root, data, priority);
if (node)
{
return "Priority: " + to_string(node->task.priority) + " " + node->task.description;
}
else
{
throw runtime_error("Task not found!");
}
} catch (const runtime_error& e) {
cout << "Error retrieving task: " << e.what() << endl;
return "";
}
}
void setPriority(string data, int newPriority)
{
try {
if (newPriority < 1 || newPriority > 5)
{
throw invalid_argument("Invalid priority! Must be between 1 and 5.");
}
// Search for the task with the current priority
Node* node = nullptr;
for (int priority = 1; priority <= 5; priority++)
{
node = search(root, data, priority);
if (node != nullptr)
{
break; // Found the task
}
}
if (node == nullptr)
{
throw runtime_error("Task not found!");
}
// Delete the task with its current priority
deleteTask(data, node->task.priority);
// Add a new task with the same description but the new priority
addTask(data, newPriority);
} catch (const exception& e) {
cout << "Error setting priority: " << e.what() << endl;
}
}
void displaySortedByPriority()
{
try {
int size;
string* tasks = retrieveAllTasks(size);
for (int i = 0; i < size; ++i)
{
cout << tasks[i] << endl;
}
delete[] tasks; // Free the dynamic array
} catch (const exception& e) {
cout << "Error displaying tasks: " << e.what() << endl;
}
}
};
#endif // TODOLIST_H