3 * gcc slinkedlist.c -o slinkedlist -Wall -W -Wextra -ansi -pedantic
11 SLIST_HEAD(listhead
, entry
) head
;
14 SLIST_ENTRY(entry
) entries
;
20 const char *str
[] = { "this", "is", "a", "single", "linked", "list" };
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
;
35 SLIST_INSERT_HEAD(&head
, n
, entries
);
37 SLIST_INSERT_AFTER(np
, n
, entries
);
42 SLIST_FOREACH(np
, &head
, entries
)
43 printf("%s\n", np
->str
);
46 /* Delete all elements */
47 while (SLIST_FIRST(&head
) != NULL
) {
48 np
= SLIST_FIRST(&head
);
49 SLIST_REMOVE_HEAD(&head
, entries
);