mirror of https://github.com/mkerrisk/man-pages
queue.3, tailq.3: EXAMPLES: Move code from queue.3 to tailq.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:
parent
32d12807a8
commit
7b8d4bbf32
56
man3/queue.3
56
man3/queue.3
|
@ -170,62 +170,6 @@ The termination condition for traversal is more complex.
|
|||
Code size is about 40% greater and operations run about 45% slower than lists.
|
||||
.El
|
||||
.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.
|
||||
|
|
56
man3/tailq.3
56
man3/tailq.3
|
@ -323,4 +323,60 @@ See the EXAMPLES section below for an example program using a tail queue.
|
|||
.SH CONFORMING TO
|
||||
.SH BUGS
|
||||
.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 SEE ALSO
|
||||
|
|
Loading…
Reference in New Issue