Add htable_iterator_() accessors
[eleutheria.git] / genstructs / slinkedlist.c
blob1a204b9ff2f67c62dfe3e77fee8f1e3df259d75a
1 /*
2 * Compile with:
3 * gcc slinkedlist.c -o slinkedlist -Wall -W -Wextra -ansi -pedantic
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/queue.h>
11 SLIST_HEAD(listhead, entry) head;
13 struct entry {
14 SLIST_ENTRY(entry) entries;
15 const char *str;
16 } *np, *n;
18 int main(void)
20 const char *str[] = { "this", "is", "a", "single", "linked", "list" };
21 unsigned int i;
23 /* Initialize list */
24 SLIST_INIT(&head);
26 /* Populate list with str[] items */
27 for (i = 0; i < sizeof str / sizeof *str; i++) {
28 if ((n = malloc(sizeof(struct entry))) == NULL) {
29 perror("malloc()");
30 goto CLEANUP_AND_EXIT;
32 n->str = str[i];
34 if (i == 0)
35 SLIST_INSERT_HEAD(&head, n, entries);
36 else
37 SLIST_INSERT_AFTER(np, n, entries);
38 np = n;
41 /* Traverse list */
42 SLIST_FOREACH(np, &head, entries)
43 printf("%s\n", np->str);
45 CLEANUP_AND_EXIT:;
46 /* Delete all elements */
47 while (SLIST_FIRST(&head) != NULL) {
48 np = SLIST_FIRST(&head);
49 SLIST_REMOVE_HEAD(&head, entries);
50 free(np);
53 return EXIT_SUCCESS;