-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathllist.c
88 lines (77 loc) · 1.62 KB
/
llist.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
/*
* 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 http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2018, Joyent, Inc.
*/
#include "llist.h"
void
llist_prepend(llist_t *list, void *data)
{
llist_t *head, *tail;
assert(list != NULL);
head = list->ll_next;
tail = list->ll_prev;
if (head == NULL) {
/* empty list */
list->ll_next = list->ll_prev = (llist_t *)data;
} else {
((llist_t *)data)->ll_next = head;
head->ll_prev = (llist_t *)data;
list->ll_next = (llist_t *)data;
}
}
void
llist_append(llist_t *list, void *data)
{
llist_t *head, *tail;
assert(list != NULL);
head = list->ll_next;
tail = list->ll_prev;
if (head == NULL) {
/* empty list */
list->ll_next = list->ll_prev = (llist_t *)data;
} else {
((llist_t *)data)->ll_prev = tail;
tail->ll_next = (llist_t *)data;
list->ll_prev = (llist_t *)data;
}
}
int
llist_walker(llist_t *list, int (*cbfunc)(llist_t *, void *), void *arg)
{
llist_t *node;
int ret;
assert(list != NULL);
node = list->ll_next;
while (node != NULL) {
ret = cbfunc(node, arg);
if (ret == LL_WALK_DONE) {
break;
} else if (ret == LL_WALK_ERR) {
return (-1);
} else {
node = node->ll_next;
}
}
return (0);
}
int
llist_remove(llist_t *list, void *data)
{
return (0);
}
void
llist_destroy(llist_t *list, void (*cbfunc)(llist_t *, void *), void *arg)
{
llist_t *node, *victim;
assert(list != NULL);
node = list->ll_next;
while (node != NULL) {
/* destroy node */
victim = node;
node = node->ll_next;
cbfunc(victim, arg);
}
}