From: Alejandro Colomar Date: Tue, 20 Oct 2020 21:31:03 +0000 (+0200) Subject: list.3, queue.3: EXAMPLES: Move example program from queue.3 to list.3 X-Git-Tag: man-pages-5.09~138 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=be7973e6b16bc130ceb5222d8abe4516661af688;p=thirdparty%2Fman-pages.git list.3, queue.3: EXAMPLES: Move example program from queue.3 to list.3 Signed-off-by: Alejandro Colomar Signed-off-by: Michael Kerrisk --- diff --git a/man3/list.3 b/man3/list.3 index 1a197fe319..6252aaf6a7 100644 --- a/man3/list.3 +++ b/man3/list.3 @@ -221,4 +221,57 @@ See the EXAMPLES section below for an example program using a linked list. .SH CONFORMING TO .SH BUGS .SH EXAMPLES +.Ss List example +.Bd -literal +#include +#include +#include +#include + +struct entry { + int data; + LIST_ENTRY(entry) entries; /* List. */ +}; + +LIST_HEAD(listhead, entry); + +int +main(void) +{ + struct entry *n1, *n2, *n3, *np; + struct listhead head; /* List head. */ + int i; + + LIST_INIT(&head); /* Initialize the list. */ + + n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ + LIST_INSERT_HEAD(&head, n1, entries); + + n2 = malloc(sizeof(struct entry)); /* Insert after. */ + LIST_INSERT_AFTER(n1, n2, entries); + + n3 = malloc(sizeof(struct entry)); /* Insert before. */ + LIST_INSERT_BEFORE(n2, n3, entries); + + i = 0; /* Forward traversal. */ + LIST_FOREACH(np, &head, entries) + np->data = i++; + + LIST_REMOVE(n2, entries); /* Deletion. */ + free(n2); + /* Forward traversal. */ + LIST_FOREACH(np, &head, entries) + printf("%i\en", np->data); + /* List Deletion. */ + n1 = LIST_FIRST(&head); + while (n1 != NULL) { + n2 = LIST_NEXT(n1, entries); + free(n1); + n1 = n2; + } + LIST_INIT(&head); + + exit(EXIT_SUCCESS); +} +.Ed .SH SEE ALSO diff --git a/man3/queue.3 b/man3/queue.3 index 6ee793e256..0f55a899ec 100644 --- a/man3/queue.3 +++ b/man3/queue.3 @@ -1155,59 +1155,6 @@ main(void) exit(EXIT_SUCCESS); } .Ed -.Ss List example -.Bd -literal -#include -#include -#include -#include - -struct entry { - int data; - LIST_ENTRY(entry) entries; /* List. */ -}; - -LIST_HEAD(listhead, entry); - -int -main(void) -{ - struct entry *n1, *n2, *n3, *np; - struct listhead head; /* List head. */ - int i; - - LIST_INIT(&head); /* Initialize the list. */ - - n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ - LIST_INSERT_HEAD(&head, n1, entries); - - n2 = malloc(sizeof(struct entry)); /* Insert after. */ - LIST_INSERT_AFTER(n1, n2, entries); - - n3 = malloc(sizeof(struct entry)); /* Insert before. */ - LIST_INSERT_BEFORE(n2, n3, entries); - - i = 0; /* Forward traversal. */ - LIST_FOREACH(np, &head, entries) - np->data = i++; - - LIST_REMOVE(n2, entries); /* Deletion. */ - free(n2); - /* Forward traversal. */ - LIST_FOREACH(np, &head, entries) - printf("%i\en", np->data); - /* List Deletion. */ - n1 = LIST_FIRST(&head); - while (n1 != NULL) { - n2 = LIST_NEXT(n1, entries); - free(n1); - n1 = n2; - } - LIST_INIT(&head); - - exit(EXIT_SUCCESS); -} -.Ed .Ss Tail queue example .Bd -literal #include