2 gcc slinkedlist.c -o slinkedlist -Wall -W -Wextra -ansi -pedantic */
9 SLIST_HEAD(listhead
, entry
) head
;
12 SLIST_ENTRY(entry
) entries
;
18 const char *str
[] = { "this", "is", "a", "single", "linked", "list" };
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
;
33 SLIST_INSERT_HEAD(&head
, n
, entries
);
35 SLIST_INSERT_AFTER(np
, n
, entries
);
40 SLIST_FOREACH(np
, &head
, entries
)
41 printf("%s\n", np
->str
);
44 /* Delete all elements */
45 while (SLIST_FIRST(&head
) != NULL
) {
46 np
= SLIST_FIRST(&head
);
47 SLIST_REMOVE_HEAD(&head
, entries
);