-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedListAPI.c
384 lines (316 loc) · 11.5 KB
/
LinkedListAPI.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include "LinkedListAPI.h"
#include "assert.h"
/** Function to initialize the list metadata head to the appropriate function pointers. Allocates memory to the struct.
*@return pointer to the list head
*@param printFunction function pointer to print a single node of the list
*@param deleteFunction function pointer to delete a single piece of data from the list
*@param compareFunction function pointer to compare two nodes of the list in order to test for equality or order
**/
List * initializeList(char* (*printFunction)(void* toBePrinted),void (*deleteFunction)(void* toBeDeleted),int (*compareFunction)(const void* first,const void* second)){
//Asserts create a partial function...
assert(printFunction != NULL);
assert(deleteFunction != NULL);
assert(compareFunction != NULL);
List * tmpList = malloc(sizeof(List));
tmpList->head = NULL;
tmpList->tail = NULL;
tmpList->length = 0;
tmpList->deleteData = deleteFunction;
tmpList->compare = compareFunction;
tmpList->printData = printFunction;
return tmpList;
}
/** Deletes the entire linked list, freeing all memory.
* uses the supplied function pointer to release allocated memory for the data
*@pre 'List' type must exist and be used in order to keep track of the linked list.
*@param list pointer to the List-type dummy node
*@return on success: NULL, on failure: head of list
**/
void freeList(List* list){
clearList(list);
free(list);
}
/**Function for creating a node for the linked list.
* This node contains abstracted (void *) data as well as previous and next
* pointers to connect to other nodes in the list
* @pre data should be of same size of void pointer on the users machine to avoid size conflicts. data must be valid.
* data must be cast to void pointer before being added.
* @post data is valid to be added to a linked list
* @return On success returns a node that can be added to a linked list. On failure, returns NULL.
* @param data - is a void * pointer to any data type. Data must be allocated on the heap.
**/
Node* initializeNode(void* data){
Node* tmpNode = (Node*)malloc(sizeof(Node));
if (tmpNode == NULL){
return NULL;
}
tmpNode->data = data;
tmpNode->previous = NULL;
tmpNode->next = NULL;
return tmpNode;
}
/**Inserts a Node at the front of a linked list. List metadata is updated
* so that head and tail pointers are correct.
*@pre 'List' type must exist and be used in order to keep track of the linked list.
*@param list pointer to the dummy head of the list
*@param toBeAdded a pointer to data that is to be added to the linked list
**/
void insertBack(List* list, void* toBeAdded){
if (list == NULL || toBeAdded == NULL){
return;
}
(list->length)++;
Node* newNode = initializeNode(toBeAdded);
if (list->head == NULL && list->tail == NULL){
list->head = newNode;
list->tail = list->head;
}else{
newNode->previous = list->tail;
list->tail->next = newNode;
list->tail = newNode;
}
}
/**Inserts a Node at the front of a linked list. List metadata is updated
* so that head and tail pointers are correct.
*@pre 'List' type must exist and be used in order to keep track of the linked list.
*@param list pointer to the dummy head of the list
*@param toBeAdded a pointer to data that is to be added to the linked list
**/
void insertFront(List* list, void* toBeAdded){
if (list == NULL || toBeAdded == NULL){
return;
}
(list->length)++;
Node* newNode = initializeNode(toBeAdded);
if (list->head == NULL && list->tail == NULL){
list->head = newNode;
list->tail = list->head;
}else{
newNode->next = list->head;
list->head->previous = newNode;
list->head = newNode;
}
}
/**Returns a pointer to the data at the front of the list. Does not alter list structure.
*@pre The list exists and has memory allocated to it
*@param the list struct
*@return pointer to the data located at the head of the list
**/
void* getFromFront(List * list){
if (list->head == NULL){
return NULL;
}
return list->head->data;
}
/**Returns a pointer to the data at the back of the list. Does not alter list structure.
*@pre The list exists and has memory allocated to it
*@param the list struct
*@return pointer to the data located at the tail of the list
**/
void* getFromBack(List * list){
if (list->tail == NULL){
return NULL;
}
return list->tail->data;
}
/** Removes data from from the list, deletes the node and frees the memory,
* changes pointer values of surrounding nodes to maintain list structure.
* returns the data
* You can assume that the list contains no duplicates
*@pre List must exist and have memory allocated to it
*@post toBeDeleted will have its memory freed if it exists in the list.
*@param list - a pointer to the List struct
*@param toBeDeleted - a pointer to data that is to be removed from the list
*@return on success: void * pointer to data on failure: NULL
**/
void* deleteDataFromList(List* list, void* toBeDeleted){
if (list == NULL || toBeDeleted == NULL){
return NULL;
}
Node* tmp = list->head;
while(tmp != NULL){
if (list->compare(toBeDeleted, tmp->data) == 0){
//Unlink the node
Node* delNode = tmp;
if (tmp->previous != NULL){
tmp->previous->next = delNode->next;
}else{
list->head = delNode->next;
}
if (tmp->next != NULL){
tmp->next->previous = delNode->previous;
}else{
list->tail = delNode->previous;
}
void* data = delNode->data;
free(delNode);
(list->length)--;
return data;
}else{
tmp = tmp->next;
}
}
return NULL;
}
/** Uses the comparison function pointer to place the element in the
* appropriate position in the list.
* should be used as the only insert function if a sorted list is required.
*@pre List exists and has memory allocated to it. Node to be added is valid.
*@post The node to be added will be placed immediately before or after the first occurrence of a related node
*@param list a pointer to the dummy head of the list containing function pointers for delete and compare, as well
as a pointer to the first and last element of the list.
*@param toBeAdded a pointer to data that is to be added to the linked list
**/
void insertSorted(List *list, void *toBeAdded){
if (list == NULL || toBeAdded == NULL){
return;
}
(list->length)++;
if (list->head == NULL){
insertBack(list, toBeAdded);
return;
}
if (list->compare(toBeAdded, list->head->data) <= 0){
insertFront(list, toBeAdded);
return;
}
if (list->compare(toBeAdded, list->tail->data) > 0){
insertBack(list, toBeAdded);
return;
}
Node* currNode = list->head;
while (currNode != NULL){
if (list->compare(toBeAdded, currNode->data) <= 0){
char* currDescr = list->printData(currNode->data);
char* newDescr = list->printData(toBeAdded);
//printf("Inserting %s before %s\n", newDescr, currDescr);
free(currDescr);
free(newDescr);
Node* newNode = initializeNode(toBeAdded);
newNode->next = currNode;
newNode->previous = currNode->previous;
currNode->previous->next = newNode;
currNode->previous = newNode;
return;
}
currNode = currNode->next;
}
return;
}
/**Returns a string that contains a string representation of the list traversed from head to tail.
Utilizes an iterator and the list's printData function pointer to create the string.
returned string must be freed by the calling function.
*@pre List must exist, but does not have to have elements.
*@param list Pointer to linked list dummy head.
*@return on success: char * to string representation of list (must be freed after use). on failure: NULL
**/
char* toString(List * list){
ListIterator iter = createIterator(list);
char* str;
str = (char*)malloc(sizeof(char));
strcpy(str, "");
void* elem;
while((elem = nextElement(&iter)) != NULL){
char* currDescr = list->printData(elem);
int newLen = strlen(str)+50+strlen(currDescr);
str = (char*)realloc(str, newLen);
strcat(str, "\n");
strcat(str, currDescr);
free(currDescr);
}
return str;
}
/**Returns the number of elements in the list.
*@pre List must exist, but does not have to have elements.
*@param list - a pointer to the List struct.
*@return on success: number of eleemnts in the list (0 or more). on failure: -1 (e.g. list not initlized correctly)
**/
int getLength(List* list){
return list->length;
}
/** Function that searches for an element in the list using a comparator function.
* If an element is found, a pointer to the data of that element is returned
* Returns NULL if the element is not found.
*@pre List exists and is valid. Comparator function has been provided.
*@post List remains unchanged.
*@return The data associated with the list element that matches the search criteria. If element is not found, return NULL.
*@param list - a pointer to the List sruct
*@param customCompare - a pointer to comparator function for customizing the search
*@param searchRecord - a pointer to search data, which contains seach criteria
*Note: while the arguments of compare() and searchRecord are all void, it is assumed that records they point to are
* all of the same type - just like arguments to the compare() function in the List struct
**/
void* findElement(List * list, bool (*customCompare)(const void* first,const void* second), const void* searchRecord){
if (customCompare == NULL)
return NULL;
ListIterator itr = createIterator(list);
void* data = nextElement(&itr);
while (data != NULL)
{
if (customCompare(data, searchRecord))
return data;
data = nextElement(&itr);
}
return NULL;
}
/** Clears the contents linked list, freeing all memory associated with these contents. The list itself is not freed.
* uses the supplied function pointer to release allocated memory for the data
*@pre 'List' type must exist and be used in order to keep track of the linked list.
*@post List is empty and list length has been set to 0
*@param list - a pointer to the List struct
**/
void clearList(List* list) {
if (list == NULL) {
return;
}
Node *temp = list->head;
Node *prev = NULL;
while (temp != NULL) {
list->deleteData(temp->data);
prev = temp;
temp = temp->next;
free(prev);
}
list->head = NULL;
list->tail = NULL;
list->length = 0;
return;
}
/** Function that returns the next element of the list through the iterator.
* This function returns the data at head of the list the first time it is called after.
* the iterator was created. Every subsequent call returns the data associated with the next element.
* Returns NULL once the end of the iterator is reached.
*@pre List exists and is valid. Iterator exists and is valid.
*@post List remains unchanged. The iterator points to the next element on the list.
*@return The data associated with the list element that the iterator pointed to when the function was called.
*@param iter - an iterator for a List struct.
**/
void* nextElement(ListIterator* iter) {
void *toReturn = NULL;
if(iter == NULL) {
return NULL;
}
if (iter->current == NULL) {
return NULL;
}
toReturn = iter->current->data;
iter->current = iter->current->next;
return toReturn;
}
/** Function for creating an iterator for the linked list.
* This node contains abstracted (void *) data as well as previous and next
* pointers to connect to other nodes in the list
*@pre List exists and is valid
*@post List remains unchanged. The iterator has been allocated and points to the head of the list.
*@return The newly created iterator object.
*@param list - pointer to the List struct to iterate over.
**/
ListIterator createIterator(List* list) {
ListIterator tmp;
if(list == NULL) {
tmp.current = NULL;
return tmp;
}
tmp.current = list->head;
return tmp;
}