Rename htable_delete() to htable_remove()
[eleutheria.git] / genstructs / slinkedlist.c
blob167bd7da37d31685d44673a279a2379c05d8dfd6
1 /* compile with:
2 gcc slinkedlist.c -o slinkedlist -Wall -W -Wextra -ansi -pedantic */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/queue.h>
9 SLIST_HEAD(listhead, entry) head;
11 struct entry {
12 SLIST_ENTRY(entry) entries;
13 const char *str;
14 } *np, *n;
16 int main(void)
18 const char *str[] = { "this", "is", "a", "single", "linked", "list" };
19 unsigned int i;
21 /* Initialize list */
22 SLIST_INIT(&head);
24 /* Populate list with str[] items */
25 for (i = 0; i < sizeof str / sizeof *str; i++) {
26 if ((n = malloc(sizeof(struct entry))) == NULL) {
27 perror("malloc");
28 goto CLEANUP_AND_EXIT;
30 n->str = str[i];
32 if (i == 0)
33 SLIST_INSERT_HEAD(&head, n, entries);
34 else
35 SLIST_INSERT_AFTER(np, n, entries);
36 np = n;
39 /* Traverse list */
40 SLIST_FOREACH(np, &head, entries)
41 printf("%s\n", np->str);
43 CLEANUP_AND_EXIT:;
44 /* Delete all elements */
45 while (SLIST_FIRST(&head) != NULL) {
46 np = SLIST_FIRST(&head);
47 SLIST_REMOVE_HEAD(&head, entries);
48 free(np);
51 return EXIT_SUCCESS;