-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_removers.c
290 lines (250 loc) · 6.46 KB
/
list_removers.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
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
286
287
288
289
290
/*Marwan Mostafa mam024 11305332*/
/* Addison McAuley, amm637, 11295940
* Ghanim Jamil, ghj729, 11298913
*/
#include <stdio.h>
#include <stdlib.h>
#include <list.h>
#include <string.h>
extern LIST listMemory[];
extern NODE nodeMemory[];
extern int nextOpenList;
extern int nextOpenNode;
extern NODE* freeNodes;
extern LIST* freeLists;
/*
* Removes the current item from the list, returns its item, and moves the
* cursor to the next item
* @preconditions: list cannot be empty
* @param list: the list we are removing our item from
* @return the item that has been removed
*/
void* ListRemove(LIST* list) {
NODE* newFree;
if (list == NULL) {
char* msg = "Error, cannot remove from a NULL list\n";
fwrite(msg, 1, strlen(msg), stderr);
exit(ERROR_EXIT);
}
else if (list->count == 0) {
char* msg = "Error, cannot remove from an empty list\n";
fwrite(msg, 1, strlen(msg), stderr);
exit(ERROR_EXIT);
}
else if (list->count == 1) {
/*
* 1. Link current node to freeNodes
* 2. Set list fields to NULL/0
* 3. Update freeNodes to be our new free node
* 4. Return the item of freeNode
*/
newFree = list->curr;
list->head = NULL;
list->tail = NULL;
list->curr = NULL;
list->count = list->count - 1;
freeNodes->next = newFree;
newFree->prev = freeNodes;
freeNodes = newFree;
return newFree->item;
}
else {
/*
* 1. Get next and prev of current node
* 2. Relink next and prev to eachother
* 3. Unlink current node from list
* 4. Link current node to freeNodes
* 5. Update cursor to be our next item
* 6. If needed, update head/tail
* 7. Subtract count
* 8. Update freeNodes to be our new free node
* 9. Return item of freeNode
*/
NODE* currentNext;
NODE* currentPrev;
newFree = list->curr;
currentNext = newFree->next;
currentPrev = newFree->prev;
/*
* Removing the tail - will make the cursor our new tail
* instead of the next item
*/
if (currentNext == NULL) {
currentPrev->next = currentNext;
list->curr = currentPrev;
list->tail = currentPrev;
}
/*
* Removing the head - still set cursor to next, but need
* to update the list head reference
*/
else if (currentPrev == NULL) {
currentNext->prev = currentPrev;
list->curr = currentNext;
list->head = currentNext;
}
/*
* Removing middle - simply just setting the cursor
*/
else {
currentNext->prev = currentPrev;
currentPrev->next = currentNext;
list->curr = currentNext;
}
list->count = list->count - 1;
newFree->prev = freeNodes;
newFree->next = NULL;
if (freeNodes != NULL) {
freeNodes->next = newFree;
}
freeNodes = newFree;
return newFree->item;
}
}
/*
* Deletes list, and frees all of its items
* @param list: list to be deleted
* @param itemFreeRoutine: pointer to a routine that will free an item from
* our list
*/
void ListFree(LIST* list, itemFree itemFreeRoutine) {
LIST* newFreeList;
NODE* newFreeNode;
if (list == NULL) {
char* msg = "Error, cannot free a null list\n";
fwrite(msg, 1, strlen(msg), stderr);
exit(ERROR_EXIT);
}
if (list->count != 0) {
ListFirst(list);
(*itemFreeRoutine)(ListCurr(list));
newFreeNode = list->curr;
newFreeNode->prev = freeNodes;
while(ListNext(list) != NULL) {
(*itemFreeRoutine)(ListCurr(list));
}
freeNodes = list->tail;
list->head = NULL;
list->tail = NULL;
list->curr = NULL;
list->count = 0;
}
newFreeList = list;
newFreeList->prev = freeLists;
newFreeList->next = NULL;
if (freeLists != NULL) {
freeLists->next = newFreeList;
}
freeLists = newFreeList;
}
/*
* Removes the last item from the list, returns its item, and moves the
* cursor to the new tail of the list
* @preconditions: list cannot be empty
* @param list: the list that we are trimming
* @return the item that has been trimmed from our list
*/
void* ListTrim(LIST* list) {
NODE* newFree;
if (list == NULL) {
char* msg = "Error, cannot trim a NULL list\n";
fwrite(msg, 1, strlen(msg), stderr);
exit(ERROR_EXIT);
}
else if (list->count == 0) {
char* msg = "Error, cannot trim an empty list\n";
fwrite(msg, 1, strlen(msg), stderr);
exit(ERROR_EXIT);
}
else if (list->count == 1) {
/*
* 1. Link current node to freeNodes
* 2. Set list fields to null/0
* 3. Update freeNodes to be new free node
* 4. Return item of free node
*/
newFree = list->tail;
list->head = NULL;
list->tail = NULL;
list->curr = NULL;
list->count = list->count - 1;
if (freeNodes != NULL) {
freeNodes->next = newFree;
}
newFree->prev = freeNodes;
freeNodes = newFree;
return newFree->item;
}
else {
/*
* 1. Get prev of current node
* 2. Unlink current from list
* 3. Link current node to freeNodes
* 4. Update cursor to be our new tail
* 5. Update tail to be new tail
* 6. Subtract count
* 7. update freeNodes to be new free node
* 8. Return item of freeNode
*/
NODE* currentPrev;
newFree = list->tail;
currentPrev = newFree->prev;
currentPrev->next = NULL;
list->curr = currentPrev;
list->tail = currentPrev;
list->count = list->count - 1;
newFree->prev = freeNodes;
newFree->next = NULL;
if (freeNodes != NULL) {
freeNodes->next = newFree;
}
freeNodes = newFree;
return newFree->item;
}
}
/*
* Concatenates list2 with list1
* @param list1: The first list
* @param list2: The second list
* @postconditions: list2 will be deleted
*/
void ListConcat(LIST* list1, LIST* list2) {
LIST* newFree;
if (list1 == NULL) {
char* msg = "Error, cannot concatenate to a null list\n";
fwrite(msg, 1, strlen(msg), stderr);
exit(ERROR_EXIT);
}
else if (list2 == NULL) {
char* msg = "Error, cannot concatenate a null list with a list\n";
fwrite(msg, 1, strlen(msg), stderr);
exit(ERROR_EXIT);
}
/* If our first list is empty, will basically make our first list
* the second list. Will also maintain cursor of list 1 (NULL)
*/
else if (list1->count == 0) {
list1->head = list2->head;
list1->tail = list2->tail;
list1->count = list2->count;
}
else {
list1->tail->next = list2->head;
if (list2->count != 0) {
list2->head->prev = list1->tail;
list1->tail = list2->tail;
}
list1->count = list1->count + list2->count;
}
list2->head = NULL;
list2->tail = NULL;
list2->curr = NULL;
list2->count = 0;
newFree = list2;
newFree->prev = freeLists;
newFree->next = NULL;
if (freeLists != NULL) {
freeLists->next = newFree;
}
freeLists = newFree;
}