-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.c
388 lines (332 loc) · 10.3 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* ################################################################### */
/* Copyright 2022, Pierre Gentile ([email protected]) */
/* */
/* This Source Code Form is subject to the terms of the Mozilla Public */
/* License, v. 2.0. If a copy of the MPL was not distributed with this */
/* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
/* ################################################################### */
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#include "xmalloc.h"
extern int
comp(void const *, void const *);
extern void
swap(void **, void **);
/* ********************* */
/* Linked List functions */
/* ********************* */
static ll_node_t *
ll_partition(ll_node_t *l,
ll_node_t *h,
int (*comp)(void const *, const void const *),
void (*swap)(void **, void **));
static void
ll_quicksort(ll_node_t *l,
ll_node_t *h,
int (*comp)(const void const *, const void const *),
void (*swap)(void **, void **));
/* ======================== */
/* Create a new linked list */
/* ======================== */
ll_t *
ll_new(void)
{
ll_t *ret = xmalloc(sizeof(ll_t));
ll_init(ret);
return ret;
}
/* ======================== */
/* Initialize a linked list */
/* ======================== */
void
ll_init(ll_t *list)
{
list->head = NULL;
list->tail = NULL;
list->len = 0;
}
/* ==================================================== */
/* Allocate the space for a new node in the linked list */
/* ==================================================== */
ll_node_t *
ll_new_node(void)
{
ll_node_t *ret = xmalloc(sizeof(ll_node_t));
return ret;
}
/* ==================================================================== */
/* Append a new node filled with its data at the end of the linked list */
/* The user is responsible for the memory management of the data */
/* ==================================================================== */
void
ll_append(ll_t * const list, void * const data)
{
ll_node_t *node;
node = ll_new_node(); /* ll_new_node cannot return NULL because it *
| uses xmalloc which does not return if there *
| is an allocation error. */
node->data = data;
node->next = NULL; /* This node will be the last. */
node->prev = list->tail; /* NULL if it is a new list. */
if (list->tail)
list->tail->next = node;
else
list->head = node;
list->tail = node;
++list->len; /* One more node in the list. */
}
/* =================================================================== */
/* Put a new node filled with its data at the beginning of the linked */
/* list. The user is responsible for the memory management of the data */
/* =================================================================== */
void
ll_prepend(ll_t * const list, void * const data)
{
ll_node_t *node;
node = ll_new_node(); /* ll_new_node cannot return NULL because it *
| uses xmalloc which does not return if there *
| is an allocation error. */
node->data = data;
node->prev = NULL; /* This node will be the first. */
node->next = list->head; /* NULL if it is a new list. */
if (list->head)
list->head->prev = node;
else
list->tail = node;
list->head = node;
++list->len; /* One more node in the list. */
}
/* ======================================================= */
/* Insert a new node before the specified node in the list */
/* TODO test it */
/* ======================================================= */
void
ll_insert_before(ll_t * const list, ll_node_t *node, void * const data)
{
ll_node_t *new_node;
if (list)
{
if (node->prev == NULL)
ll_prepend(list, data);
else
{
new_node = ll_new_node();
if (new_node)
{
new_node->data = data;
new_node->next = node;
new_node->prev = node->prev;
node->prev->next = new_node;
++list->len;
}
}
}
}
/* ====================================================== */
/* Insert a new node after the specified node in the list */
/* TODO test it */
/* ====================================================== */
void
ll_insert_after(ll_t * const list, ll_node_t *node, void * const data)
{
ll_node_t *new_node;
if (node->next == NULL)
ll_append(list, data);
else
{
new_node = ll_new_node();
if (new_node)
{
new_node->data = data;
new_node->prev = node;
new_node->next = node->next;
node->next->prev = new_node;
++list->len;
}
}
}
/* ================================================================= */
/* Remove a node from a linked list. */
/* The memory taken by the deleted node must be freed by the caller. */
/* ================================================================= */
int
ll_delete(ll_t * const list, ll_node_t *node)
{
if (list->head == list->tail)
{
/* We delete the last remaining element from the list. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""" */
if (list->head == NULL)
return 0;
list->head = list->tail = NULL;
}
else if (node->prev == NULL)
{
/* We delete the first element from the list. */
/* """""""""""""""""""""""""""""""""""""""""" */
list->head = node->next;
list->head->prev = NULL;
}
else if (node->next == NULL)
{
/* We delete the last element from the list. */
/* """"""""""""""""""""""""""""""""""""""""" */
list->tail = node->prev;
list->tail->next = NULL;
}
else
{
/* We delete an element from the list. */
/* """"""""""""""""""""""""""""""""""" */
node->next->prev = node->prev;
node->prev->next = node->next;
}
free(node);
--list->len; /* One less node in the list. */
return 1;
}
/* =========================================================== */
/* Creates a new list formed by the concatenation of two lists */
/* =========================================================== */
ll_t *
ll_concat(ll_t *list1, ll_t *list2)
{
ll_t *new_list = ll_new();
ll_node_t *node;
if (list1)
{
node = list1->head;
while (node)
{
ll_append(new_list, node->data);
node = node->next;
}
}
if (list2)
{
node = list2->head;
while (node)
{
ll_append(new_list, node->data);
node = node->next;
}
}
return new_list;
}
/* ====================================================== */
/* Partition code for the quicksort function */
/* Based on code found here: */
/* http://www.geeksforgeeks.org/quicksort-for-linked-list */
/* ====================================================== */
static ll_node_t *
ll_partition(ll_node_t *l,
ll_node_t *h,
int (*comp)(void const *, void const *),
void (*swap)(void **, void **))
{
/* Considers last element as pivot, places the pivot element at its */
/* correct position in sorted array, and places all smaller (smaller than */
/* pivot) to left of pivot and all greater elements to right of pivot */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
/* set pivot as h element */
/* """""""""""""""""""""" */
void *x = h->data;
ll_node_t *i = l->prev;
ll_node_t *j;
for (j = l; j != h; j = j->next)
{
if (comp(j->data, x) < 1)
{
i = (i == NULL) ? l : i->next;
swap(&(i->data), &(j->data));
}
}
i = (i == NULL) ? l : i->next;
swap(&(i->data), &(h->data));
return i;
}
/* ======================================================== */
/* A recursive implementation of quicksort for linked list. */
/* Based on code found here: */
/* http://www.geeksforgeeks.org/quicksort-for-linked-list */
/* ======================================================== */
static void
ll_quicksort(ll_node_t *l,
ll_node_t *h,
int (*comp)(void const *, void const *),
void (*swap)(void **, void **))
{
if (h != NULL && l != h && l != h->next)
{
ll_node_t *p = ll_partition(l, h, comp, swap);
ll_quicksort(l, p->prev, comp, swap);
ll_quicksort(p->next, h, comp, swap);
}
}
/* =========================== */
/* A linked list sort function */
/* =========================== */
void
ll_sort(ll_t *list,
int (*comp)(void const *, void const *),
void (*swap)(void **, void **))
{
/* Call the recursive ll_quicksort function */
/* """""""""""""""""""""""""""""""""""""""" */
ll_quicksort(list->head, list->tail, comp, swap);
}
/* ======================================================================== */
/* Find a node in the list containing data. Return the node pointer or NULL */
/* if not found. */
/* A comparison function must be provided to compare a and b (strcmp like). */
/* ======================================================================== */
ll_node_t *
ll_find(ll_t * const list,
void const *data,
int (*cmpfunc)(void const *a, void const *b))
{
ll_node_t *node;
if (NULL == (node = list->head))
return NULL;
do
{
if (0 == cmpfunc(node->data, data))
return node;
} while (NULL != (node = node->next));
return NULL;
}
/* =============================================== */
/* Free all the elements of a list (make it empty) */
/* NULL or a custom function may be used to free */
/* the sub components of the elements. */
/* =============================================== */
void
ll_free(ll_t * const list, void (*clean)(void *))
{
if (list)
{
ll_node_t *node = list->head;
while (node)
{
/* Apply a custom cleaner if not NULL. */
/* """"""""""""""""""""""""""""""""""" */
if (clean)
clean(node->data);
ll_delete(list, node);
node = list->head;
}
}
}
/* ==================================== */
/* Destroy a list and all its elements. */
/* ==================================== */
void
ll_destroy(ll_t *list, void (*clean)(void *))
{
if (list)
{
ll_free(list, clean);
free(list);
}
}