Set file permissions to 755
[eleutheria.git] / netbsd-llist.c
blob9e6d012ad1d21ba7d72563e5ecc91dfd5bf49b60
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/queue.h>
6 LIST_HEAD(listhead, entry) head;
7 struct listhead *headp;
9 struct entry {
10 LIST_ENTRY(entry) entries;
11 const char *str;
12 } *np, *n;
14 int main(void)
16 const char *str[] = { "this", "is", "a", "linked", "list" };
17 unsigned int i;
19 /* Initialize list */
20 LIST_INIT(&head);
21 headp = &head;
23 /* Populate list with str[] items */
24 for (i = 0; i < sizeof str / sizeof *str; i++) {
25 if ((n = malloc(sizeof(struct entry))) == NULL) {
26 perror("malloc");
27 goto CLEANUP_AND_EXIT;
29 n->str = str[i];
31 if (i == 0)
32 LIST_INSERT_HEAD(&head, n, entries);
33 else
34 LIST_INSERT_AFTER(np, n, entries);
35 np = n;
38 /* Traverse list */
39 LIST_FOREACH(np, &head, entries)
40 printf("%s\n", np->str);
42 CLEANUP_AND_EXIT:;
43 /* Delete all elements */
44 while (LIST_FIRST(&head) != NULL)
45 LIST_REMOVE(LIST_FIRST(&head), entries);
47 return EXIT_SUCCESS;