-
Notifications
You must be signed in to change notification settings - Fork 0
/
slist.h
61 lines (49 loc) · 1.07 KB
/
slist.h
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
#ifndef SLIST_H
#define SLIST_H
#define SLIST_FOREACH(l) \
for (; (l); (l) = (l)->next)
struct slist {
struct slist *next;
};
typedef int (*slist_cmp)(const struct slist *p1, const struct slist *p2);
/* Add element sorted based on the slist_cmp function */
static inline void slist_add(struct slist *head, struct slist *n, slist_cmp fn)
{
struct slist **t = &head;
while (*t) {
if (fn(n, *t) < 0) {
n->next = *t;
break;
}
t = &(*t)->next;
}
*t = n;
}
static inline void slist_append(struct slist *head, struct slist *n)
{
struct slist **t = &head;
while (*t) {
t = &(*t)->next;
}
*t = n;
}
static inline void slist_remove(struct slist *head, struct slist *n)
{
struct slist **t = &head;
while (*t) {
if (*t == n) {
*t = (*t)->next;
}
t = &(*t)->next;
}
}
static inline unsigned int slist_size(struct slist *head)
{
unsigned int size = 0;
while (head) {
size++;
head = head->next;
}
return size;
}
#endif