Initial import of netbsd-llist.c file
[eleutheria.git] / netbsd-llist.c
blobeafc9b76c67bd692f3d5de6e0dfd816a28b2c5a5
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/queue.h>
5 #include <assert.h>
7 LIST_HEAD(listhead, entry) head;
8 struct listhead *headp;
10 struct entry {
11 LIST_ENTRY(entry) entries;
12 const char *str;
13 } *np, *n;
15 void diep(const char *s);
17 int main(void)
19 const char *str[] = { "this", "is", "a", "linked", "list" };
20 unsigned int i;
22 /* Initialize list */
23 LIST_INIT(&head);
24 headp = &head;
26 /* Populate list with str[] items */
27 for (i = 0; i < sizeof str / sizeof *str; i++) {
28 n = malloc(sizeof(struct entry));
29 if (n == NULL)
30 diep("malloc");
31 n->str = str[i];
33 if (i == 0)
34 LIST_INSERT_HEAD(&head, n, entries);
35 else
36 LIST_INSERT_AFTER(np, n, entries);
37 np = n;
40 /* Traverse list */
41 LIST_FOREACH(np, &head, entries)
42 printf("%s\n", np->str);
44 /* Delete all elements */
45 while (LIST_FIRST(&head) != NULL)
46 LIST_REMOVE(LIST_FIRST(&head), entries);
48 return EXIT_SUCCESS;
51 void diep(const char *s)
53 perror(s);
54 exit(EXIT_FAILURE);