---
name: "queue"
section: "3"
platform: "macos"
package: "xnu"
version: "12377.121.6"
release: "macOS 26.5"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/queue/3"
---

# queue(3)

implementations of singly-linked lists, singly-linked tail queues, lists and tail queues

## NAME

**SLIST\_EMPTY**, **SLIST\_ENTRY**, **SLIST\_FIRST**, **SLIST\_FOREACH**, **SLIST\_FOREACH\_SAFE**, **SLIST\_HEAD**, **SLIST\_HEAD\_INITIALIZER**, **SLIST\_INIT**, **SLIST\_INSERT\_AFTER**, **SLIST\_INSERT\_HEAD**, **SLIST\_NEXT**, **SLIST\_REMOVE\_HEAD**, **SLIST\_REMOVE**, **STAILQ\_CONCAT**, **STAILQ\_EMPTY**, **STAILQ\_ENTRY**, **STAILQ\_FIRST**, **STAILQ\_FOREACH**, **STAILQ\_FOREACH\_SAFE**, **STAILQ\_HEAD**, **STAILQ\_HEAD\_INITIALIZER**, **STAILQ\_INIT**, **STAILQ\_INSERT\_AFTER**, **STAILQ\_INSERT\_HEAD**, **STAILQ\_INSERT\_TAIL**, **STAILQ\_LAST**, **STAILQ\_NEXT**, **STAILQ\_REMOVE\_HEAD**, **STAILQ\_REMOVE**, **LIST\_EMPTY**, **LIST\_ENTRY**, **LIST\_FIRST**, **LIST\_FOREACH**, **LIST\_FOREACH\_SAFE**, **LIST\_HEAD**, **LIST\_HEAD\_INITIALIZER**, **LIST\_INIT**, **LIST\_INSERT\_AFTER**, **LIST\_INSERT\_BEFORE**, **LIST\_INSERT\_HEAD**, **LIST\_NEXT**, **LIST\_REMOVE**, **TAILQ\_CONCAT**, **TAILQ\_EMPTY**, **TAILQ\_ENTRY**, **TAILQ\_FIRST**, **TAILQ\_FOREACH**, **TAILQ\_FOREACH\_SAFE**, **TAILQ\_FOREACH\_REVERSE**, **TAILQ\_FOREACH\_REVERSE\_SAFE**, **TAILQ\_HEAD**, **TAILQ\_HEAD\_INITIALIZER**, **TAILQ\_INIT**, **TAILQ\_INSERT\_AFTER**, **TAILQ\_INSERT\_BEFORE**, **TAILQ\_INSERT\_HEAD**, **TAILQ\_INSERT\_TAIL**, **TAILQ\_LAST**, **TAILQ\_NEXT**, **TAILQ\_PREV**, **TAILQ\_REMOVE** – implementations of singly-linked lists, singly-linked tail queues, lists and tail queues

## SYNOPSIS

**#include \<sys/queue.h\>**

**SLIST\_EMPTY**(*SLIST\_HEAD \*head*);

**SLIST\_ENTRY**(*TYPE*);

**SLIST\_FIRST**(*SLIST\_HEAD \*head*);

**SLIST\_FOREACH**(*TYPE \*var*, *SLIST\_HEAD \*head*, *SLIST\_ENTRY NAME*);

**SLIST\_FOREACH\_SAFE**(*TYPE \*var*, *SLIST\_HEAD \*head*, *SLIST\_ENTRY NAME*, *TYPE \*temp\_var*);

**SLIST\_HEAD**(*HEADNAME*, *TYPE*);

**SLIST\_HEAD\_INITIALIZER**(*SLIST\_HEAD head*);

**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\_NEXT**(*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*);

**STAILQ\_CONCAT**(*STAILQ\_HEAD \*head1*, *STAILQ\_HEAD \*head2*);

**STAILQ\_EMPTY**(*STAILQ\_HEAD \*head*);

**STAILQ\_ENTRY**(*TYPE*);

**STAILQ\_FIRST**(*STAILQ\_HEAD \*head*);

**STAILQ\_FOREACH**(*TYPE \*var*, *STAILQ\_HEAD \*head*, *STAILQ\_ENTRY NAME*);

**STAILQ\_FOREACH\_SAFE**(*TYPE \*var*, *STAILQ\_HEAD \*head*, *STAILQ\_ENTRY NAME*, *TYPE \*temp\_var*);

**STAILQ\_HEAD**(*HEADNAME*, *TYPE*);

**STAILQ\_HEAD\_INITIALIZER**(*STAILQ\_HEAD head*);

**STAILQ\_INIT**(*STAILQ\_HEAD \*head*);

**STAILQ\_INSERT\_AFTER**(*STAILQ\_HEAD \*head*, *TYPE \*listelm*, *TYPE \*elm*, *STAILQ\_ENTRY NAME*);

**STAILQ\_INSERT\_HEAD**(*STAILQ\_HEAD \*head*, *TYPE \*elm*, *STAILQ\_ENTRY NAME*);

**STAILQ\_INSERT\_TAIL**(*STAILQ\_HEAD \*head*, *TYPE \*elm*, *STAILQ\_ENTRY NAME*);

**STAILQ\_LAST**(*STAILQ\_HEAD \*head*, *TYPE*, *STAILQ\_ENTRY NAME*);

**STAILQ\_NEXT**(*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*);

**LIST\_EMPTY**(*LIST\_HEAD \*head*);

**LIST\_ENTRY**(*TYPE*);

**LIST\_FIRST**(*LIST\_HEAD \*head*);

**LIST\_FOREACH**(*TYPE \*var*, *LIST\_HEAD \*head*, *LIST\_ENTRY NAME*);

**LIST\_FOREACH\_SAFE**(*TYPE \*var*, *LIST\_HEAD \*head*, *LIST\_ENTRY NAME*, *TYPE \*temp\_var*);

**LIST\_HEAD**(*HEADNAME*, *TYPE*);

**LIST\_HEAD\_INITIALIZER**(*LIST\_HEAD head*);

**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\_NEXT**(*TYPE \*elm*, *LIST\_ENTRY NAME*);

**LIST\_REMOVE**(*TYPE \*elm*, *LIST\_ENTRY NAME*);

**TAILQ\_CONCAT**(*TAILQ\_HEAD \*head1*, *TAILQ\_HEAD \*head2*, *TAILQ\_ENTRY NAME*);

**TAILQ\_EMPTY**(*TAILQ\_HEAD \*head*);

**TAILQ\_ENTRY**(*TYPE*);

**TAILQ\_FIRST**(*TAILQ\_HEAD \*head*);

**TAILQ\_FOREACH**(*TYPE \*var*, *TAILQ\_HEAD \*head*, *TAILQ\_ENTRY NAME*);

**TAILQ\_FOREACH\_SAFE**(*TYPE \*var*, *TAILQ\_HEAD \*head*, *TAILQ\_ENTRY NAME*, *TYPE \*temp\_var*);

**TAILQ\_FOREACH\_REVERSE**(*TYPE \*var*, *TAILQ\_HEAD \*head*, *HEADNAME*, *TAILQ\_ENTRY NAME*);

**TAILQ\_FOREACH\_REVERSE\_SAFE**(*TYPE \*var*, *TAILQ\_HEAD \*head*, *HEADNAME*, *TAILQ\_ENTRY NAME*, *TYPE \*temp\_var*);

**TAILQ\_HEAD**(*HEADNAME*, *TYPE*);

**TAILQ\_HEAD\_INITIALIZER**(*TAILQ\_HEAD head*);

**TAILQ\_INIT**(*TAILQ\_HEAD \*head*);

**TAILQ\_INSERT\_AFTER**(*TAILQ\_HEAD \*head*, *TYPE \*listelm*, *TYPE \*elm*, *TAILQ\_ENTRY NAME*);

**TAILQ\_INSERT\_BEFORE**(*TYPE \*listelm*, *TYPE \*elm*, *TAILQ\_ENTRY NAME*);

**TAILQ\_INSERT\_HEAD**(*TAILQ\_HEAD \*head*, *TYPE \*elm*, *TAILQ\_ENTRY NAME*);

**TAILQ\_INSERT\_TAIL**(*TAILQ\_HEAD \*head*, *TYPE \*elm*, *TAILQ\_ENTRY NAME*);

**TAILQ\_LAST**(*TAILQ\_HEAD \*head*, *HEADNAME*);

**TAILQ\_NEXT**(*TYPE \*elm*, *TAILQ\_ENTRY NAME*);

**TAILQ\_PREV**(*TYPE \*elm*, *HEADNAME*, *TAILQ\_ENTRY NAME*);

**TAILQ\_REMOVE**(*TAILQ\_HEAD \*head*, *TYPE \*elm*, *TAILQ\_ENTRY NAME*);

## DESCRIPTION

These macros define and operate on four types of data structures: singly-linked lists, singly-linked tail queues, lists, and tail queues. All four structures support the following functionality:

1. Insertion of a new entry at the head of the list.

2. Insertion of a new entry after any element in the list.

3. O(1) removal of an entry from the head of the list.

4. O(n) removal of any entry in the list.

5. Forward traversal through the list.

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.

Singly-linked tail queues add the following functionality:

1. Entries can be added at the end of a list.

2. They may be concatenated.

However:

1. All list insertions 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 singly-linked lists.

Singly-linked tailqs 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 and tail queues) additionally allow:

1. Insertion of a new entry before any element in the list.

2. O(1) removal of any entry in the list.

However:

1. Each elements requires two pointers rather than one.

2. Code size and execution time of operations (except for removal) is about twice that of the singly-linked data-structures.

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:

1. Entries can be added at the end of a list.

2. They may be traversed backwards, from tail to head.

3. They may be concatenated.

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. Code size is about 15% greater and operations run about 20% slower than singly-linked lists.

In the macro definitions, *TYPE* is the name of a user defined structure, that must contain a field of type SLIST\_ENTRY, STAILQ\_ENTRY, LIST\_ENTRY, or TAILQ\_ENTRY, named *NAME*. The argument *HEADNAME* is the name of a user defined structure that must be declared using the macros SLIST\_HEAD, STAILQ\_HEAD, LIST\_HEAD, or TAILQ\_HEAD. See the examples below for further explanation of how these macros are used.

## SINGLY-LINKED LISTS

A singly-linked list is headed by a structure defined by the **SLIST\_HEAD** macro. This structure contains a single pointer to the first element on the list. The elements are singly linked for minimum space and pointer manipulation overhead at the expense of O(n) removal for arbitrary elements. New elements can be added to the list after an existing element or at the head of the list. An *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\_FOREACH\_SAFE** traverses the list referenced by *head* in the forward direction, assigning each element in turn to *var*. However, unlike **SLIST\_FOREACH**() here it is permitted to both remove *var* as well as free it from within the loop safely without interfering with the traversal.

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\_HEAD** removes the element *elm* 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.

The macro **SLIST\_REMOVE** removes the element *elm* from the list.

## SINGLY-LINKED LIST EXAMPLE

```
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-> ...
					/* Safe forward traversal. */
SLIST_FOREACH_SAFE(np, &head, entries, np_temp) {
	np->do_stuff();
	...
	SLIST_REMOVE(&head, np, entry, entries);
	free(np);
}
while (!SLIST_EMPTY(&head)) {		/* List Deletion. */
	n1 = SLIST_FIRST(&head);
	SLIST_REMOVE_HEAD(&head, entries);
	free(n1);
}
```

## SINGLY-LINKED TAIL QUEUES

A singly-linked tail queue is headed by a structure defined by the **STAILQ\_HEAD** 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 singly linked for minimum space and pointer manipulation overhead at the expense of O(n) removal for arbitrary elements. New elements can be added to the tail queue after an existing element, at the head of the tail queue, or at the end of the tail queue. A *STAILQ\_HEAD* structure is declared as follows:

```
STAILQ_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 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 **STAILQ\_HEAD\_INITIALIZER** evaluates to an initializer for the tail queue *head*.

The macro **STAILQ\_CONCAT** concatenates the tail queue headed by *head2* onto the end of the one headed by *head1* removing all entries from the former.

The macro **STAILQ\_EMPTY** evaluates to true if there are no items on the tail queue.

The macro **STAILQ\_ENTRY** declares a structure that connects the elements in the tail queue.

The macro **STAILQ\_FIRST** returns the first item on the tail queue or NULL if the tail queue is empty.

The macro **STAILQ\_FOREACH** traverses the tail queue referenced by *head* in the forward direction, assigning each element in turn to *var*.

The macro **STAILQ\_FOREACH\_SAFE** traverses the tail queue referenced by *head* in the forward direction, assigning each element in turn to *var*. However, unlike **STAILQ\_FOREACH**() here it is permitted to both remove *var* as well as free it from within the loop safely without interfering with the traversal.

The macro **STAILQ\_INIT** initializes the tail queue referenced by *head*.

The macro **STAILQ\_INSERT\_HEAD** inserts the new element *elm* at the head of the tail queue.

The macro **STAILQ\_INSERT\_TAIL** inserts the new element *elm* at the end of the tail queue.

The macro **STAILQ\_INSERT\_AFTER** inserts the new element *elm* after the element *listelm*.

The macro **STAILQ\_LAST** returns the last item on the tail queue. If the tail queue is empty the return value is NULL.

The macro **STAILQ\_NEXT** returns the next item on the tail queue, or NULL this item is the last.

The macro **STAILQ\_REMOVE\_HEAD** removes the element at the head of the tail queue. For optimum efficiency, elements being removed from the head of the tail queue should use this macro explicitly rather than the generic *STAILQ\_REMOVE* macro.

The macro **STAILQ\_REMOVE** removes the element *elm* from the tail queue.

## SINGLY-LINKED TAIL QUEUE EXAMPLE

```
STAILQ_HEAD(stailhead, entry) head =
    STAILQ_HEAD_INITIALIZER(head);
struct stailhead *headp;		/* Singly-linked tail queue head. */
struct entry {
	...
	STAILQ_ENTRY(entry) entries;	/* Tail queue. */
	...
} *n1, *n2, *n3, *np;
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);
					/* Deletion. */
STAILQ_REMOVE(&head, n2, entry, entries);
free(n2);
					/* Deletion from the head. */
n3 = STAILQ_FIRST(&head);
STAILQ_REMOVE_HEAD(&head, entries);
free(n3);
					/* Forward traversal. */
STAILQ_FOREACH(np, &head, entries)
	np-> ...
					/* Safe forward traversal. */
STAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
	np->do_stuff();
	...
	STAILQ_REMOVE(&head, np, entry, entries);
	free(np);
}
					/* TailQ Deletion. */
while (!STAILQ_EMPTY(&head)) {
	n1 = STAILQ_FIRST(&head);
	STAILQ_REMOVE_HEAD(&head, entries);
	free(n1);
}
					/* Faster TailQ Deletion. */
n1 = STAILQ_FIRST(&head);
while (n1 != NULL) {
	n2 = STAILQ_NEXT(n1, entries);
	free(n1);
	n1 = n2;
}
STAILQ_INIT(&head);
```

## LISTS

A list is headed by a structure defined by the **LIST\_HEAD** 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 *LIST\_HEAD* structure is declared as follows:

```
LIST_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 **LIST\_HEAD\_INITIALIZER** evaluates to an initializer for the list *head*.

The macro **LIST\_EMPTY** evaluates to true if there are no elements in the list.

The macro **LIST\_ENTRY** declares a structure that connects the elements in the list.

The macro **LIST\_FIRST** returns the first element in the list or NULL if the list is empty.

The macro **LIST\_FOREACH** traverses the list referenced by *head* in the forward direction, assigning each element in turn to *var*.

The macro **LIST\_FOREACH\_SAFE** traverses the list referenced by *head* in the forward direction, assigning each element in turn to *var*. However, unlike **LIST\_FOREACH**() here it is permitted to both remove *var* as well as free it from within the loop safely without interfering with the traversal.

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\_NEXT** returns the next element in the list, or NULL if this is the last.

The macro **LIST\_REMOVE** removes the element *elm* from the list.

## LIST EXAMPLE

```
LIST_HEAD(listhead, entry) head =
    LIST_HEAD_INITIALIZER(head);
struct listhead *headp;			/* List head. */
struct entry {
	...
	LIST_ENTRY(entry) entries;	/* List. */
	...
} *n1, *n2, *n3, *np, *np_temp;
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);
n3 = malloc(sizeof(struct entry));	/* Insert before. */
LIST_INSERT_BEFORE(n2, n3, entries);
LIST_REMOVE(n2, entries);		/* Deletion. */
free(n2);
					/* Forward traversal. */
LIST_FOREACH(np, &head, entries)
	np-> ...
					/* Safe forward traversal. */
LIST_FOREACH_SAFE(np, &head, entries, np_temp) {
	np->do_stuff();
	...
	LIST_REMOVE(np, entries);
	free(np);
}
while (!LIST_EMPTY(&head)) {		/* List Deletion. */
	n1 = LIST_FIRST(&head);
	LIST_REMOVE(n1, entries);
	free(n1);
}
n1 = LIST_FIRST(&head);			/* Faster List Deletion. */
while (n1 != NULL) {
	n2 = LIST_NEXT(n1, entries);
	free(n1);
	n1 = n2;
}
LIST_INIT(&head);
```

## TAIL QUEUES

A tail queue is headed by a structure defined by the **TAILQ\_HEAD** 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 tail queue after an existing element, before an existing element, at the head of the tail queue, or at the end of the tail queue. A *TAILQ\_HEAD* structure is declared as follows:

```
TAILQ_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 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 **TAILQ\_HEAD\_INITIALIZER** evaluates to an initializer for the tail queue *head*.

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.

The macro **TAILQ\_EMPTY** evaluates to true if there are no items on the tail queue.

The macro **TAILQ\_ENTRY** declares a structure that connects the elements in the tail queue.

The macro **TAILQ\_FIRST** returns the first item on the tail queue or NULL if the tail queue is empty.

The macro **TAILQ\_FOREACH** traverses the tail queue referenced by *head* in the forward direction, assigning each element in turn to *var*. *var* is set to NULL if the loop completes normally, or if there were no elements.

The macro **TAILQ\_FOREACH\_REVERSE** traverses the tail queue referenced by *head* in the reverse direction, assigning each element in turn to *var*.

The macros **TAILQ\_FOREACH\_SAFE** and **TAILQ\_FOREACH\_REVERSE\_SAFE** traverse the list referenced by *head* in the forward or reverse direction respectively, assigning each element in turn to *var*. However, unlike their unsafe counterparts, **TAILQ\_FOREACH** and **TAILQ\_FOREACH\_REVERSE** permit to both remove *var* as well as free it from within the loop safely without interfering with the traversal.

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\_LAST** returns the last item on the tail queue. If the tail queue is empty the return value is NULL.

The macro **TAILQ\_NEXT** returns the next item on the tail queue, or NULL if this item is the last.

The macro **TAILQ\_PREV** returns the previous item on the tail queue, or NULL if this item is the first.

The macro **TAILQ\_REMOVE** removes the element *elm* from the tail queue.

## TAIL QUEUE EXAMPLE

```
TAILQ_HEAD(tailhead, entry) head =
    TAILQ_HEAD_INITIALIZER(head);
struct tailhead *headp;			/* Tail queue head. */
struct entry {
	...
	TAILQ_ENTRY(entry) entries;	/* Tail queue. */
	...
} *n1, *n2, *n3, *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);
n3 = malloc(sizeof(struct entry));	/* Insert before. */
TAILQ_INSERT_BEFORE(n2, n3, entries);
TAILQ_REMOVE(&head, n2, entries);	/* Deletion. */
free(n2);
					/* Forward traversal. */
TAILQ_FOREACH(np, &head, entries)
	np-> ...
					/* Safe forward traversal. */
TAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
	np->do_stuff();
	...
	TAILQ_REMOVE(&head, np, entries);
	free(np);
}
					/* Reverse traversal. */
TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
	np-> ...
					/* TailQ Deletion. */
while (!TAILQ_EMPTY(&head)) {
	n1 = TAILQ_FIRST(&head);
	TAILQ_REMOVE(&head, n1, entries);
	free(n1);
}
					/* Faster TailQ Deletion. */
n1 = TAILQ_FIRST(&head);
while (n1 != NULL) {
	n2 = TAILQ_NEXT(n1, entries);
	free(n1);
	n1 = n2;
}
TAILQ_INIT(&head);
```

## HISTORY

The **queue** functions first appeared in 4.4BSD.
