From 1c3800bbc343f614f5d4fb89044b3794e4ea3f14 Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Sun, 5 Jun 2016 19:06:28 +0200 Subject: [PATCH] list snippet --- .gitignore | 1 + misc/list.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 misc/list.c diff --git a/.gitignore b/.gitignore index 8020363..b55eb49 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.a *.so *.swp +*.out diff --git a/misc/list.c b/misc/list.c new file mode 100644 index 0000000..aea2341 --- /dev/null +++ b/misc/list.c @@ -0,0 +1,53 @@ +#include +#include +#include "../lib/queue.h" + +// create the head of the list +LIST_HEAD(mlist, node); + +// elements structure of the list +struct node { + int content; + LIST_ENTRY(node) entries; +}; + +int main(int argc, char * argv[]) +{ + // the list + struct mlist *list; + list = malloc (sizeof(struct mlist)); + LIST_INIT(list); + + // create the elements + struct node *n1 = malloc (sizeof(struct node)); + n1->content = 10; + struct node *n2 = malloc (sizeof(struct node)); + n2->content = 20; + + // insert element into the list + LIST_INSERT_HEAD(list, n1, entries); + LIST_INSERT_HEAD(list, n2, entries); + + // loop over the list + struct node *np = NULL; + LIST_FOREACH(np, list, entries) { + printf ("elem : %d\n", np->content); + } + + // remove elements from the list + LIST_REMOVE(n1, entries); + LIST_REMOVE(n2, entries); + + // to be sure that nothing still is into the list + np = NULL; + LIST_FOREACH(np, list, entries) { + printf ("\033[31mSHOULD NOT BE PRINTED : %d\033[00m\n", np->content); + } + + // free the elements then the list itself + free (n1); + free (n2); + free (list); + + return EXIT_SUCCESS; +}