queue.3, stailq.3: EXAMPLES: Move stailq example from queue.3 to stailq.3

Signed-off-by: Alejandro Colomar <colomar.6.4.3@gmail.com>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
This commit is contained in:
Alejandro Colomar 2020-10-25 00:21:13 +02:00 committed by Michael Kerrisk
parent cdba777fd5
commit d47b080534
2 changed files with 61 additions and 61 deletions

View File

@ -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 <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/queue.h>
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 <stddef.h>

View File

@ -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 <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/queue.h>
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