-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.c
260 lines (217 loc) · 6.51 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
259
260
/********************************************************************
*
* $Id: List.c 2135 2019-09-16 07:22:19Z phil $
*
********************************************************************/
/**
*
* Creation of a generic (simply linked) List structure.
*
* To create a list, one must provide two functions (one function to
* compare / order elements, one function to display them). Unlike arrays,
* indices begins with 1.
*
********************************************************************/
#include <stdio.h>
#include "List.h"
/** stores the list of available Nodes instead of deallocating / reallocating
* them all the time */
static Node *available = 0;
/* create a new, empty list */
List *newList(compFun comp, prFun pr) {
List *l;
l = (List *) malloc(sizeof(List));
if (!l) return 0;
l->nelts = 0;
l->head = 0;
l->comp = comp;
l->pr = pr;
return l;
}
/* destroy the list by deallocating used memory */
void delList(List *l) {
Node *tmp = l->head;
while (tmp) {
l->head = tmp->next;
tmp->next = available;
available = tmp;
tmp = l->head;
}
/* tmp == 0 : list is empty */
l->head = 0;
l->nelts = 0;
free(l);
}
/* return the Nth element of the list if exists */
status nthInList(List *l, int pos, void *res) {
Node *tmp = l->head;
if (pos <= 0 || pos > l->nelts)
return ERRINDEX;
while (pos-- > 1) tmp = tmp->next;
*(void **) res = tmp->val;
return OK;
}
/* Insert element at a given position in the list */
status addListAt(List *l, int pos, void *elt) {
if (pos <= 0 || pos > l->nelts + 1)
return ERRINDEX;
/* get a new Node and increment length */
Node *toAdd = available;
if (!toAdd) toAdd = (Node *) malloc(sizeof(Node));
else available = available->next;
if (!toAdd) return ERRALLOC;
l->nelts++;
toAdd->val = elt;
/* if pos is 1, must change list head */
if (pos == 1) {
toAdd->next = l->head;
l->head = toAdd;
}
/* otherwise we need a temporary pointer */
else {
Node *tmp = l->head;
/* tmp points to the predecessor */
while (pos-- > 2) tmp = tmp->next;
/* actual insertion */
toAdd->next = tmp->next;
tmp->next = toAdd;
}
return OK;
}
/* remove the element located at a given position in list */
status remFromListAt(List *l, int pos, void *res) {
Node *toRem = l->head;
if (pos <= 0 || pos > l->nelts) return ERRINDEX;
/* particular case: must remove list head */
if (pos == 1)
l->head = toRem->next;
/* otherwise point to predecessor to change links */
else {
Node *prec = toRem;
while (pos-- > 2) prec = prec->next;
toRem = prec->next;
prec->next = toRem->next;
}
*(void **) res = toRem->val;
toRem->next = available;
available = toRem;
l->nelts--;
return OK;
}
/* remove given element from given list */
status remFromList(List *l, void *elt) {
Node *prec = l->head, *toRem = 0;
if (l->comp == 0) return ERRUNABLE;
if (l->nelts == 0) return ERRABSENT;
if (!(l->comp)(elt, prec->val)) return remFromListAt(l, 1, &(prec->val));
/* points to the predecessor */
while (prec->next && (l->comp)(elt, prec->next->val)) {
prec = prec->next;
}
/* here
* - either we point to the last element
* - or next one is the one to remove!
*/
if (!prec->next) return ERRABSENT;
toRem = prec->next;
prec->next = toRem->next;
toRem->next = available;
available = toRem;
l->nelts--;
return OK;
}
/* display list elements as "[ e1 ... eN ]" */
status displayList(List *l) {
Node *tmp = l->head;
if (l->pr == 0) return ERRUNABLE;
printf("[ ");
while (tmp) {
(l->pr)(tmp->val);
putchar(' ');
tmp = tmp->next;
}
putchar(']');
return OK;
}
/* sequencially call given function with each element of given list */
void forEach(List *l, void(*f)(void *)) {
Node *tmp = l->head;
while (tmp) {
(*f)(tmp->val);
tmp = tmp->next;
}
}
/* compute and return the number of elements in given list */
int lengthList(List *l) { return l->nelts; }
/** private function to get the node preceding the one we're looking for
* @param e the searched element
* @return 0 if element is not found (typically, list is empty)
* @return 1 if element is head of list
* @return a pointer to the predecessor otherwise
*/
static Node *previous(List *l, void *e) {
Node *prec = l->head;
if (l->nelts == 0) return 0;
if (!(l->comp)(e, prec->val)) return (Node *) 1;
/* prec must address element prior to the one to remove */
while (prec->next && (l->comp)(e, prec->next->val))
prec = prec->next;
/* here,
* - either we address nothing (no such element)
* - or we address the element prior to the one we're looking for
*/
return prec;
}
/* add given element to given list according to compFun function */
status addList(List *l, void *e) {
Node *prec = l->head, *toAdd;
if (l->comp == 0) return ERRUNABLE;
/* add to the head if list is empty, if no comparison function is given
* or if e < head element
*/
if (l->nelts == 0 || (l->comp)(e, l->head->val) < 0)
return addListAt(l, 1, e);
/* otherwise, get predecessor and link new element just after it */
while (prec && prec->next && (l->comp)(prec->next->val, e) < 0)
prec = prec->next;
toAdd = available;
if (!toAdd) toAdd = (Node *) malloc(sizeof(Node));
else available = available->next;
if (!toAdd) return ERRALLOC;
toAdd->next = prec->next;
toAdd->val = e;
prec->next = toAdd;
l->nelts++;
return OK;
}
/* test whether the list contains given element */
Node *isInList(List *l, void *e) {
Node *prec = previous(l, e);
if (!prec) return 0;
if (prec == (Node *) 1) return (Node *) 1;
if (prec->next == 0) return 0;
if (!(l->comp)(e, prec->next->val))
return prec;
return 0;
}
/* return the first element in l that matches f, or 0 */
void *firstThat(List *l, int(*f)(void *)) {
Node *n = l->head;
while (n) {
if (f(n->val))
return n->val;
n = n->next;
}
return 0;
}
/* return the list of all elements in l that match f */
List *allThat(List *l, int(*f)(void *)) {
List *res = newList(l->comp, l->pr);
Node *n = l->head;
while (n) {
if (f(n->val))
addListAt(res, res->nelts + 1, n->val);
n = n->next;
}
return res;
}