diff options
Diffstat (limited to 'misc/examples/linkedlists/intrusive.c')
| -rw-r--r-- | misc/examples/linkedlists/intrusive.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/misc/examples/linkedlists/intrusive.c b/misc/examples/linkedlists/intrusive.c new file mode 100644 index 00000000..c22ed260 --- /dev/null +++ b/misc/examples/linkedlists/intrusive.c @@ -0,0 +1,33 @@ +// Example of clist using the node API. + +#include <stdio.h> + +#define i_type List +#define i_key int +#define i_cmp_native +#include <stc/clist.h> + +void printList(List list) { + printf("list:"); + c_foreach (i, List, list) + printf(" %d", *i.ref); + puts(""); +} + +int main(void) { + List list = {0}; + c_forlist (i, int, {6, 9, 3, 1, 7, 4, 5, 2, 8}) + List_push_back_node(&list, c_new(List_node, {0, *i.ref})); + + printList(list); + + puts("Sort list"); + List_sort(&list); + printList(list); + + puts("Remove nodes from list"); + while (!List_empty(&list)) + c_free(List_unlink_after_node(&list, list.last)); + + printList(list); +} |
