-
Notifications
You must be signed in to change notification settings - Fork 1
/
task_scheduler.c
244 lines (206 loc) · 6.26 KB
/
task_scheduler.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
struct task {
char title[50];
int deadline; // hrs
int priority; // 1 to 3 (importance)
struct task* next;
struct task* prev;
bool status;
};
void addTask(struct task** head) {
struct task* newTask = (struct task*)malloc(sizeof(struct task));
newTask->next = NULL;
newTask->status = false;
printf("\nEnter task name: ");
getchar(); // Consume newline character
gets(newTask->title);
printf("Enter task deadline (in hrs): ");
scanf("%d", &(newTask->deadline));
printf("Enter task priority (1 -> High, 2 -> Medium, 3 -> Low): ");
scanf("%d", &(newTask->priority));
if (*head == NULL) {
newTask->prev = NULL;
*head = newTask;
return;
}
struct task* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newTask;
newTask->prev = temp;
}
void sortTask(struct task** head) {
struct task* temp = *head;
int swapped, i;
struct task* ptr1;
struct task* lptr = NULL;
if (temp == NULL)
return;
do {
swapped = 0;
ptr1 = temp;
while (ptr1->next != lptr) {
// Compare by deadline first
if (ptr1->deadline > ptr1->next->deadline ||
(ptr1->deadline == ptr1->next->deadline && ptr1->priority > ptr1->next->priority)) {
struct task* tempTask = ptr1->next;
if (tempTask->next != NULL)
tempTask->next->prev = ptr1;
ptr1->next = tempTask->next;
tempTask->prev = ptr1;
tempTask->next = ptr1;
if (ptr1->prev != NULL)
ptr1->prev->next = tempTask;
else
*head = tempTask;
ptr1->prev = tempTask;
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
} while (swapped);
}
void completeTask(struct task** head) {
char markTask[50];
printf("Enter task name to mark as complete: ");
getchar(); // Consume newline character
gets(markTask);
struct task* temp = *head;
while (temp != NULL) {
if (strcmp(temp->title, markTask) == 0) {
temp->status = true;
return;
}
temp = temp->next;
}
printf("Task not found.\n");
}
void removeTask(struct task** head) {
char removeTask[50];
printf("Enter task name to be deleted: ");
getchar(); // Consume newline character
gets(removeTask);
struct task* temp = *head;
while (temp != NULL) {
if (strcmp(temp->title, removeTask) == 0) {
if (temp->prev != NULL) {
temp->prev->next = temp->next;
}
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
if (temp == *head) {
*head = temp->next;
}
free(temp);
return;
}
temp = temp->next;
}
printf("Task not found.\n");
}
void displayTasks(struct task* head) {
printf("%-30s%-15s%-15s%-10s\n", "Task Name", "Deadline (hrs)", "Priority", "Status");
while (head != NULL) {
printf("%-30s%-15d%-15d%-10s\n", head->title, head->deadline, head->priority, head->status ? "Complete" : "Incomplete");
head = head->next;
}
}
void saveTasksToFile(struct task* head) {
char filename[50];
printf("Enter the filename to save tasks: ");
scanf("%s", filename);
FILE* file = fopen(filename, "w");
if (file == NULL) {
printf("Error opening the file for writing.\n");
return;
}
while (head != NULL) {
fprintf(file, "%s %d %d %d\n", head->title, head->deadline, head->priority, head->status);
head = head->next;
}
fclose(file);
printf("Tasks have been saved to %s.\n", filename);
}
void loadTasksFromFile(struct task** head) {
char filename[50];
printf("Enter the filename to load tasks from: ");
scanf("%s", filename);
FILE* file = fopen(filename, "r");
if (file == NULL) {
printf("Error opening the file for reading.\n");
return;
}
// Read task details from the file and create a linked list
while (!feof(file)) {
struct task* newTask = (struct task*)malloc(sizeof(struct task));
if (fscanf(file, "%s %d %d %d", newTask->title, &(newTask->deadline), &(newTask->priority), &(newTask->status)) != 4) {
free(newTask);
break;
}
newTask->next = NULL;
if (*head == NULL) {
newTask->prev = NULL;
*head = newTask;
} else {
struct task* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newTask;
newTask->prev = temp;
}
}
fclose(file);
printf("Tasks have been loaded from %s.\n", filename);
}
int main() {
struct user* userList = NULL;
struct task* taskList = NULL;
int choice;
do {
printf("\nTask Management Menu:\n");
printf("1. Add Task\n");
printf("2. Sort Tasks\n");
printf("3. Complete Task\n");
printf("4. Remove Task\n");
printf("5. Display Tasks\n");
printf("6. Save Tasks to File\n");
printf("7. Load Tasks from File\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addTask(&taskList);
break;
case 2:
sortTask(&taskList);
displayTasks(taskList); // Display sorted tasks
break;
case 3:
completeTask(&taskList);
break;
case 4:
removeTask(&taskList);
break;
case 5:
displayTasks(taskList);
break;
case 6:
saveTasksToFile(taskList);
break;
case 7:
loadTasksFromFile(&taskList);
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 8);
return 0;
}