aboutsummaryrefslogtreecommitdiffstats
path: root/man3/queue.3
diff options
context:
space:
mode:
authorAlejandro Colomar <colomar.6.4.3@gmail.com>2020-10-13 16:55:49 +0200
committerMichael Kerrisk <mtk.manpages@gmail.com>2020-10-13 17:36:17 +0200
commit46b0a9d69cc031e0cb64f0f7ba6fe7eb2aba3a57 (patch)
treede992b9d7122d19d34b2f98fe6355e83e7586a93 /man3/queue.3
parent861b602b53cda7aa894cdddd20c2f6e37fe77b8a (diff)
downloadman-pages-46b0a9d69cc031e0cb64f0f7ba6fe7eb2aba3a57.tar.gz
queue.3: circleq: Complete example
Signed-off-by: Alejandro Colomar <colomar.6.4.3@gmail.com> Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
Diffstat (limited to 'man3/queue.3')
-rw-r--r--man3/queue.3105
1 files changed, 56 insertions, 49 deletions
diff --git a/man3/queue.3 b/man3/queue.3
index 95bc7d5bc0..fed8d126f5 100644
--- a/man3/queue.3
+++ b/man3/queue.3
@@ -1318,55 +1318,6 @@ The macro
removes the element
.Fa elm
from the circular queue.
-.Ss Circular queue example
-.Bd -literal
-CIRCLEQ_HEAD(circleq, entry) head =
- CIRCLEQ_HEAD_INITIALIZER(head);
-struct circleq *headp; /* Circular queue head. */
-struct entry {
- ...
- CIRCLEQ_ENTRY(entry) entries; /* Circular queue. */
- ...
-} *n1, *n2, *n3, *np;
-
-CIRCLEQ_INIT(&head); /* Initialize the queue. */
-
-n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
-CIRCLEQ_INSERT_HEAD(&head, n1, entries);
-
-n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
-CIRCLEQ_INSERT_TAIL(&head, n1, entries);
-
-n2 = malloc(sizeof(struct entry)); /* Insert after. */
-CIRCLEQ_INSERT_AFTER(&head, n1, n2, entries);
-
-n3 = malloc(sizeof(struct entry)); /* Insert before. */
-CIRCLEQ_INSERT_BEFORE(&head, n2, n3, entries);
-
-CIRCLEQ_REMOVE(&head, n2, entries); /* Deletion. */
-free(n2);
- /* Forward traversal. */
-CIRCLEQ_FOREACH(np, &head, entries)
- np\-> ...
- /* Reverse traversal. */
-CIRCLEQ_FOREACH_REVERSE(np, &head, entries)
- np\-> ...
- /* CircleQ Deletion. */
-while (!CIRCLEQ_EMPTY(&head)) {
- n1 = CIRCLEQ_FIRST(&head);
- CIRCLEQ_REMOVE(&head, n1, entries);
- free(n1);
-}
- /* Faster CircleQ Deletion. */
-n1 = CIRCLEQ_FIRST(&head);
-while (n1 != (void *)&head) {
- n2 = CIRCLEQ_NEXT(n1, entries);
- free(n1);
- n1 = n2;
-}
-
-CIRCLEQ_INIT(&head);
-.Ed
.Sh EXAMPLES
.Ss Singly-linked list example
.Bd -literal
@@ -1481,6 +1432,62 @@ main(void)
exit(EXIT_SUCCESS);
}
.Ed
+.Ss Circular queue example
+.Bd -literal
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/queue.h>
+
+struct entry {
+ int data;
+ CIRCLEQ_ENTRY(entry) entries; /* Queue. */
+};
+
+CIRCLEQ_HEAD(circlehead, entry);
+
+int
+main(void)
+{
+ struct entry *n1, *n2, *n3, *np;
+ struct circlehead head; /* Queue head. */
+ int i;
+
+ CIRCLEQ_INIT(&head); /* Initialize the queue. */
+
+ n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
+ CIRCLEQ_INSERT_HEAD(&head, n1, entries);
+
+ n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
+ CIRCLEQ_INSERT_TAIL(&head, n1, entries);
+
+ n2 = malloc(sizeof(struct entry)); /* Insert after. */
+ CIRCLEQ_INSERT_AFTER(&head, n1, n2, entries);
+
+ n3 = malloc(sizeof(struct entry)); /* Insert before. */
+ CIRCLEQ_INSERT_BEFORE(&head, n2, n3, entries);
+
+ CIRCLEQ_REMOVE(&head, n2, entries); /* Deletion. */
+ free(n2);
+ /* Forward traversal. */
+ i = 0;
+ CIRCLEQ_FOREACH(np, &head, entries)
+ np->data = i++;
+ /* Reverse traversal. */
+ CIRCLEQ_FOREACH_REVERSE(np, &head, entries)
+ printf("%i\en", np->data);
+ /* Queue deletion. */
+ n1 = CIRCLEQ_FIRST(&head);
+ while (n1 != (void *)&head) {
+ n2 = CIRCLEQ_NEXT(n1, entries);
+ free(n1);
+ n1 = n2;
+ }
+ CIRCLEQ_INIT(&head);
+
+ exit(EXIT_SUCCESS);
+}
+.Ed
.Sh CONFORMING TO
Not in POSIX.1, POSIX.1-2001 or POSIX.1-2008.
Present on the BSDs.