2 gcc tailq.c -o tailq -Wall -W -Wextra -ansi -pedantic */
9 TAILQ_HEAD(tailhead
, entry
) head
;
12 TAILQ_ENTRY(entry
) entries
;
18 const char *str
[] = { "this", "is", "a", "double", "linked", "tailq" };
24 /* Populate list with str[] items */
25 for (i
= 0; i
< sizeof str
/ sizeof *str
; i
++) {
26 if ((n
= malloc(sizeof(struct entry
))) == NULL
) {
28 goto CLEANUP_AND_EXIT
;
32 TAILQ_INSERT_TAIL(&head
, n
, entries
);
35 /* Traverse list forward */
36 TAILQ_FOREACH(np
, &head
, entries
)
37 printf("%s %s", np
->str
,
38 TAILQ_NEXT(np
, entries
) != NULL
?
41 /* Traverse list in reverse order */
42 TAILQ_FOREACH_REVERSE(np
, &head
, tailhead
, entries
)
43 printf("%s %s", np
->str
,
44 TAILQ_PREV(np
, tailhead
, entries
) != NULL
?
48 /* Delete all elements */
49 while (TAILQ_FIRST(&head
) != NULL
) {
50 np
= TAILQ_FIRST(&head
);
51 TAILQ_REMOVE(&head
, TAILQ_FIRST(&head
), entries
);