MyWebUniversity.com Home Page
 



Darwin Mac OS X man pages main menu
QUEUE(3)                 BSD Library Functions Manual                 QUEUE(3)

NAME
     LISTENTRY, LISTHEAD, LISTINIT, LISTINSERTAFTER, LISTINSERTBEFORE,
     LISTINSERTHEAD, LISTREMOVE, TAILQENTRY, TAILQHEAD, TAILQINIT,
     TAILQINSERTAFTER, TAILQINSERTBEFORE, TAILQINSERTHEAD,
     TAILQINSERTAIL, TAILQREMOVE, CIRCLEQENTRY, CIRCLEQHEAD,
     CIRCLEQINIT, CIRCLEQINSERTAFTER, CIRCLEQINSERTBEFORE,
     CIRCLEQINSERTHEAD, CIRCLEQINSERTAIL, CIRCLEQREMOVE -- implementa-
     tions of lists, tail queues, and circular queues

SYNOPSIS
     ##include <>


     LISTENTRY(TYPE);

     LISTHEAD(HEADNAME, TYPE);

     LISTINIT(LISTHEAD *head);

     LISTINSERTAFTER(TYPE *listelm, TYPE *elm, LISTENTRY NAME);

     LISTINSERTBEFORE(TYPE *listelm, TYPE *elm, LISTENTRY NAME);

     LISTINSERTHEAD(LISTHEAD *head, TYPE *elm, LISTENTRY NAME);

     LISTREMOVE(TYPE *elm, LISTENTRY NAME);


     TAILQENTRY(TYPE);

     TAILQHEAD(HEADNAME, TYPE);

     TAILQINIT(TAILQHEAD *head);

     TAILQINSERTAFTER(TAILQHEAD *head, TYPE *listelm, TYPE *elm,
         TAILQENTRY NAME);

     TAILQINSERTBEFORE(TYPE *listelm, TYPE *elm, TAILQENTRY NAME);

     TAILQINSERTHEAD(TAILQHEAD *head, TYPE *elm, TAILQENTRY NAME);

     TAILQINSERTAIL(TAILQHEAD *head, TYPE *elm, TAILQENTRY NAME);

     TAILQREMOVE(TAILQHEAD *head, TYPE *elm, TAILQENTRY NAME);


     CIRCLEQENTRY(TYPE);

     CIRCLEQHEAD(HEADNAME, TYPE);

     CIRCLEQINIT(CIRCLEQHEAD *head);

     CIRCLEQINSERTAFTER(CIRCLEQHEAD *head, TYPE *listelm, TYPE *elm,
         CIRCLEQENTRY NAME);

     CIRCLEQINSERTBEFORE(CIRCLEQHEAD *head, TYPE *listelm, TYPE *elm,
         CIRCLEQENTRY NAME);

     CIRCLEQINSERTHEAD(CIRCLEQHEAD *head, TYPE *elm, CIRCLEQENTRY NAME);

     CIRCLEQINSERTAIL(CIRCLEQHEAD *head, TYPE *elm, CIRCLEQENTRY NAME);

     CIRCLEQREMOVE(CIRCLEQHEAD *head, TYPE *elm, CIRCLEQENTRY NAME);

DESCRIPTION
     These macros define and operate on three types of data structures: lists,
     tail queues, and circular queues.  All three structures support the fol-
     lowing functionality:
           1.   Insertion of a new entry at the head of the list.
           2.   Insertion of a new entry before or after any element in the
                list.
           3.   Removal of any entry in the list.
           4.   Forward traversal through the list.

     Lists are the simplest of the three data structures and support only the
     above functionality.

     Tail queues add the following functionality:
           1.   Entries can be added at the end of a list.
     However:
           1.   All list insertions and removals, except insertion before
                another element, must specify the head of the list.
           2.   Each head entry requires two pointers rather than one.
           3.   Code size is about 15% greater and operations run about 20%
                slower than lists.

     Circular queues add the following functionality:
           1.   Entries can be added at the end of a list.
           2.   They may be traversed backwards, from tail to head.
     However:
           1.   All list insertions and removals must specify the head of the
                list.
           2.   Each head entry requires two pointers rather than one.
           3.   The termination condition for traversal is more complex.
           4.   Code size is about 40% greater and operations run about 45%
                slower than lists.

     In the macro definitions, TYPE is the name of a user defined structure,
     that must contain a field of type LISTENTRY, TAILQENTRY, or
     CIRCLEQENTRY, named NAME.  The argument HEADNAME is the name of a user
     defined structure that must be declared using the macros LISTHEAD,
     TAILQHEAD, or CIRCLEQHEAD.  See the examples below for further explana-
     tion of how these macros are used.

LISTS
     A list is headed by a structure defined by the LISTHEAD macro.  This
     structure contains a single pointer to the first element on the list.
     The elements are doubly linked so that an arbitrary element can be
     removed without traversing the list.  New elements can be added to the
     list after an existing element, before an existing element, or at the
     head of the list.  A LISTHEAD structure is declared as follows:

           LISTHEAD(HEADNAME, TYPE) head;

     where HEADNAME is the name of the structure to be defined, and TYPE is
     the type of the elements to be linked into the list.  A pointer to the
     head of the list can later be declared as:

           struct HEADNAME *headp;

     (The names head and headp are user selectable.)

     The macro LISTENTRY declares a structure that connects the elements in
     the list.

     The macro LISTINIT initializes the list referenced by head.

     The macro LISTINSERTHEAD inserts the new element elm at the head of the
     list.

     The macro LISTINSERTAFTER inserts the new element elm after the element
     listelm.

     The macro LISTINSERTBEFORE inserts the new element elm before the ele-
     ment listelm.

     The macro LISTREMOVE removes the element elm from the list.

LIST EXAMPLE
     LISTHEAD(listhead, entry) head;
     struct listhead *headp;         /* List head. */
     struct entry {
             ...
             LISTENTRY(entry) entries;      /* List. */
             ...
     } *n1, *n2, *np;

     LISTINIT(&head);                       /* Initialize the list. */

     n1 = malloc(sizeof(struct entry));      /* Insert at the head. */
     LISTINSERTHEAD(&head, n1, entries);

     n2 = malloc(sizeof(struct entry));      /* Insert after. */
     LISTINSERTAFTER(n1, n2, entries);

     n2 = malloc(sizeof(struct entry));      /* Insert before. */
     LISTINSERTBEFORE(n1, n2, entries);
                                             /* Forward traversal. */
     for (np = head.lhfirst; np != NUL; np = np->entries.lenext)
             np-> ...

     while (head.lhfirst != NUL)           /* Delete. */
             LISTREMOVE(head.lhfirst, entries);

TAIL QUEUES
     A tail queue is headed by a structure defined by the TAILQHEAD macro.
     This structure contains a pair of pointers, one to the first element in
     the tail queue and the other to the last element in the tail queue.  The
     elements are doubly linked so that an arbitrary element can be removed
     without traversing the tail queue.  New elements can be added to the
     queue after an existing element, before an existing element, at the head
     of the queue, or at the end the queue.  A TAILQHEAD structure is
     declared as follows:

           TAILQHEAD(HEADNAME, TYPE) head;

     where HEADNAME is the name of the structure to be defined, and TYPE is
     the type of the elements to be linked into the tail queue.  A pointer to
     the head of the tail queue can later be declared as:

           struct HEADNAME *headp;

     (The names head and headp are user selectable.)

     The macro TAILQENTRY declares a structure that connects the elements in
     the tail queue.

     The macro TAILQINIT initializes the tail queue referenced by head.

     The macro TAILQINSERTHEAD inserts the new element elm at the head of
     the tail queue.

     The macro TAILQINSERTAIL inserts the new element elm at the end of the
     tail queue.

     The macro TAILQINSERTAFTER inserts the new element elm after the ele-
     ment listelm.

     The macro TAILQINSERTBEFORE inserts the new element elm before the ele-
     ment listelm.

     The macro TAILQREMOVE removes the element elm from the tail queue.

TAIL QUEUE EXAMPLE
     TAILQHEAD(tailhead, entry) head;
     struct tailhead *headp;         /* Tail queue head. */
     struct entry {
             ...
             TAILQENTRY(entry) entries;     /* Tail queue. */
             ...
     } *n1, *n2, *np;

     TAILQINIT(&head);                      /* Initialize the queue. */

     n1 = malloc(sizeof(struct entry));      /* Insert at the head. */
     TAILQINSERTHEAD(&head, n1, entries);

     n1 = malloc(sizeof(struct entry));      /* Insert at the tail. */
     TAILQINSERTAIL(&head, n1, entries);

     n2 = malloc(sizeof(struct entry));      /* Insert after. */
     TAILQINSERTAFTER(&head, n1, n2, entries);

     n2 = malloc(sizeof(struct entry));      /* Insert before. */
     TAILQINSERTBEFORE(n1, n2, entries);
                                             /* Forward traversal. */
     for (np = head.tqhfirst; np != NUL; np = np->entries.tqenext)
             np-> ...
                                             /* Delete. */
     while (head.tqhfirst != NUL)
             TAILQREMOVE(&head, head.tqhfirst, entries);

CIRCULAR QUEUES
     A circular queue is headed by a structure defined by the CIRCLEQHEAD
     macro.  This structure contains a pair of pointers, one to the first ele-
     ment in the circular queue and the other to the last element in the cir-
     cular queue.  The elements are doubly linked so that an arbitrary element
     can be removed without traversing the queue.  New elements can be added
     to the queue after an existing element, before an existing element, at
     the head of the queue, or at the end of the queue.  A CIRCLEQHEAD struc-
     ture is declared as follows:

           CIRCLEQHEAD(HEADNAME, TYPE) head;

     where HEADNAME is the name of the structure to be defined, and TYPE is
     the type of the elements to be linked into the circular queue.  A pointer
     to the head of the circular queue can later be declared as:

           struct HEADNAME *headp;

     (The names head and headp are user selectable.)

     The macro CIRCLEQENTRY declares a structure that connects the elements
     in the circular queue.

     The macro CIRCLEQINIT initializes the circular queue referenced by head.

     The macro CIRCLEQINSERTHEAD inserts the new element elm at the head of
     the circular queue.

     The macro CIRCLEQINSERTAIL inserts the new element elm at the end of
     the circular queue.

     The macro CIRCLEQINSERTAFTER inserts the new element elm after the ele-
     ment listelm.

     The macro CIRCLEQINSERTBEFORE inserts the new element elm before the
     element listelm.

     The macro CIRCLEQREMOVE removes the element elm from the circular queue.

CIRCULAR QUEUE EXAMPLE
     CIRCLEQHEAD(circleq, entry) head;
     struct circleq *headp;                  /* Circular queue head. */
     struct entry {
             ...
             CIRCLEQENTRY entries;          /* Circular queue. */
             ...
     } *n1, *n2, *np;

     CIRCLEQINIT(&head);                    /* Initialize the circular queue. */

     n1 = malloc(sizeof(struct entry));      /* Insert at the head. */
     CIRCLEQINSERTHEAD(&head, n1, entries);

     n1 = malloc(sizeof(struct entry));      /* Insert at the tail. */
     CIRCLEQINSERTAIL(&head, n1, entries);

     n2 = malloc(sizeof(struct entry));      /* Insert after. */
     CIRCLEQINSERTAFTER(&head, n1, n2, entries);

     n2 = malloc(sizeof(struct entry));      /* Insert before. */
     CIRCLEQINSERTBEFORE(&head, n1, n2, entries);
                                             /* Forward traversal. */
     for (np = head.cqhfirst; np != (void *)&head; np = np->entries.cqenext)
             np-> ...
                                             /* Reverse traversal. */
     for (np = head.cqhlast; np != (void *)&head; np = np->entries.cqeprev)
             np-> ...
                                             /* Delete. */
     while (head.cqhfirst != (void *)&head)
             CIRCLEQREMOVE(&head, head.cqhfirst, entries);

HISTORY
     The queue functions first appeared in 4.4BSD.

4th Berkeley Distribution      December 13, 1993     4th Berkeley Distribution
Darwin Mac OS X man pages main menu

Contact us      |       About us      |       Term of use      |       Copyright © 2000-2010 MyWebUniversity.com ™