-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.c
128 lines (119 loc) · 3.13 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
#include "linked_list.h"
#include <stdlib.h>
bool InitList(LinkedList **list, bool (* free)(void *item), bool (* match)(const void *itemInList, const void *item))
{
LinkedListNode *head;
if (!MakeListNode(&head, NULL)) {
return false;
}
LinkedList *newList;
if ((newList = (LinkedList *)malloc(sizeof(LinkedList))) == NULL) {
FreeListNode(head);
return false;
}
head->item = head->next = NULL;
newList->head = head;
newList->len = 0;
newList->free = free;
newList->match = match;
*list = newList;
return true;
}
bool FreeList(LinkedList *list)
{
LinkedListNode *current = list->head->next;
while (current != NULL) {
list->head->next = current->next;
list->free(current->item);
FreeListNode(current);
current = list->head->next;
}
if (list->head) {
FreeListNode(list->head);
}
free(list);
return true;
}
bool ResetList(LinkedList *list)
{
LinkedListNode *current = list->head->next;
while (current != NULL) {
list->head->next = current->next;
list->free(current->item);
FreeListNode(current);
current = list->head->next;
}
list->len = 0;
return true;
}
bool MakeListNode(LinkedListNode **node, void *item)
{
if ((*node = (LinkedListNode *)malloc(sizeof(LinkedListNode))) == NULL) {
return false;
}
(*node)->item = item;
(*node)->next = NULL;
return true;
}
bool FreeListNode(LinkedListNode *node)
{
free(node);
return true;
}
bool FindListItem(const LinkedList *list, void **item, const void *findItem)
{
LinkedListNode *current = list->head->next;
while (current != NULL) {
if (list->match(current->item, findItem)) {
*item = current->item;
return true;
}
current = current->next;
}
return false;
}
bool InsertListItem(LinkedList *list, void *item, const void *afterThisItem)
{
LinkedListNode *previous = list->head;
while (previous != NULL) {
if (list->match(previous->item, afterThisItem)) {
LinkedListNode *newNode;
if (!MakeListNode(&newNode, item)) {
return false;
}
newNode->next = previous->next;
previous->next = newNode;
list->len++;
return true;
}
previous = previous->next;
}
return false;
}
bool DeleteListItem(LinkedList *list, void *findItem)
{
LinkedListNode *previous = list->head, *current = list->head->next;
while (current != NULL) {
if (list->match(current->item, findItem)) {
previous->next = current->next;
list->free(current->item);
FreeListNode(current);
list->len--;
return true;
}
previous = current;
current = current->next;
}
return false;
}
bool TraverseList(LinkedList *list, bool (* invoke)(const void *item))
{
LinkedListNode *current = list->head->next;
while (current != NULL) {
if (!invoke(current->item)) {
return false;
}
current = current->next;
}
return true;
}