-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.c
More file actions
64 lines (54 loc) · 1.53 KB
/
Copy pathlist.c
File metadata and controls
64 lines (54 loc) · 1.53 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
/**
* Various list operations
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
#include "task.h"
// add a new task to the list of tasks
void insert(struct node** head, Task* newTask) {
// add the new task to the list
struct node* newNode = malloc(sizeof(struct node));
newNode->task = newTask;
newNode->next = *head;
*head = newNode;
}
// delete the selected task from the list
void delete(struct node** head, Task* task) {
struct node* temp;
struct node* prev;
temp = *head;
// special case - beginning of list
if (temp != NULL && strcmp(task->name, temp->task->name) == 0) {
*head = temp->next; // Move head to next node
free(temp->task->name);
free(temp->task);
free(temp); // Free the removed node
} else {
// interior or last element in the list
prev = *head;
temp = temp->next;
while (temp != NULL && strcmp(task->name, temp->task->name) != 0) {
prev = temp;
temp = temp->next;
}
// If found, remove it
if (temp != NULL) {
prev->next = temp->next;
free(temp->task->name);
free(temp->task);
free(temp); // Free the removed node
}
}
}
// traverse the list
void traverse(struct node* head) {
struct node* temp;
temp = head;
while (temp != NULL) {
printf("[%s] [%d] [%d]\n", temp->task->name, temp->task->priority,
temp->task->burst);
temp = temp->next;
}
}