From d47b080534635e8d3fcc698a56ff204cb4e6ada8 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sun, 25 Oct 2020 00:21:13 +0200 Subject: [PATCH] queue.3, stailq.3: EXAMPLES: Move stailq example from queue.3 to stailq.3 Signed-off-by: Alejandro Colomar Signed-off-by: Michael Kerrisk --- man3/queue.3 | 61 --------------------------------------------------- man3/stailq.3 | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 61 deletions(-) diff --git a/man3/queue.3 b/man3/queue.3 index ee15e60be..bebfc7a25 100644 --- a/man3/queue.3 +++ b/man3/queue.3 @@ -456,67 +456,6 @@ from the tail queue. .Pp See the EXAMPLES section below for an example program using a tail queue. .Sh EXAMPLES -.Ss Singly-linked tail queue example -.Bd -literal -#include -#include -#include -#include - -struct entry { - int data; - STAILQ_ENTRY(entry) entries; /* Singly-linked tail queue. */ -}; - -STAILQ_HEAD(stailhead, entry); - -int -main(void) -{ - struct entry *n1, *n2, *n3, *np; - struct stailhead head; /* Singly-linked tail queue - head. */ - - STAILQ_INIT(&head); /* Initialize the queue. */ - - n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ - STAILQ_INSERT_HEAD(&head, n1, entries); - - n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ - STAILQ_INSERT_TAIL(&head, n1, entries); - - n2 = malloc(sizeof(struct entry)); /* Insert after. */ - STAILQ_INSERT_AFTER(&head, n1, n2, entries); - - STAILQ_REMOVE(&head, n2, entry, entries);/* Deletion. */ - free(n2); - - n3 = STAILQ_FIRST(&head); - STAILQ_REMOVE_HEAD(&head, entries); /* Deletion from the head. */ - free(n3); - - n1 = STAILQ_FIRST(&head); - n1->data = 0; - for (int i = 1; i < 5; i++) { - n1 = malloc(sizeof(struct entry)); - STAILQ_INSERT_HEAD(&head, n1, entries); - n1->data = i; - } - /* Forward traversal. */ - STAILQ_FOREACH(np, &head, entries) - printf("%i\en", np->data); - /* TailQ Deletion. */ - n1 = STAILQ_FIRST(&head); - while (n1 != NULL) { - n2 = STAILQ_NEXT(n1, entries); - free(n1); - n1 = n2; - } - STAILQ_INIT(&head); - - exit(EXIT_SUCCESS); -} -.Ed .Ss Tail queue example .Bd -literal #include diff --git a/man3/stailq.3 b/man3/stailq.3 index 9b0322e68..88cdccbbc 100644 --- a/man3/stailq.3 +++ b/man3/stailq.3 @@ -255,4 +255,65 @@ using a singly-linked tail queue. .SH CONFORMING TO .SH BUGS .SH EXAMPLES +.Ss Singly-linked tail queue example +.Bd -literal +#include +#include +#include +#include + +struct entry { + int data; + STAILQ_ENTRY(entry) entries; /* Singly-linked tail queue. */ +}; + +STAILQ_HEAD(stailhead, entry); + +int +main(void) +{ + struct entry *n1, *n2, *n3, *np; + struct stailhead head; /* Singly-linked tail queue + head. */ + + STAILQ_INIT(&head); /* Initialize the queue. */ + + n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ + STAILQ_INSERT_HEAD(&head, n1, entries); + + n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ + STAILQ_INSERT_TAIL(&head, n1, entries); + + n2 = malloc(sizeof(struct entry)); /* Insert after. */ + STAILQ_INSERT_AFTER(&head, n1, n2, entries); + + STAILQ_REMOVE(&head, n2, entry, entries);/* Deletion. */ + free(n2); + + n3 = STAILQ_FIRST(&head); + STAILQ_REMOVE_HEAD(&head, entries); /* Deletion from the head. */ + free(n3); + + n1 = STAILQ_FIRST(&head); + n1->data = 0; + for (int i = 1; i < 5; i++) { + n1 = malloc(sizeof(struct entry)); + STAILQ_INSERT_HEAD(&head, n1, entries); + n1->data = i; + } + /* Forward traversal. */ + STAILQ_FOREACH(np, &head, entries) + printf("%i\en", np->data); + /* TailQ Deletion. */ + n1 = STAILQ_FIRST(&head); + while (n1 != NULL) { + n2 = STAILQ_NEXT(n1, entries); + free(n1); + n1 = n2; + } + STAILQ_INIT(&head); + + exit(EXIT_SUCCESS); +} +.Ed .SH SEE ALSO