linux-man.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alejandro Colomar <colomar.6.4.3@gmail.com>
To: mtk.manpages@gmail.com
Cc: Alejandro Colomar <colomar.6.4.3@gmail.com>,
	linux-man@vger.kernel.org, libc-alpha@sourceware.org
Subject: [PATCH] queue.3: Replace incomplete example by a complete example
Date: Sat, 10 Oct 2020 21:02:27 +0200	[thread overview]
Message-ID: <20201010190226.19236-1-colomar.6.4.3@gmail.com> (raw)

I added the EXAMPLES section.
The examples in this page are incomplete
(you can't copy&paste&compile&run).
I fixed the one about TAILQ first,
and the rest should follow.

Signed-off-by: Alejandro Colomar <colomar.6.4.3@gmail.com>
---

Hi Michael,

I think this page needs a big overhaul.

First of all, it's a very big page,
where it's a bit difficult to go to the subsection you want.
Then, the examples are incomplete.
And also, the language of the page is weird.

I thought about having queue.h with an overview of all the different
data structures, and the differences about them.

And then separate pages for each data structure:
slist.3, list.3,
stailq.3, tailq.3,
simpleq.3 (which right now isn't documented),
and circleq.3
with details about each macro and a complete example program.

Your thoughts?

Cheers,

Alex


 man3/queue.3 | 115 ++++++++++++++++++++++++++-------------------------
 1 file changed, 59 insertions(+), 56 deletions(-)

diff --git a/man3/queue.3 b/man3/queue.3
index 55cd5847e..ea43f018b 100644
--- a/man3/queue.3
+++ b/man3/queue.3
@@ -1220,62 +1220,8 @@ from the tail queue.
 .\" .Fa head1
 .\" and
 .\" .Fa head2 .
-.Ss Tail queue example
-.Bd -literal
-TAILQ_HEAD(tailhead, entry) head =
-    TAILQ_HEAD_INITIALIZER(head);
-struct tailhead *headp;			/* Tail queue head. */
-struct entry {
-	...
-	TAILQ_ENTRY(entry) entries;	/* Tail queue. */
-	...
-} *n1, *n2, *n3, *np;
-
-TAILQ_INIT(&head);			/* Initialize the queue. */
-
-n1 = malloc(sizeof(struct entry));	/* Insert at the head. */
-TAILQ_INSERT_HEAD(&head, n1, entries);
-
-n1 = malloc(sizeof(struct entry));	/* Insert at the tail. */
-TAILQ_INSERT_TAIL(&head, n1, entries);
-
-n2 = malloc(sizeof(struct entry));	/* Insert after. */
-TAILQ_INSERT_AFTER(&head, n1, n2, entries);
-
-n3 = malloc(sizeof(struct entry));	/* Insert before. */
-TAILQ_INSERT_BEFORE(n2, n3, entries);
-
-TAILQ_REMOVE(&head, n2, entries);	/* Deletion. */
-free(n2);
-					/* Forward traversal. */
-TAILQ_FOREACH(np, &head, entries)
-	np\-> ...
-.\" 					/* Safe forward traversal. */
-.\" TAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
-.\" 	np\->do_stuff();
-.\" 	...
-.\" 	TAILQ_REMOVE(&head, np, entries);
-.\" 	free(np);
-.\" }
-					/* Reverse traversal. */
-TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
-	np\-> ...
-					/* TailQ Deletion. */
-while (!TAILQ_EMPTY(&head)) {
-	n1 = TAILQ_FIRST(&head);
-	TAILQ_REMOVE(&head, n1, entries);
-	free(n1);
-}
-					/* Faster TailQ Deletion. */
-n1 = TAILQ_FIRST(&head);
-while (n1 != NULL) {
-	n2 = TAILQ_NEXT(n1, entries);
-	free(n1);
-	n1 = n2;
-}
-
-TAILQ_INIT(&head);
-.Ed
+.Pp
+See the EXAMPLES section below for an example program using a tail queue.
 .Ss Circular queues
 A circular queue is headed by a structure defined by the
 .Nm CIRCLEQ_HEAD
@@ -1462,6 +1408,63 @@ while (n1 != (void *)&head) {
 
 CIRCLEQ_INIT(&head);
 .Ed
+.Sh EXAMPLES
+.Ss Tail queue example
+.Bd -literal
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/queue.h>
+
+struct entry {
+    int data;
+    TAILQ_ENTRY(entry) entries;             /* Tail queue. */
+};
+
+TAILQ_HEAD(tailhead, entry);
+
+int
+main(void)
+{
+    struct entry    *n1, *n2, *n3, *np;
+    struct tailhead head;                   /* Tail queue head. */
+    int     i;
+
+    TAILQ_INIT(&head);                      /* Initialize the queue. */
+
+    n1 = malloc(sizeof(struct entry));      /* Insert at the head. */
+    TAILQ_INSERT_HEAD(&head, n1, entries);
+
+    n1 = malloc(sizeof(struct entry));      /* Insert at the tail. */
+    TAILQ_INSERT_TAIL(&head, n1, entries);
+
+    n2 = malloc(sizeof(struct entry));      /* Insert after. */
+    TAILQ_INSERT_AFTER(&head, n1, n2, entries);
+
+    n3 = malloc(sizeof(struct entry));      /* Insert before. */
+    TAILQ_INSERT_BEFORE(n2, n3, entries);
+
+    TAILQ_REMOVE(&head, n2, entries);       /* Deletion. */
+    free(n2);
+                                            /* Forward traversal. */
+    i = 0;
+    TAILQ_FOREACH(np, &head, entries)
+        np->data = i++;
+                                            /* Reverse traversal. */
+    TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
+        printf("%i\en", np->data);
+                                            /* TailQ Deletion. */
+    n1 = TAILQ_FIRST(&head);
+    while (n1 != NULL) {
+        n2 = TAILQ_NEXT(n1, entries);
+        free(n1);
+        n1 = n2;
+    }
+    TAILQ_INIT(&head);
+
+    exit(EXIT_SUCCESS);
+}
+.Ed
 .Sh CONFORMING TO
 Not in POSIX.1, POSIX.1-2001 or POSIX.1-2008.
 Present on the BSDs.
-- 
2.28.0


             reply	other threads:[~2020-10-10 22:55 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-10 19:02 Alejandro Colomar [this message]
2020-10-10 19:08 ` [PATCH] queue.3: Replace incomplete example by a complete example Alejandro Colomar
2020-10-11  6:00 ` Michael Kerrisk (man-pages)
2020-10-11  9:03   ` Alejandro Colomar
2020-10-11 12:24     ` Michael Kerrisk (man-pages)
2020-10-12 12:53 ` Zack Weinberg
2020-10-12 18:47   ` Michael Kerrisk (man-pages)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201010190226.19236-1-colomar.6.4.3@gmail.com \
    --to=colomar.6.4.3@gmail.com \
    --cc=libc-alpha@sourceware.org \
    --cc=linux-man@vger.kernel.org \
    --cc=mtk.manpages@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).