-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.c
189 lines (172 loc) · 4.33 KB
/
linkedlist.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
#include "linkedlist.h"
#include <stdlib.h>
#include <assert.h>
list_t* makeEmptyList(void) {
list_t *list;
list = (list_t*)malloc(sizeof(*list));
assert(list!=NULL);
list->head = list->foot = NULL;
return list;
}
int isEmptyList(list_t *list) {
assert(list!=NULL);
return list->head==NULL;
}
void freeList(list_t *list) {
node_t *curr, *prev;
assert(list!=NULL);
curr = list->head;
while (curr) {
prev = curr;
curr = curr->next;
free(prev->process->processName);
free(prev->process);
free(prev);
}
free(list);
}
// returns the number of nodes in the linked list
int numberOfNodes(list_t *list) {
node_t *curr;
assert(list!=NULL);
curr = list->head;
int n = 0;
while (curr) {
curr = curr->next;
n++;
}
return n;
}
// checks whether all processes in the list are finished. if all are finished, returned 1, otherwise return 0
int isFinished(list_t *list) {
int unfinished = 0;
node_t *curr, *prev;
assert(list!=NULL);
curr = list->head;
while (curr) {
prev = curr;
curr = curr->next;
if (prev->process->state != FINISHED) {
unfinished++;
}
}
if (unfinished == 0) {
return 1;
} else {
return 0;
}
}
list_t* insertAtHead(list_t *list, process_t *value) {
node_t *new;
new = (node_t*)malloc(sizeof(*new));
assert(list!=NULL && new!=NULL);
new->process = value;
new->next = list->head;
new->prev = NULL;
list->head = new;
if (list->foot==NULL) {
/* this is the first insertion into the list */
list->foot = new;
}
return list;
}
list_t* insertAtFoot(list_t *list, process_t *value) {
node_t *new;
new = (node_t*)malloc(sizeof(*new));
assert(list!=NULL && new!=NULL);
new->process = value;
new->prev = list->foot;
new->next = NULL;
if (list->foot==NULL) {
/* this is the first insertion into the list */
list->head = list->foot = new;
} else {
new->prev = list->foot;
list->foot->next = new;
list->foot = new;
}
return list;
}
// for debugging, prints all the contents of the processes in the given list
void printAllNodes(list_t *list) {
node_t *curr, *prev;
assert(list!=NULL);
curr = list->head;
while (curr) {
prev = curr;
curr = curr->next;
printf("%d %s %d %d\n", prev->process->timeArrived, prev->process->processName,
prev->process->serviceTime, prev->process->memoryRequirement);
}
}
// loops through the list to check whether there are any new input processes
int checkForArrived(list_t* list, int makespan) {
node_t *curr, *prev;
curr = list->head;
while (curr) {
prev = curr;
curr = curr->next;
if (prev->process->state == ARRIVED && (prev->process->timeArrived <= makespan)) {
return 1;
}
}
return 0;
}
// deletes a node from the given list, based on the name of the process
void deleteNode(list_t *list, char *processName) {
node_t *curr;
assert(list != NULL);
curr = list->head;
while (curr != NULL) {
if (strcmp(processName, curr->process->processName) == 0) {
break;
}
curr = curr->next;
}
// if node was found, remove it from the list
if (curr != NULL) {
// if node is the head of the list
if (curr == list->head) {
list->head = curr->next;
}
// if node is the foot of the list
if (curr == list->foot) {
list->foot = curr->prev;
}
// update the previous node's next pointer
if (curr->prev != NULL) {
curr->prev->next = curr->next;
}
// update the next node's previous pointer
if (curr->next != NULL) {
curr->next->prev = curr->prev;
}
}
free(curr);
}
// checks how many processes are left in the input or ready queues
int remainingProcesses(list_t* processes, int makespan, int quantum) {
node_t *curr, *prev;
int remainingProcesses = 0;
assert(processes!=NULL);
curr = processes->head;
while (curr) {
prev = curr;
curr = curr->next;
// a process is remaining if it is in the input or ready queue
if (prev->process->timeArrived < (makespan - quantum) && (prev->process->state == READY || prev->process->state == ARRIVED)) {
remainingProcesses++;
}
}
return remainingProcesses;
}
// rounds up a float
int roundUp(float x) {
int integerPart = (int)x;
float decimalPart = x - integerPart;
if (decimalPart > 0) {
return integerPart + 1;
} else {
return integerPart;
}
}