public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: "Michael Kerrisk (man-pages)" <mtk.manpages@gmail.com>
To: Alejandro Colomar <colomar.6.4.3@gmail.com>
Cc: mtk.manpages@gmail.com, linux-man@vger.kernel.org,
	libc-alpha@sourceware.org
Subject: Re: [PATCH] queue.3: circleq: Complete example
Date: Tue, 13 Oct 2020 17:36:57 +0200	[thread overview]
Message-ID: <78286397-45ac-80c6-2b41-bfac240a50d9@gmail.com> (raw)
In-Reply-To: <20201013145548.14387-1-colomar.6.4.3@gmail.com>

Hi Alex,

On 10/13/20 4:55 PM, Alejandro Colomar wrote:
> Signed-off-by: Alejandro Colomar <colomar.6.4.3@gmail.com>
> ---
> 
> Hola Michael,
> 
> I'm a little busy this week as last week,
> so I'll send less/simpler patches than in the previous weeks :-)
> 
> Un abrazo,

Thanks! Patch applied.

Cheers,

Michael

>  man3/queue.3 | 105 +++++++++++++++++++++++++++------------------------
>  1 file changed, 56 insertions(+), 49 deletions(-)
> 
> diff --git a/man3/queue.3 b/man3/queue.3
> index 95bc7d5bc..fed8d126f 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.
> 


-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/

      reply	other threads:[~2020-10-13 15:37 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-13 14:55 Alejandro Colomar
2020-10-13 15:36 ` Michael Kerrisk (man-pages) [this message]

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=78286397-45ac-80c6-2b41-bfac240a50d9@gmail.com \
    --to=mtk.manpages@gmail.com \
    --cc=colomar.6.4.3@gmail.com \
    --cc=libc-alpha@sourceware.org \
    --cc=linux-man@vger.kernel.org \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).