-
Notifications
You must be signed in to change notification settings - Fork 5
/
list.c
258 lines (211 loc) · 5.15 KB
/
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
/*
* See list.h for more info
*
* This is free and unencumbered software released into the public domain.
* https://unlicense.org/
*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "list.h"
/*****************************************************************************/
link_list *list_create()
{
link_list *list;
list = malloc(sizeof *list);
if(!list)
return NULL;
list->first = NULL;
list->last = NULL;
return list;
}
/*****************************************************************************/
void list_destroy(link_list *list, list_dtor dtor)
{
while(list->first) {
list_element *next = list->first->next;
if(dtor) dtor(list->first->data);
free(list->first);
list->first = next;
}
free(list);
}
/*****************************************************************************/
list_element *list_append(link_list *list, void *data)
{
list_element *el;
if(!data) return NULL;
el = malloc(sizeof *el);
if(!el)
return NULL;
el->next = NULL;
if(!list->first)
{
assert(list->last == NULL);
list->first = el;
el->prev = NULL;
}
else
{
assert(list->last->next == NULL);
list->last->next = el;
el->prev = list->last;
}
list->last = el;
el->data = data;
return el;
}
/*****************************************************************************/
list_element *list_prepend(link_list *list, void *data)
{
list_element *el;
if(!data) return NULL;
el = malloc(sizeof *el);
el->prev = NULL;
if(!list->last)
{
assert(list->first == NULL);
list->last = el;
el->next = NULL;
}
else
{
assert(list->first->prev == NULL);
list->first->prev = el;
el->next = list->first;
}
list->first = el;
el->data = data;
return el;
}
/*****************************************************************************/
int list_iterate(link_list *list, list_iter iter)
{
list_element *le;
if(!list || !iter) return 1;
for(le = list->first; le; le = le->next)
if(!iter(le->data))
return 0;
return 1;
}
/*****************************************************************************/
int list_iterate_reverse(link_list *list, list_iter iter)
{
list_element *le;
if(!list || !iter) return 1;
for(le = list->last; le; le = le->prev)
if(!iter(le->data))
return 0;
return 1;
}
/*****************************************************************************/
list_element *list_find(link_list *list, void *data, list_comp comp)
{
list_element *le;
if(!list || !data || !comp) return NULL;
for(le = list->first; le; le = le->next)
if(comp(le->data, data))
return le;
return NULL;
}
/*****************************************************************************/
void *list_remove_element(link_list *list, list_element *el)
{
void *d = el->data;
assert(list);
if(!el) return NULL;
if(!el->prev)
{
assert(list->first == el);
if(!el->next)
{
assert(list->last == el);
list->first = NULL;
list->last = NULL;
}
else
{
list->first = el->next;
el->next->prev = NULL;
}
}
else
{
if(!el->next)
{
assert(list->last == el);
list->last = el->prev;
el->prev->next = NULL;
}
else
{
el->next->prev = el->prev;
el->prev->next = el->next;
}
}
free(el);
return d;
}
/*****************************************************************************/
void *list_remove(link_list *list, void *data, list_comp comp)
{
list_element *le;
if(!list || !data || !comp) return NULL;
for(le = list->first; le; le = le->next)
if(comp(le->data, data))
return list_remove_element(list, le);
return NULL;
}
/*****************************************************************************/
int list_strcmp(void *p, void *q)
{
char *s1 = p, *s2 = q;
return !strcmp(s1, s2);
}
/*****************************************************************************/
int list_stricmp(void *p, void *q)
{
char *s1 = p, *s2 = q;
for(;s1[0] && s2[0] && tolower(s1[0]) == tolower(s2[0]); s1++, s2++);
return tolower(s1[0]) == tolower(s2[0]);
}
/*****************************************************************************/
void *list_pop_front(link_list *list)
{
assert(list);
if(!list->first)
{
assert(!list->last);
return NULL;
}
return list_remove_element(list, list->first);
}
/*****************************************************************************/
void *list_pop_back(link_list *list)
{
assert(list);
if(!list->last)
{
assert(!list->first);
return NULL;
}
return list_remove_element(list, list->last);
}
/*****************************************************************************/
int list_isempty(link_list *list)
{
assert(list);
return !list->first;
}
/*****************************************************************************/
int list_count(link_list *list)
{
list_element *le;
int count = 0;
assert(list);
for(le = list->first; le; le = le->next)
count++;
return count;
}
/*****************************************************************************/