summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/linkedlists/intrusive.c
blob: edb072c71ba324df0b4c1124644e2ca49d083382 (plain)
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
// Example of clist using the node API.

#include <stdio.h> 

#define i_type List
#define i_key int
#define i_use_cmp
#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, {.value=*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);
}