Dummy commit to test new ssh key
[eleutheria.git] / genstructs / tailq.c
blob508c68cdac77dd4a4b9dd5b7fb5c1e3c0e7fe599
1 /*
2 * Compile with:
3 * gcc tailq.c -o tailq -Wall -W -Wextra -ansi -pedantic
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/queue.h>
11 TAILQ_HEAD(tailhead, entry) head;
13 struct entry {
14 TAILQ_ENTRY(entry) entries;
15 const char *str;
16 } *np, *n;
18 int main(void)
20 const char *str[] = { "this", "is", "a", "double", "linked", "tailq" };
21 unsigned int i;
23 /* Initialize list */
24 TAILQ_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 TAILQ_INSERT_TAIL(&head, n, entries);
37 /* Traverse list forward */
38 TAILQ_FOREACH(np, &head, entries)
39 printf("%s %s", np->str,
40 TAILQ_NEXT(np, entries) != NULL ?
41 "-> " : "\n");
43 /* Traverse list in reverse order */
44 TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
45 printf("%s %s", np->str,
46 TAILQ_PREV(np, tailhead, entries) != NULL ?
47 "-> " : "\n");
49 CLEANUP_AND_EXIT:;
50 /* Delete all elements */
51 while (TAILQ_FIRST(&head) != NULL) {
52 np = TAILQ_FIRST(&head);
53 TAILQ_REMOVE(&head, TAILQ_FIRST(&head), entries);
54 free(np);
57 return EXIT_SUCCESS;