Mercurial > hg > toybox
comparison lib/llist.c @ 624:1e8b9acdafeb
Genericize llist code a bit: rename llist_free() to llist_traverse(), and no longer accept NULL as a synonym for free.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 15 Jul 2012 17:22:04 -0500 |
parents | c2f39708a4c4 |
children | 786841fdb1e0 |
comparison
equal
deleted
inserted
replaced
623:f51beec92738 | 624:1e8b9acdafeb |
---|---|
4 * Linked list structures have a next pointer as their first element. | 4 * Linked list structures have a next pointer as their first element. |
5 */ | 5 */ |
6 | 6 |
7 #include "toys.h" | 7 #include "toys.h" |
8 | 8 |
9 // Free all the elements of a linked list | 9 // Call a function (such as free()) on each element of a linked list. |
10 // if freeit!=NULL call freeit() on each element before freeing it. | 10 void llist_traverse(void *list, void (*using)(void *data)) |
11 | |
12 void llist_free(void *list, void (*freeit)(void *data)) | |
13 { | 11 { |
14 while (list) { | 12 while (list) { |
15 void *pop = llist_pop(&list); | 13 void *pop = llist_pop(&list); |
16 if (freeit) freeit(pop); | 14 using(pop); |
17 else free(pop); | |
18 | 15 |
19 // End doubly linked list too. | 16 // End doubly linked list too. |
20 if (list==pop) break; | 17 if (list==pop) break; |
21 } | 18 } |
22 } | 19 } |