]> mj.ucw.cz Git - libucw.git/blob - lib/slists.c
avoid compiler warnings with debugging turned off
[libucw.git] / lib / slists.c
1 /*
2  *      UCW Library -- Single-Linked Lists
3  *
4  *      (c) 2005 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/slists.h"
12
13 static inline snode *
14 slist_raw_prev(slist *l, snode *n)
15 {
16   snode *m = &l->head;
17   while (m)
18     {
19       if (n == m->next)
20         return m;
21       m = m->next;
22     }
23   ASSERT(0);
24   return NULL;
25 }
26
27 void *
28 slist_prev(slist *l, snode *n)
29 {
30   snode *p = slist_raw_prev(l, n);
31   return (p == &l->head) ? NULL : p;
32 }
33
34 void
35 slist_insert_before(slist *l, snode *what, snode *before)
36 {
37   what->next = before;
38   slist_raw_prev(l, before)->next = what;
39 }
40
41 void
42 slist_remove(slist *l, snode *n)
43 {
44   snode *p = slist_raw_prev(l, n);
45   slist_remove_after(l, p);
46 }
47
48 #ifdef TEST
49
50 #include <stdio.h>
51 #include <alloca.h>
52
53 int main(void)
54 {
55   slist l;
56
57   struct x {
58     snode n;
59     int val;
60   };
61
62   slist_init(&l);
63   for (int i=1; i<=10; i++)
64     {
65       struct x *x = alloca(sizeof(*x));
66       x->val = i;
67       if (i % 2)
68         slist_add_head(&l, &x->n);
69       else
70         slist_add_tail(&l, &x->n);
71     }
72
73   struct x *x, *prev;
74   SLIST_WALK_DELSAFE(x, l, prev)
75     if (x->val == 5)
76       slist_remove_after(&l, &prev->n);
77     else if (x->val == 6)
78       slist_remove(&l, &x->n);
79   SLIST_FOR_EACH(struct x *, x, l)
80     printf("%d/", x->val);
81   putchar('\n');
82 }
83
84 #endif