3 * gcc tailq.c -o tailq -Wall -W -Wextra -ansi -pedantic
11 TAILQ_HEAD(tailhead
, entry
) head
;
14 TAILQ_ENTRY(entry
) entries
;
20 const char *str
[] = { "this", "is", "a", "double", "linked", "tailq" };
26 /* Populate list with str[] items */
27 for (i
= 0; i
< sizeof str
/ sizeof *str
; i
++) {
28 if ((n
= malloc(sizeof(struct entry
))) == NULL
) {
30 goto CLEANUP_AND_EXIT
;
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
?
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
?
50 /* Delete all elements */
51 while (TAILQ_FIRST(&head
) != NULL
) {
52 np
= TAILQ_FIRST(&head
);
53 TAILQ_REMOVE(&head
, TAILQ_FIRST(&head
), entries
);