list snippet

more_to_read
Philippe PITTOLI 2016-06-05 19:06:28 +02:00
parent 999f858f31
commit 1c3800bbc3
2 changed files with 54 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
*.a
*.so
*.swp
*.out

53
misc/list.c Normal file
View File

@ -0,0 +1,53 @@
#include <stdio.h>
#include <stdlib.h>
#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;
}