HEADNAME
, TYPE
)
SLIST_HEAD_INITIALIZER(
head
)
SLIST_ENTRY(
TYPE
)
SLIST_INIT(
SLIST_HEAD *head
)
SLIST_INSERT_AFTER(
TYPE *listelm
, TYPE *elm
, SLIST_ENTRY NAME
)
SLIST_INSERT_HEAD(
SLIST_HEAD *head
, TYPE *elm
, SLIST_ENTRY NAME
)
SLIST_REMOVE_HEAD(
SLIST_HEAD *head
, SLIST_ENTRY NAME
)
SLIST_REMOVE(
SLIST_HEAD *head
, TYPE *elm
, TYPE
, SLIST_ENTRY NAME
)
SLIST_FOREACH(
TYPE *var
, SLIST_HEAD *head
, SLIST_ENTRY NAME
)
int
SLIST_EMPTY(
SLIST_HEAD *head
)
TYPE
*
SLIST_FIRST(
SLIST_HEAD *head
)
TYPE
*
SLIST_NEXT(
TYPE *elm
, SLIST_ENTRY NAME
)
HEADNAME
, TYPE
)
SIMPLEQ_HEAD_INITIALIZER(
head
)
SIMPLEQ_ENTRY(
TYPE
)
SIMPLEQ_INIT(
SIMPLEQ_HEAD *head
)
SIMPLEQ_INSERT_HEAD(
SIMPLEQ_HEAD *head
, TYPE *elm
, SIMPLEQ_ENTRY NAME
)
SIMPLEQ_INSERT_TAIL(
SIMPLEQ_HEAD *head
, TYPE *elm
, SIMPLEQ_ENTRY NAME
)
SIMPLEQ_INSERT_AFTER(
SIMPLEQ_HEAD *head
, TYPE *listelm
, TYPE *elm
, SIMPLEQ_ENTRY NAME
)
SIMPLEQ_REMOVE_HEAD(
SIMPLEQ_HEAD *head
, SIMPLEQ_ENTRY NAME
)
SIMPLEQ_REMOVE(
SIMPLEQ_HEAD *head
, TYPE *elm
, TYPE
, SIMPLEQ_ENTRY NAME
)
SIMPLEQ_FOREACH(
TYPE *var
, SIMPLEQ_HEAD *head
, SIMPLEQ_ENTRY NAME
)
int
SIMPLEQ_EMPTY(
SIMPLEQ_HEAD *head
)
TYPE
*
SIMPLEQ_FIRST(
SIMPLEQ_HEAD *head
)
TYPE
*
SIMPLEQ_NEXT(
TYPE *elm
, SIMPLEQ_ENTRY NAME
)
HEADNAME
, TYPE
)
STAILQ_HEAD_INITIALIZER(
head
)
STAILQ_ENTRY(
TYPE
)
STAILQ_INIT(
STAILQ_HEAD *head
)
STAILQ_INSERT_HEAD(
STAILQ_HEAD *head
, TYPE *elm
, STAILQ_ENTRY NAME
)
STAILQ_INSERT_TAIL(
STAILQ_HEAD *head
, TYPE *elm
, STAILQ_ENTRY NAME
)
STAILQ_INSERT_AFTER(
STAILQ_HEAD *head
, TYPE *listelm
, TYPE *elm
, STAILQ_ENTRY NAME
)
STAILQ_REMOVE_HEAD(
STAILQ_HEAD *head
, STAILQ_ENTRY NAME
)
STAILQ_REMOVE(
STAILQ_HEAD *head
, TYPE *elm
, TYPE
, STAILQ_ENTRY NAME
)
STAILQ_FOREACH(
TYPE *var
, STAILQ_HEAD *head
, STAILQ_ENTRY NAME
)
int
STAILQ_EMPTY(
STAILQ_HEAD *head
)
TYPE
*
STAILQ_FIRST(
STAILQ_HEAD *head
)
TYPE
*
STAILQ_NEXT(
TYPE *elm
, STAILQ_ENTRY NAME
)
STAILQ_CONCAT(
STAILQ_HEAD *head1
, STAILQ_HEAD *head2
)
HEADNAME
, TYPE
)
LIST_HEAD_INITIALIZER(
head
)
LIST_ENTRY(
TYPE
)
LIST_INIT(
LIST_HEAD *head
)
LIST_INSERT_AFTER(
TYPE *listelm
, TYPE *elm
, LIST_ENTRY NAME
)
LIST_INSERT_BEFORE(
TYPE *listelm
, TYPE *elm
, LIST_ENTRY NAME
)
LIST_INSERT_HEAD(
LIST_HEAD *head
, TYPE *elm
, LIST_ENTRY NAME
)
LIST_REMOVE(
TYPE *elm
, LIST_ENTRY NAME
)
LIST_FOREACH(
TYPE *var
, LIST_HEAD *head
, LIST_ENTRY NAME
)
int
LIST_EMPTY(
LIST_HEAD *head
)
TYPE
*
LIST_FIRST(
LIST_HEAD *head
)
TYPE
*
LIST_NEXT(
TYPE *elm
, LIST_ENTRY NAME
)
HEADNAME
, TYPE
)
TAILQ_HEAD_INITIALIZER(
head
)
TAILQ_ENTRY(
TYPE
)
TAILQ_INIT(
TAILQ_HEAD *head
)
TAILQ_INSERT_HEAD(
TAILQ_HEAD *head
, TYPE *elm
, TAILQ_ENTRY NAME
)
TAILQ_INSERT_TAIL(
TAILQ_HEAD *head
, TYPE *elm
, TAILQ_ENTRY NAME
)
TAILQ_INSERT_AFTER(
TAILQ_HEAD *head
, TYPE *listelm
, TYPE *elm
, TAILQ_ENTRY NAME
)
TAILQ_INSERT_BEFORE(
TYPE *listelm
, TYPE *elm
, TAILQ_ENTRY NAME
)
TAILQ_REMOVE(
TAILQ_HEAD *head
, TYPE *elm
, TAILQ_ENTRY NAME
)
TAILQ_FOREACH(
TYPE *var
, TAILQ_HEAD *head
, TAILQ_ENTRY NAME
)
TAILQ_FOREACH_REVERSE(
TYPE *var
, TAILQ_HEAD *head
, HEADNAME
, TAILQ_ENTRY NAME
)
int
TAILQ_EMPTY(
TAILQ_HEAD *head
)
TYPE
*
TAILQ_FIRST(
TAILQ_HEAD *head
)
TYPE
*
TAILQ_NEXT(
TYPE *elm
, TAILQ_ENTRY NAME
)
TYPE
*
TAILQ_LAST(
TAILQ_HEAD *head
, HEADNAME
)
TYPE
*
TAILQ_PREV(
TYPE *elm
, HEADNAME
, TAILQ_ENTRY NAME
)
TAILQ_CONCAT(
TAILQ_HEAD *head1
, TAILQ_HEAD *head2
, TAILQ_ENTRY NAME
)
HEADNAME
, TYPE
)
CIRCLEQ_HEAD_INITIALIZER(
head
)
CIRCLEQ_ENTRY(
TYPE
)
CIRCLEQ_INIT(
CIRCLEQ_HEAD *head
)
CIRCLEQ_INSERT_AFTER(
CIRCLEQ_HEAD *head
, TYPE *listelm
, TYPE *elm
, CIRCLEQ_ENTRY NAME
)
CIRCLEQ_INSERT_BEFORE(
CIRCLEQ_HEAD *head
, TYPE *listelm
, TYPE *elm
, CIRCLEQ_ENTRY NAME
)
CIRCLEQ_INSERT_HEAD(
CIRCLEQ_HEAD *head
, TYPE *elm
, CIRCLEQ_ENTRY NAME
)
CIRCLEQ_INSERT_TAIL(
CIRCLEQ_HEAD *head
, TYPE *elm
, CIRCLEQ_ENTRY NAME
)
CIRCLEQ_REMOVE(
CIRCLEQ_HEAD *head
, TYPE *elm
, CIRCLEQ_ENTRY NAME
)
CIRCLEQ_FOREACH(
TYPE *var
, CIRCLEQ_HEAD *head
, CIRCLEQ_ENTRY NAME
)
CIRCLEQ_FOREACH_REVERSE(
TYPE *var
, CIRCLEQ_HEAD *head
, CIRCLEQ_ENTRY NAME
)
int
CIRCLEQ_EMPTY(
CIRCLEQ_HEAD *head
)
TYPE
*
CIRCLEQ_FIRST(
CIRCLEQ_HEAD *head
)
TYPE
*
CIRCLEQ_LAST(
CIRCLEQ_HEAD *head
)
TYPE
*
CIRCLEQ_NEXT(
TYPE *elm
, CIRCLEQ_ENTRY NAME
)
TYPE
*
CIRCLEQ_PREV(
TYPE *elm
, CIRCLEQ_ENTRY NAME
)
TYPE
*
CIRCLEQ_LOOP_NEXT(
CIRCLEQ_HEAD *head
, TYPE *elm
, CIRCLEQ_ENTRY NAME
)
TYPE
*
CIRCLEQ_LOOP_PREV(
CIRCLEQ_HEAD *head
, TYPE *elm
, CIRCLEQ_ENTRY NAME
)
Singly-linked lists are the simplest of the five data structures and support only the above functionality. Singly-linked lists are ideal for applications with large datasets and few or no removals, or for implementing a LIFO queue.
Simple queues add the following functionality:
Simple queues are ideal for applications with large datasets and few or no removals, or for implementing a FIFO queue.
All doubly linked types of data structures (lists, tail queues, and circle queues) additionally allow:
Linked lists are the simplest of the doubly linked data structures and support only the above functionality over singly-linked lists.
Tail queues add the following functionality:
Circular queues add the following functionality:
In the macro definitions,
TYPE
is the name of a user defined structure,
that must contain a field of type
LIST_ENTRY
,
SIMPLEQ_ENTRY
,
SLIST_ENTRY
,
TAILQ_ENTRY
,
or
CIRCLEQ_ENTRY
,
named
NAME
.
The argument
HEADNAME
is the name of a user defined structure that must be declared
using the macros
LIST_HEAD
,
SIMPLEQ_HEAD
,
SLIST_HEAD
,
TAILQ_HEAD
,
or
CIRCLEQ_HEAD
.
See the examples below for further explanation of how these
macros are used.
box tab(:); l | c | c | c | c | c | c l | c | c | c | c | c | c l | c | c | c | c | c | c l | c | c | c | c | c | c l | c | c | c | c | c | c l | c | c | c | c | c | c. :SLIST:LIST:SIMPLEQ:STAILQ:TAILQ:CIRCLEQ _ _EMPTY:+:+:+:+:+:+ _FIRST:+:+:+:+:+:+ _FOREACH:+:+:+:+:+:+ _FOREACH_REVERSE:-:-:-:-:+:+ _INSERT_AFTER:+:+:+:+:+:+ _INSERT_BEFORE:-:+:-:-:+:+ _INSERT_HEAD:+:+:+:+:+:+ _INSERT_TAIL:-:-:+:+:+:+ _LAST:-:-:-:-:+:+ _LOOP_NEXT:-:-:-:-:-:+ _LOOP_PREV:-:-:-:-:-:+ _NEXT:+:+:+:+:+:+ _PREV:-:-:-:-:+:+ _REMOVE:+:+:+:+:+:+ _REMOVE_HEAD:+:-:+:+:-:- _CONCAT:-:-:-:+:+:-
SLIST_HEAD
structure is declared as follows:
SLIST_HEAD(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
SLIST_HEAD_INITIALIZER
evaluates to an initializer for the list
head
.
The macro SLIST_EMPTY evaluates to true if there are no elements in the list.
The macro SLIST_ENTRY declares a structure that connects the elements in the list.
The macro SLIST_FIRST returns the first element in the list or NULL if the list is empty.
The macro
SLIST_FOREACH
traverses the list referenced by
head
in the forward direction, assigning each element in
turn to
var
.
The macro
SLIST_INIT
initializes the list referenced by
head
.
The macro
SLIST_INSERT_HEAD
inserts the new element
elm
at the head of the list.
The macro
SLIST_INSERT_AFTER
inserts the new element
elm
after the element
listelm
.
The macro SLIST_NEXT returns the next element in the list.
The macro
SLIST_REMOVE
removes the element
elm
from the list.
The macro SLIST_REMOVE_HEAD removes the first element from the head of the list. For optimum efficiency, elements being removed from the head of the list should explicitly use this macro instead of the generic SLIST_REMOVE macro.
SLIST_HEAD(slisthead, entry) head =
SLIST_HEAD_INITIALIZER(head);
struct slisthead *headp; /* Singly-linked List head. */
struct entry {
...
SLIST_ENTRY(entry) entries; /* Singly-linked List. */
...
} *n1, *n2, *n3, *np;
SLIST_INIT(&head); /* Initialize the list. */
n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
SLIST_INSERT_HEAD(&head, n1, entries);
n2 = malloc(sizeof(struct entry)); /* Insert after. */
SLIST_INSERT_AFTER(n1, n2, entries);
SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
free(n2);
n3 = SLIST_FIRST(&head);
SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */
free(n3);
/* Forward traversal. */
SLIST_FOREACH(np, &head, entries)
np-> ...
while (!SLIST_EMPTY(&head)) { /* List Deletion. */
n1 = SLIST_FIRST(&head);
SLIST_REMOVE_HEAD(&head, entries);
free(n1);
}
SIMPLEQ_HEAD
structure is declared as follows:
SIMPLEQ_HEAD(HEADNAME, TYPE) head;
HEADNAME
is the name of the structure to be defined, and
TYPE
is the type of the elements to be linked into the simple queue.
A pointer to the head of the simple queue can later be declared as:
struct HEADNAME *headp;
head
and
headp
are user selectable.)
The macro SIMPLEQ_ENTRY declares a structure that connects the elements in the simple queue.
The macro SIMPLEQ_HEAD_INITIALIZER provides a value which can be used to initialize a simple queue head at compile time, and is used at the point that the simple queue head variable is declared, like:
struct HEADNAME head = SIMPLEQ_HEAD_INITIALIZER(head);
The macro
SIMPLEQ_INIT
initializes the simple queue referenced by
head
.
The macro
SIMPLEQ_INSERT_HEAD
inserts the new element
elm
at the head of the simple queue.
The macro
SIMPLEQ_INSERT_TAIL
inserts the new element
elm
at the end of the simple queue.
The macro
SIMPLEQ_INSERT_AFTER
inserts the new element
elm
after the element
listelm
.
The macro
SIMPLEQ_REMOVE
removes
elm
from the simple queue.
The macro SIMPLEQ_REMOVE_HEAD removes the first element from the head of the simple queue. For optimum efficiency, elements being removed from the head of the queue should explicitly use this macro instead of the generic SIMPLQ_REMOVE macro.
The macro
SIMPLEQ_EMPTY
return true if the simple queue
head
has no elements.
The macro
SIMPLEQ_FIRST
returns the first element of the simple queue
head
.
The macro
SIMPLEQ_FOREACH
traverses the tail queue referenced by
head
in the forward direction, assigning each element
in turn to
var
.
The macro
SIMPLEQ_NEXT
returns the element after the element
elm
.
The macros prefixed with ``STAILQ_'' (STAILQ_HEAD, STAILQ_HEAD_INITIALIZER, STAILQ_ENTRY, STAILQ_INIT, STAILQ_INSERT_HEAD, STAILQ_INSERT_TAIL, STAILQ_INSERT_AFTER, STAILQ_REMOVE_HEAD, STAILQ_REMOVE, STAILQ_FOREACH, STAILQ_EMPTY, STAILQ_FIRST, and STAILQ_NEXT) are functionally identical to these simple queue functions, and are provided for compatibility with FreeBSD.
SIMPLEQ_HEAD(simplehead, entry) head;
struct simplehead *headp; /* Simple queue head. */
struct entry {
...
SIMPLEQ_ENTRY(entry) entries; /* Simple queue. */
...
} *n1, *n2, *np;
SIMPLEQ_INIT(&head); /* Initialize the queue. */
n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
SIMPLEQ_INSERT_HEAD(&head, n1, entries);
n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
SIMPLEQ_INSERT_TAIL(&head, n1, entries);
n2 = malloc(sizeof(struct entry)); /* Insert after. */
SIMPLEQ_INSERT_AFTER(&head, n1, n2, entries);
/* Forward traversal. */
SIMPLEQ_FOREACH(np, &head, entries)
np-> ...
/* Delete. */
while (SIMPLEQ_FIRST(&head) != NULL)
SIMPLEQ_REMOVE_HEAD(&head, entries);
if (SIMPLEQ_EMPTY(&head)) /* Test for emptiness. */
printf("nothing to do\n");
LIST_HEAD
structure is declared as follows:
LIST_HEAD(HEADNAME, TYPE) head;
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;
head
and
headp
are user selectable.)
The macro LIST_ENTRY declares a structure that connects the elements in the list.
The macro LIST_HEAD_INITIALIZER provides a value which can be used to initialize a list head at compile time, and is used at the point that the list head variable is declared, like:
struct HEADNAME head = LIST_HEAD_INITIALIZER(head);
The macro
LIST_INIT
initializes the list referenced by
head
.
The macro
LIST_INSERT_HEAD
inserts the new element
elm
at the head of the list.
The macro
LIST_INSERT_AFTER
inserts the new element
elm
after the element
listelm
.
The macro
LIST_INSERT_BEFORE
inserts the new element
elm
before the element
listelm
.
The macro
LIST_REMOVE
removes the element
elm
from the list.
The macro
LIST_EMPTY
return true if the list
head
has no elements.
The macro
LIST_FIRST
returns the first element of the list
head
.
The macro
LIST_FOREACH
traverses the list referenced by
head
in the forward direction, assigning each element in turn to
var
.
The macro
LIST_NEXT
returns the element after the element
elm
.
LIST_HEAD(listhead, entry) head;
struct listhead *headp; /* List head. */
struct entry {
...
LIST_ENTRY(entry) entries; /* List. */
...
} *n1, *n2, *np;
LIST_INIT(&head); /* Initialize the list. */
n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
LIST_INSERT_HEAD(&head, n1, entries);
n2 = malloc(sizeof(struct entry)); /* Insert after. */
LIST_INSERT_AFTER(n1, n2, entries);
n2 = malloc(sizeof(struct entry)); /* Insert before. */
LIST_INSERT_BEFORE(n1, n2, entries);
/* Forward traversal. */
LIST_FOREACH(np, &head, entries)
np-> ...
/* Delete. */
while (LIST_FIRST(&head) != NULL)
LIST_REMOVE(LIST_FIRST(&head), entries);
if (LIST_EMPTY(&head)) /* Test for emptiness. */
printf("nothing to do\n");
TAILQ_HEAD
structure is declared as follows:
TAILQ_HEAD(HEADNAME, TYPE) head;
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;
head
and
headp
are user selectable.)
The macro TAILQ_ENTRY declares a structure that connects the elements in the tail queue.
The macro TAILQ_HEAD_INITIALIZER provides a value which can be used to initialize a tail queue head at compile time, and is used at the point that the tail queue head variable is declared, like:
struct HEADNAME head = TAILQ_HEAD_INITIALIZER(head);
The macro
TAILQ_INIT
initializes the tail queue referenced by
head
.
The macro
TAILQ_INSERT_HEAD
inserts the new element
elm
at the head of the tail queue.
The macro
TAILQ_INSERT_TAIL
inserts the new element
elm
at the end of the tail queue.
The macro
TAILQ_INSERT_AFTER
inserts the new element
elm
after the element
listelm
.
The macro
TAILQ_INSERT_BEFORE
inserts the new element
elm
before the element
listelm
.
The macro
TAILQ_REMOVE
removes the element
elm
from the tail queue.
The macro
TAILQ_EMPTY
return true if the tail queue
head
has no elements.
The macro
TAILQ_FIRST
returns the first element of the tail queue
head
.
The macro
TAILQ_FOREACH
traverses the tail queue referenced by
head
in the forward direction, assigning each element in turn to
var
.
The macro
TAILQ_FOREACH_REVERSE
traverses the tail queue referenced by
head
in the reverse direction, assigning each element in turn to
var
.
The macro
TAILQ_NEXT
returns the element after the element
elm
.
The macro
TAILQ_CONCAT
concatenates the tail queue headed by
head2
onto the end of the one headed by
head1
removing all entries from the former.
TAILQ_HEAD(tailhead, entry) head;
struct tailhead *headp; /* Tail queue head. */
struct entry {
...
TAILQ_ENTRY(entry) entries; /* Tail queue. */
...
} *n1, *n2, *np;
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);
n2 = malloc(sizeof(struct entry)); /* Insert before. */
TAILQ_INSERT_BEFORE(n1, n2, entries);
/* Forward traversal. */
TAILQ_FOREACH(np, &head, entries)
np-> ...
/* Reverse traversal. */
TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
np-> ...
/* Delete. */
while (TAILQ_FIRST(&head) != NULL)
TAILQ_REMOVE(&head, TAILQ_FIRST(&head), entries);
if (TAILQ_EMPTY(&head)) /* Test for emptiness. */
printf("nothing to do\n");
CIRCLEQ_HEAD
structure is declared as follows:
CIRCLEQ_HEAD(HEADNAME, TYPE) head;
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;
head
and
headp
are user selectable.)
The macro CIRCLEQ_ENTRY declares a structure that connects the elements in the circular queue.
The macro CIRCLEQ_HEAD_INITIALIZER provides a value which can be used to initialize a circular queue head at compile time, and is used at the point that the circular queue head variable is declared, like:
struct HEADNAME head = CIRCLEQ_HEAD_INITIALIZER(head);
The macro
CIRCLEQ_INIT
initializes the circular queue referenced by
head
.
The macro
CIRCLEQ_INSERT_HEAD
inserts the new element
elm
at the head of the circular queue.
The macro
CIRCLEQ_INSERT_TAIL
inserts the new element
elm
at the end of the circular queue.
The macro
CIRCLEQ_INSERT_AFTER
inserts the new element
elm
after the element
listelm
.
The macro
CIRCLEQ_INSERT_BEFORE
inserts the new element
elm
before the element
listelm
.
The macro
CIRCLEQ_REMOVE
removes the element
elm
from the circular queue.
The macro
CIRCLEQ_EMPTY
return true if the circular queue
head
has no elements.
The macro
CIRCLEQ_FIRST
returns the first element of the circular queue
head
.
The macro
CIRCLEQ_FOREACH
traverses the circle queue referenced by
head
in the forward direction, assigning each element in turn to
var
.
Each element is assigned exactly once.
The macro
CIRCLEQ_FOREACH_REVERSE
traverses the circle queue referenced by
head
in the reverse direction, assigning each element in turn to
var
.
Each element is assigned exactly once.
The macro
CIRCLEQ_LAST
returns the last element of the circular queue
head
.
The macro
CIRCLEQ_NEXT
returns the element after the element
elm
.
The macro
CIRCLEQ_PREV
returns the element before the element
elm
.
The macro
CIRCLEQ_LOOP_NEXT
returns the element after the element
elm
.
If
elm
was the last element in the queue, the first element is returned.
The macro
CIRCLEQ_LOOP_PREV
returns the element before the element
elm
.
If
elm
was the first element in the queue, the last element is returned.
CIRCLEQ_HEAD(circleq, entry) head;
struct circleq *headp; /* Circular queue head. */
struct entry {
...
CIRCLEQ_ENTRY(entry) entries; /* Circular queue. */
...
} *n1, *n2, *np;
CIRCLEQ_INIT(&head); /* Initialize the circular 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);
n2 = malloc(sizeof(struct entry)); /* Insert before. */
CIRCLEQ_INSERT_BEFORE(&head, n1, n2, entries);
/* Forward traversal. */
CIRCLEQ_FOREACH(np, &head, entries)
np-> ...
/* Reverse traversal. */
CIRCLEQ_FOREACH_REVERSE(np, &head, entries)
np-> ...
/* Delete. */
while (CIRCLEQ_FIRST(&head) != (void *)&head)
CIRCLEQ_REMOVE(&head, CIRCLEQ_FIRST(&head), entries);
if (CIRCLEQ_EMPTY(&head)) /* Test for emptiness. */
printf("nothing to do\n");