-
Notifications
You must be signed in to change notification settings - Fork 1
/
linked_list.c
220 lines (157 loc) · 3.83 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
#include <stdlib.h>
#include "linked_list.h"
// List creation/destruction functions
list *list_new(list_free_func node_free_func)
{
list *new = malloc(sizeof(list));
if (new != NULL)
{
new->data_free_func = node_free_func;
new->start = NULL;
new->end = NULL;
}
return new;
}
void list_free(list *doomed)
{
list_node *needle;
while ((needle = list_pop_node(doomed)) != NULL)
{
list_node_free(needle, doomed->data_free_func);
}
free(doomed);
}
void list_node_free(list_node *doomed, list_free_func node_free_func)
{
if (node_free_func != NULL)
node_free_func(doomed->data);
else
free(doomed->data);
free(doomed);
}
// List manipulation (automatic mem)
int list_push(list *haystack, void *data)
{
list_node *needle;
if (haystack == NULL)
return E_LINKED_LIST_NULL_HAYSTACK;
needle = malloc(sizeof(list_node));
if (needle == NULL)
return E_LINKED_LIST_OUT_OF_MEM;
needle->data = data;
needle->prev = NULL;
needle->next = NULL;
return list_push_node(haystack, needle);
}
void *list_pop(list *haystack)
{
list_node *needle;
void *data;
if (haystack == NULL)
return NULL;
needle = list_pop_node(haystack);
if (needle == NULL)
return NULL;
data = needle->data;
free(needle);
return data;
}
int list_unshift(list *haystack, void *data)
{
list_node *needle;
if (haystack == NULL)
return E_LINKED_LIST_NULL_HAYSTACK;
needle = malloc(sizeof(list_node));
if (needle == NULL)
return E_LINKED_LIST_OUT_OF_MEM;
needle->data = data;
needle->prev = NULL;
needle->next = NULL;
return list_unshift_node(haystack, needle);
}
void *list_shift(list *haystack)
{
list_node *needle;
void *data;
if (haystack == NULL)
return NULL;
needle = list_shift_node(haystack);
if (needle == NULL)
return NULL;
data = needle->data;
free(needle);
return data;
}
// List manipulation (manual mem)
int list_push_node(list *haystack, list_node *needle)
{
list_node *last;
if (haystack == NULL)
return E_LINKED_LIST_NULL_HAYSTACK;
last = haystack->end;
if (needle == NULL)
return E_LINKED_LIST_NULL_NEEDLE;
needle->prev = last;
needle->next = NULL;
if (last != NULL)
last->next = needle;
haystack->end = needle;
if (haystack->start == NULL)
haystack->start = needle;
return 0;
}
list_node *list_pop_node(list *haystack)
{
list_node *last, *next_to_last;
if (haystack == NULL)
return NULL;
last = haystack->end;
if (last != NULL)
{
next_to_last = last->prev;
if (next_to_last != NULL)
next_to_last->next = NULL;
else
haystack->start = NULL;
last->prev = NULL;
last->next = NULL;
haystack->end = next_to_last;
}
return last;
}
int list_unshift_node(list *haystack, list_node *needle)
{
list_node *first;
if (haystack == NULL)
return E_LINKED_LIST_NULL_HAYSTACK;
if (needle == NULL)
return E_LINKED_LIST_NULL_NEEDLE;
first = haystack->start;
needle->prev = NULL;
needle->next = first;
if (first != NULL)
first->prev = needle;
haystack->start = needle;
if (haystack->end == NULL)
haystack->end = needle;
return 0;
}
list_node *list_shift_node(list *haystack)
{
list_node *first, *second;
if (haystack == NULL)
return NULL;
first = haystack->start;
if (first != NULL)
{
second = first->next;
if (second != NULL)
second->prev = NULL;
else
haystack->end = NULL;
first->prev = NULL;
first->next = NULL;
haystack->start = second;
}
return first;
}