-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.c
317 lines (274 loc) · 7.79 KB
/
linked_list.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "linked_list.h"
#include <stdlib.h> // For malloc
// NODE STRUCT
struct node{
struct node* next;
struct node* previous;
void* nodeContent;
};
// CREATE FUNCTIONS
ll_node** LL_initRoot() {
ll_node** root = malloc(sizeof(ll_node*));
*root = NULL;
return root;
}
ll_node* LL_createNode(void* content) {
ll_node* newNode = malloc(sizeof(ll_node));
newNode->next = NULL;
newNode->previous = NULL;
newNode->nodeContent = content;
return newNode;
}
// GET FUNCTIONS
ll_node* LL_getNext(ll_node* toGetFrom) {
if(toGetFrom != NULL) {
return toGetFrom->next;
} else {
return NULL;
}
}
ll_node* LL_getPrevious(ll_node** root, ll_node* toGetFrom) {
if(toGetFrom != NULL) {
return toGetFrom->previous;
} else {
return NULL;
}
}
void* LL_getContent(ll_node* toGetFrom) {
if(toGetFrom != NULL) {
return toGetFrom->nodeContent;
} else {
return NULL;
}
}
ll_node* LL_getNodeAtIndex(ll_node** root, int index) {
ll_node* currentNode = *root;
int i = 0;
while(currentNode != NULL && i < index) {
currentNode = currentNode->next;
i++;
}
return currentNode;
}
int LL_getNodeIndex(ll_node** root, ll_node* toGetFrom) {return -1;};
// INSERT FUNCTIONS
void LL_insertSequentially(ll_node** root, ll_node* toInsert){
// if currentNode is empty, the list is empty and root will point at the new node
if(*root == NULL) {
*root = toInsert;
} else {
ll_node* cursor = *root;
while(cursor->next != NULL) {
cursor = cursor->next;
}
toInsert->previous = cursor;
cursor->next = toInsert;
}
}
ll_node* LL_createAndInsertSequentially(ll_node** root, void* content){
ll_node* newNode = LL_createNode(content);
LL_insertSequentially(root, newNode);
return newNode;
}
/* insertComparatively
Inserts a node into the linked list and uses a pointer to a function which accepts two void pointers and compares the values at these pointers. This function will insert the node whenever the function returns 0 or more.
*/
void LL_insertComparatively(ll_node** root, ll_node* toInsert,
int(*compareFunction)(void*, void*)) {
if(toInsert != NULL) {
if(*root == NULL) {
// if list is empty
*root = toInsert;
} else if (compareFunction(toInsert->nodeContent, (*root)->nodeContent) < 0) {
// if the new node should be all the way to the left
toInsert->next = *root;
*root = toInsert;
if(toInsert->next != NULL) {
toInsert->next->previous = toInsert;
}
} else {
ll_node* cursor = *root;
while(cursor->next != NULL &&
compareFunction(toInsert->nodeContent, cursor->next->nodeContent) >= 0) {
cursor = cursor->next;
}
toInsert->next = cursor->next;
toInsert->previous = cursor;
cursor->next = toInsert;
if(toInsert->next != NULL) {
toInsert->next->previous = toInsert;
}
}
}
}
ll_node* LL_createAndInsertComparatively(ll_node** root, void* content,
int(*compareFunction)(void*, void*)) {
ll_node* newNode = LL_createNode(content);
LL_insertComparatively(root, newNode, compareFunction);
return newNode;
}
// REMOVE FUNCTIONS
ll_node* LL_removePointer(ll_node** root, ll_node* toRemove){
if(toRemove == NULL || LL_isEmpty(root)) {
return NULL;
} else if(*root == toRemove) {
// if toRemove is first node, set root as next node
*root = toRemove->next;
if(*root != NULL) {
(*root)->previous = NULL;
}
return toRemove;
} else {
// if toRemove is in other part of the list, search for it and
// make previous node point to the one after toRemove.
ll_node* cursor = *root;
while(cursor->next != NULL && cursor->next != toRemove){
cursor = cursor->next;
}
// if the node was found, remove and return it, otherwise return NULL
if(cursor->next != NULL) {
cursor->next = cursor->next->next;
if(cursor-> next != NULL) {
cursor->next->previous = cursor;
}
return toRemove;
} else {
return NULL;
}
}
}
ll_node* LL_removeIndex(ll_node** root, int index){
if(index < 0 || LL_isEmpty(root)) {
// if index is less than 0 or if list is empty return NULL
return NULL;
} else if(index == 0) {
// if index is 0, set root as next node
ll_node* returnNode = *root;
*root = (*root)->next;
if(*root != NULL) {
(*root)->previous = NULL;
}
return returnNode;
} else {
// if index is higher than 0, search for node and make previous
// node point to the one after the one at index.
ll_node* cursor = *root;
int i = 0;
while(cursor->next != NULL && i < index-1){
cursor = cursor->next;
i++;
}
// if the node was found, remove and return it, otherwise return NULL
if(cursor->next != NULL) {
ll_node* returnNode = cursor->next;
cursor->next = cursor->next->next;
if(cursor->next != NULL) {
cursor->next->previous = cursor;
}
return returnNode;
} else {
return NULL;
}
}
}
void* LL_deletePointer(ll_node** root, ll_node* toRemove){
ll_node* node = LL_removePointer(root, toRemove);
void* content = LL_getContent(node);
free(node);
return content;
}
void* LL_deleteIndex(ll_node** root, int index){
ll_node* node = LL_removeIndex(root, index);
void* content = LL_getContent(node);
free(node);
return content;
}
void LL_purgePointer(ll_node** root, ll_node* toRemove){
free(LL_deletePointer(root, toRemove));
}
void LL_purgeIndex(ll_node** root, int index){
free(LL_deleteIndex(root, index));
}
void LL_deleteList(ll_node** root){
for(int i = LL_length(root); i > 0; i--) {
LL_deletePointer(root, *root);
}
free(root);
}
void LL_purgeList(ll_node** root){
for(int i = LL_length(root); i > 0; i--) {
LL_purgePointer(root, *root);
}
free(root);
}
void LL_purgeNode(ll_node* node){
free(LL_getContent(node));
free(node);
}
/************************************/
/* */
/* MISC FUNCTIONS */
/* */
/************************************/
int LL_length(ll_node** root) {
int i = 0;
ll_node* cursor = *root;
while(cursor != NULL) {
cursor = cursor->next;
i++;
}
return i;
}
int LL_isEmpty(ll_node** root) {
return(*root == NULL);
}
int LL_isValidAux(ll_node* cursor, int(validationFunction)(void*, void*), void* validationData);
int LL_isValid(ll_node** root, int(validationFunction)(void*, void*), void* validationData) {
if(validationData == NULL){
return 0;
}
if(root == NULL) {
// if there is no list return 1. This is because the assumption is
// that a list is valid and saying it is invalid implies that
// nodes were checked.
return 1;
} else {
return LL_isValidAux(*root, validationFunction, validationData);
}
}
int LL_isValidAux(ll_node* cursor, int(validationFunction)(void*, void*), void* validationData) {
if(cursor == NULL) {
// if end of list is reached, return valid
return 1;
} else if(!validationFunction(LL_getContent(cursor), validationData)) {
// if validation function returns 0, return invalid
return 0;
} else {
// recursively continue
return LL_isValidAux(LL_getNext(cursor), validationFunction, validationData);
}
}
void LL_map(ll_node** root, void(*mapFunction)(void*)) {
ll_node* cursor = *root;
while(cursor != NULL) {
mapFunction(LL_getContent(cursor));
cursor = LL_getNext(cursor);
}
}
ll_node* LL_search(ll_node** root, void* content, int(*compareFunction)(void*, void*)) {
ll_node* cursor;
if(root != NULL) {
cursor = *root;
} else {
cursor = NULL;
}
while(cursor != NULL) {
if(compareFunction(LL_getContent(cursor), content) == 0) {
// if content matches the content of the cursor, return cursor
return cursor;
}
cursor = LL_getNext(cursor);
}
// if nothing was found, return NULL
return NULL;
}