aboutsummaryrefslogtreecommitdiffstats
path: root/man3
diff options
context:
space:
mode:
authorAlejandro Colomar <colomar.6.4.3@gmail.com>2020-10-10 21:02:27 +0200
committerMichael Kerrisk <mtk.manpages@gmail.com>2020-10-11 14:22:20 +0200
commitb3617b75f97836948c25acb2bc882e4698be3ced (patch)
tree6747d8362f7364584bcd0280d30a70ce93dde367 /man3
parent8e3a41435472bce03e2d3fce52d1649fa8d6ea5d (diff)
downloadman-pages-b3617b75f97836948c25acb2bc882e4698be3ced.tar.gz
queue.3: Replace incomplete example by a complete example
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> Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
Diffstat (limited to 'man3')
-rw-r--r--man3/queue.3115
1 files changed, 59 insertions, 56 deletions
diff --git a/man3/queue.3 b/man3/queue.3
index 55cd5847ec..ea43f018b0 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.