-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked-list.c
265 lines (213 loc) · 3.8 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define LL_MINIMAL
#include "linked-list.h"
#include "_ll-internal.h"
void ll_init(linked_list_t * ll, cmp_func_t cmp)
{
if (ll == NULL)
{
errno = EINVAL;
return;
}
memset(ll, '\0', sizeof(linked_list_t));
ll->cmp = cmp;
}
void ll_clear(linked_list_t * ll, consume_func_t f, void * ctx)
{
if (ll == NULL)
{
errno = EINVAL;
return;
}
ll_node_t * n1 = ll->head;
ll_node_t * n2;
ll->head = ll->tail = NULL;
while (n1 != NULL)
{
if (f != NULL)
f(n1->value, ctx);
n2 = n1;
n1 = n1->next;
_ll_free_node(n2);
}
ll->size = 0;
}
bool ll_get_item(const linked_list_t * ll, int64_t index, ll_value_t * res)
{
if (ll == NULL || res == NULL)
{
errno = EINVAL;
return false;
}
const ll_node_t * n;
if (!_ll_shorter_index(ll_len(ll), &index))
return false;
if (index < 0)
{
++index;
index *= -1;
n = _ll_get_node_n(ll->tail, index, false);
}
else
{
n = _ll_get_node_n(ll->head, index, true);
}
if (n == NULL)
return false;
*res = n->value;
return true;
}
bool ll_set_item(linked_list_t * ll, int64_t index, ll_value_t val)
{
if (ll == NULL)
{
errno = EINVAL;
return false;
}
ll_node_t * n;
if (!_ll_shorter_index(ll_len(ll), &index))
return false;
if (index < 0)
{
++index;
index *= -1;
n = _ll_get_node_n(ll->tail, index, false);
}
else
{
n = _ll_get_node_n(ll->head, index, true);
}
if (n == NULL)
return false;
n->value = val;
return true;
}
bool ll_remove_item(linked_list_t * ll, int64_t index, ll_value_t * res)
{
if (ll == NULL)
{
errno = EINVAL;
return false;
}
ll_node_t * n;
if (!_ll_shorter_index(ll_len(ll), &index))
return false;
if (index < 0)
{
++index;
index *= -1;
n = _ll_get_node_n(ll->tail, index, false);
}
else
{
n = _ll_get_node_n(ll->head, index, true);
}
if (n == NULL)
return false;
if (n->next != NULL)
n->next->prev = n->prev;
if (n->prev != NULL)
n->prev->next = n->next;
if (n == ll->head)
ll->head = n->next;
if (n == ll->tail)
ll->tail = n->prev;
ll->size--;
if (res != NULL)
*res = n->value;
_ll_free_node(n);
return true;
}
bool ll_insert_head(linked_list_t * ll, ll_value_t val)
{
if (ll == NULL)
{
errno = EINVAL;
return false;
}
ll_node_t * n = _ll_new_node(val, NULL, ll->head);
if (n == NULL)
return false;
if (n->next != NULL)
n->next->prev = n;
ll->head = n;
if (ll->tail == NULL)
ll->tail = n;
ll->size++;
return true;
}
bool ll_insert_tail(linked_list_t * ll, ll_value_t val)
{
if (ll == NULL)
{
errno = EINVAL;
return false;
}
ll_node_t * n = _ll_new_node(val, ll->tail, NULL);
if (n == NULL)
return false;
if (n->prev != NULL)
n->prev->next = n;
ll->tail = n;
if (ll->head == NULL)
ll->head = n;
ll->size++;
return true;
}
bool ll_search(const linked_list_t * ll, ll_value_t target, int64_t * index, ll_value_t * value)
{
if (ll == NULL || ll->cmp == NULL)
{
errno = EINVAL;
return false;
}
int64_t i;
ll_node_t * n = ll->head;
for (i = 0; n != NULL; ++i, n = n->next)
{
if (ll->cmp(n->value, target) == 0)
{
if (index != NULL)
*index = i;
if (value != NULL)
*value = n->value;
return true;
}
}
return false;
}
int64_t ll_remove_values(linked_list_t * ll, ll_value_t v)
{
int64_t count = 0;
ll_node_t * n, * tmp;
if (ll == NULL || ll->cmp == NULL)
{
errno = EINVAL;
return -1;
}
for (n = ll->head; n != NULL;)
{
if (ll->cmp(n->value, v) == 0) /* n->value == v */
{
if (n->next != NULL)
n->next->prev = n->prev;
else /* is tail */
ll->tail = n->prev;
if (n->prev != NULL)
n->prev->next = n->next;
else /* is head */
ll->head = n->next;
++count;
ll->size--;
tmp = n;
n = n->next;
_ll_free_node(tmp);
}
else
n = n->next;
}
return count;
}