-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.js
More file actions
285 lines (255 loc) · 8.79 KB
/
queue.js
File metadata and controls
285 lines (255 loc) · 8.79 KB
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
281
282
283
284
285
class Node {
constructor(task, description, dueDate, category) {
this.task = task;
this.description = description;
this.dueDate = dueDate;
this.category = category;
this.isComplete = false;
this.next = null;
}
}
class CompletedNode {
constructor(task, description, dueDate, category) {
this.task = task;
this.description = description;
this.dueDate = dueDate;
this.category = category;
this.next = null;
}
}
class TaskQueue {
constructor() {
this.front = null;
this.rear = null;
this.completedHead = null;
this.completedCount = 0;
this.totalCount = 0;
}
isEmpty() {
return this.front === null;
}
// Add task
enqueue(task, description, dueDate, category) {
const today = new Date();
const taskDate = new Date(dueDate);
// Validate date: must not be in the past and within one year
if (
isNaN(taskDate) ||
taskDate < today ||
taskDate > new Date(today.getFullYear() + 1, today.getMonth(), today.getDate())
) {
console.log(`Invalid due date: ${dueDate}. Please enter a future date within one year.`);
return;
}
const newNode = new Node(task, description, dueDate, category);
this.totalCount++;
if (this.isEmpty()) {
this.front = this.rear = newNode;
} else {
let current = this.front;
let prev = null;
while (current !== null && new Date(current.dueDate) <= new Date(newNode.dueDate)) {
prev = current;
current = current.next;
}
if (prev === null) {
newNode.next = this.front;
this.front = newNode;
} else {
newNode.next = current;
prev.next = newNode;
if (current === null) this.rear = newNode;
}
}
console.log(`Task "${task}" added successfully.`);
}
// Remove the highest priority task
dequeueHighestPriority() {
if (this.isEmpty()) {
console.log("Queue is empty, cannot dequeue.");
return;
}
const temp = this.front;
this.front = this.front.next;
this.totalCount--;
console.log(`Task "${temp.task}" [Due: ${temp.dueDate}] removed successfully.`);
if (this.front === null) this.rear = null;
}
// View all tasks
viewTasks() {
if (this.isEmpty()) {
console.log("No tasks available.");
return;
}
console.log("Current Tasks:");
let curr = this.front;
let index = 1;
while (curr !== null) {
console.log(
`${index++}. ${curr.task} [Due: ${curr.dueDate}, Status: ${curr.isComplete ? "Complete" : "Incomplete"}]`
);
curr = curr.next;
}
}
// Mark a task as complete
markComplete(taskName) {
let curr = this.front;
let prev = null;
while (curr !== null) {
if (curr.task === taskName) {
const completedTask = new CompletedNode(curr.task, curr.description, curr.dueDate, curr.category);
this.completedCount++;
completedTask.next = this.completedHead;
this.completedHead = completedTask;
if (prev === null) {
this.front = curr.next;
} else {
prev.next = curr.next;
}
console.log(`Task "${curr.task}" marked as complete.`);
if (this.front === null) this.rear = null;
return;
}
prev = curr;
curr = curr.next;
}
console.log(`Task not found: "${taskName}".`);
}
// Undo a completed task
undoCompleted() {
if (this.completedHead === null) {
console.log("No completed tasks to undo.");
return;
}
const temp = this.completedHead;
this.completedHead = this.completedHead.next;
this.enqueue(temp.task, temp.description, temp.dueDate, temp.category);
this.completedCount--;
console.log(`Task "${temp.task}" has been undone.`);
}
// Edit a task
editTask(taskName, newTask, newDescription, newDueDate, newCategory) {
let curr = this.front;
while (curr !== null) {
if (curr.task === taskName) {
this.deleteTask(taskName); // Remove the old task
this.enqueue(newTask, newDescription, newDueDate, newCategory); // Add the updated task
console.log("Task updated successfully.");
return;
}
curr = curr.next;
}
console.log(`Task not found: "${taskName}".`);
}
// Delete a task
deleteTask(taskName) {
if (this.isEmpty()) {
console.log("Queue is empty, cannot delete.");
return;
}
let curr = this.front;
let prev = null;
while (curr !== null) {
if (curr.task === taskName) {
if (prev === null) {
this.front = curr.next;
} else {
prev.next = curr.next;
}
if (curr === this.rear) {
this.rear = prev;
}
console.log(`Task "${curr.task}" deleted successfully.`);
return;
}
prev = curr;
curr = curr.next;
}
console.log(`Task not found: "${taskName}".`);
}
// View completed tasks
viewCompletedTasks() {
if (this.completedHead === null) {
console.log("No completed tasks available.");
return;
}
console.log("Completed Tasks:");
let curr = this.completedHead;
let index = 1;
while (curr !== null) {
console.log(`${index++}. ${curr.task} [Due: ${curr.dueDate}]`);
curr = curr.next;
}
}
// Show progress
showProgress() {
const totalTasks = this.totalCount;
const progress = totalTasks === 0 ? 0.0 : (this.completedCount / totalTasks) * 100;
console.log(`Progress: ${progress}%`);
}
// View tasks by a category
viewTasksByCategory(category) {
if (this.isEmpty()) {
console.log("No tasks available.");
return;
}
console.log(`Tasks in category "${category}":`);
let curr = this.front;
let found = false;
while (curr !== null) {
if (curr.category === category) {
console.log(
`- ${curr.task} [Due: ${curr.dueDate}, Status: ${curr.isComplete ? "Complete" : "Incomplete"}]`
);
found = true;
}
curr = curr.next;
}
if (!found) console.log("No tasks found in this category.");
}
// Display the task with the latest deadline
displayLatestDeadlineTask() {
if (this.isEmpty()) {
console.log("No tasks available.");
return;
}
let latestTask = this.front;
let curr = this.front;
while (curr !== null) {
if (new Date(curr.dueDate) > new Date(latestTask.dueDate)) {
latestTask = curr;
}
curr = curr.next;
}
console.log(
`Task with the latest deadline: "${latestTask.task}" [Due: ${latestTask.dueDate}, Category: ${latestTask.category}, Status: ${latestTask.isComplete ? "Complete" : "Incomplete"}]`
);
}
// Clear all tasks
clearAllTasks() {
if (this.isEmpty()) {
console.log("No tasks to clear.");
return;
}
console.log("Clearing all tasks...");
while (!this.isEmpty()) {
this.dequeueHighestPriority();
}
this.completedHead = null;
this.completedCount = 0;
console.log("All tasks cleared.");
}
}
// Example usage:
const taskQueue = new TaskQueue();
taskQueue.enqueue("Task 1", "Description 1", "2024-12-01", "Work");
taskQueue.enqueue("Task 2", "Description 2", "2024-11-15", "Personal");
taskQueue.enqueue("Task 3", "Description 3", "2025-01-01", "Work");
taskQueue.viewTasks();
taskQueue.dequeueHighestPriority();
taskQueue.viewTasks();
taskQueue.markComplete("Task 3");
taskQueue.viewCompletedTasks();
taskQueue.undoCompleted();
taskQueue.viewTasks();
taskQueue.clearAllTasks();