list.c
3.92 KB
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
/* "generic" lists */
#include <stdlib.h>
#include "list.h"
#if 01
typedef struct node *node;
struct _list {
node head, tail;
node state; // allows one iterator per list
};
struct node {
void *element;
node next;
};
#endif
fd_list fd_list_new()
{
fd_list l = malloc(sizeof(struct _list));
if (l)
l->head = l->tail = 0;
return l;
}
int fd_list_empty(fd_list list)
{
return list->head == 0;
}
/* insert ELEMENT at the head of LIST */
int fd_list_insert(fd_list list, void *element)
{
node n = malloc(sizeof(struct node));
if (n == 0)
return 0;
n->element = element;
n->next = list->head;
list->head = n;
if (list->tail == 0)
list->tail = n;
return 1;
}
/* append ELEMENT to LIST */
int fd_list_append(fd_list list, void *element)
{
node n = malloc(sizeof(struct node)); // XXX: check for NULL
if (n == 0)
return 0;
n->element = element;
n->next = 0;
if (list->tail == 0)
list->head = n;
else
list->tail->next = n;
list->tail = n;
return 1;
}
/* insert ELEMENT in LIST, sorted according to COMPARE, without repetitions */
int fd_list_sorted_insert(fd_list list, void *element, int (*compare)(void *, void *))
{
node new, prev, cur;
int cmp;
cur = list->head;
while (cur && (cmp = compare(cur->element, element)) < 0)
{
prev = cur;
cur = cur->next;
}
if (cur && cmp == 0)
return 0;
new = malloc(sizeof(struct node)); // XXX: check for NULL
new->next = cur;
new->element = element;
if (cur == list->head)
{
list->head = new;
if (list->tail == 0)
list->tail = new;
}
else
prev->next = new;
return 1;
}
/* remove and return the first element from LIST */
void *fd_list_remove(fd_list list)
{
void *element;
node first;
if (list->head == 0)
return 0;
first = list->head;
element = first->element;
list->head = first->next;
free(first);
if (list->head == 0)
list->tail = 0;
return element;
}
/* return the first element from LIST */
void *fd_list_head(fd_list list)
{
return (list->head == 0) ? 0 : list->head->element;
}
/* return the last element from LIST */
void *fd_list_tail(fd_list list)
{
return (list->head == 0) ? 0 : list->tail->element;
}
/* iterator (one per list) */
void fd_list_iterate(fd_list list)
{
list->state = list->head;
}
int fd_list_has_next(fd_list list)
{
return list->state != 0;
}
void *fd_list_next_element(fd_list list)
{
void *element = list->state->element;
list->state = list->state->next;
return element;
}
/* return an iterator for LIST */
fd_list_iterator fd_list_iterate2(fd_list list)
{
return list->head;
}
int fd_list_has_next2(fd_list_iterator iterator)
{
return iterator != 0;
}
void *fd_list_next_element2(fd_list_iterator *iterator)
{
void *element = (*iterator)->element;
*iterator = (*iterator)->next;
return element;
}
/* free the space occupied by LIST */
void fd_list_dispose(fd_list list)
{
struct node *n, *m;
n = list->head;
while (n)
{
m = n->next;
free(n);
n = m;
}
free(list);
}
/* free the space occupied by LIST and by its elements */
void fd_list_dispose_deep(fd_list list, void (*free_element)(void *))
{
struct node *n, *m;
n = list->head;
while (n)
{
free_element(n->element);
m = n->next;
free(n);
n = m;
}
free(list);
}
/* empty LIST */
void fd_list_empty_list(fd_list list)
{
struct node *n;
while (n = list->head)
{
list->head = n->next;
free(n);
}
list->tail = 0;
}
/* empty LIST and free the space occupied by its elements */
void fd_list_empty_list_deep(fd_list list, void (*free_element)(void *))
{
struct node *n;
while (n = list->head)
{
free_element(n->element);
list->head = n->next;
free(n);
}
list->tail = 0;
}
// XXX: not to be used
int fd_list_length(fd_list list)
{
struct node *n;
int l = 0;
for (n = list->head; n; n = n->next)
l++;
return l;
}