2 .\" The Regents of the University of California. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\" must display the following acknowledgement:
14 .\" This product includes software developed by the University of
15 .\" California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" @(#)queue.3 8.2 (Berkeley) 1/24/94
33 .\" $FreeBSD: /repoman/r/ncvs/src/share/man/man3/queue.3,v 1.37 2006/03/22 02:40:38 mckusick Exp $
43 .Nm SLIST_FOREACH_SAFE ,
45 .Nm SLIST_HEAD_INITIALIZER ,
47 .Nm SLIST_INSERT_AFTER ,
48 .Nm SLIST_INSERT_HEAD ,
50 .Nm SLIST_REMOVE_HEAD ,
57 .Nm STAILQ_FOREACH_SAFE ,
59 .Nm STAILQ_HEAD_INITIALIZER ,
61 .Nm STAILQ_INSERT_AFTER ,
62 .Nm STAILQ_INSERT_HEAD ,
63 .Nm STAILQ_INSERT_TAIL ,
66 .Nm STAILQ_REMOVE_HEAD ,
72 .Nm LIST_FOREACH_SAFE ,
74 .Nm LIST_HEAD_INITIALIZER ,
76 .Nm LIST_INSERT_AFTER ,
77 .Nm LIST_INSERT_BEFORE ,
78 .Nm LIST_INSERT_HEAD ,
86 .Nm TAILQ_FOREACH_SAFE ,
87 .Nm TAILQ_FOREACH_REVERSE ,
88 .Nm TAILQ_FOREACH_REVERSE_SAFE ,
90 .Nm TAILQ_HEAD_INITIALIZER ,
92 .Nm TAILQ_INSERT_AFTER ,
93 .Nm TAILQ_INSERT_BEFORE ,
94 .Nm TAILQ_INSERT_HEAD ,
95 .Nm TAILQ_INSERT_TAIL ,
100 .Nd implementations of singly-linked lists, singly-linked tail queues,
101 lists and tail queues
105 .Fn SLIST_EMPTY "SLIST_HEAD *head"
106 .Fn SLIST_ENTRY "TYPE"
107 .Fn SLIST_FIRST "SLIST_HEAD *head"
108 .Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
109 .Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
110 .Fn SLIST_HEAD "HEADNAME" "TYPE"
111 .Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
112 .Fn SLIST_INIT "SLIST_HEAD *head"
113 .Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
114 .Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
115 .Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
116 .Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
117 .Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
119 .Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2"
120 .Fn STAILQ_EMPTY "STAILQ_HEAD *head"
121 .Fn STAILQ_ENTRY "TYPE"
122 .Fn STAILQ_FIRST "STAILQ_HEAD *head"
123 .Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
124 .Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
125 .Fn STAILQ_HEAD "HEADNAME" "TYPE"
126 .Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
127 .Fn STAILQ_INIT "STAILQ_HEAD *head"
128 .Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
129 .Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
130 .Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
131 .Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE" "STAILQ_ENTRY NAME"
132 .Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
133 .Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
134 .Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
136 .Fn LIST_EMPTY "LIST_HEAD *head"
137 .Fn LIST_ENTRY "TYPE"
138 .Fn LIST_FIRST "LIST_HEAD *head"
139 .Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
140 .Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
141 .Fn LIST_HEAD "HEADNAME" "TYPE"
142 .Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
143 .Fn LIST_INIT "LIST_HEAD *head"
144 .Fn LIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
145 .Fn LIST_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "LIST_ENTRY NAME"
146 .Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
147 .Fn LIST_NEXT "TYPE *elm" "LIST_ENTRY NAME"
148 .Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
150 .Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME"
151 .Fn TAILQ_EMPTY "TAILQ_HEAD *head"
152 .Fn TAILQ_ENTRY "TYPE"
153 .Fn TAILQ_FIRST "TAILQ_HEAD *head"
154 .Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
155 .Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
156 .Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
157 .Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
158 .Fn TAILQ_HEAD "HEADNAME" "TYPE"
159 .Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
160 .Fn TAILQ_INIT "TAILQ_HEAD *head"
161 .Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
162 .Fn TAILQ_INSERT_BEFORE "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
163 .Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
164 .Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
165 .Fn TAILQ_LAST "TAILQ_HEAD *head" "HEADNAME"
166 .Fn TAILQ_NEXT "TYPE *elm" "TAILQ_ENTRY NAME"
167 .Fn TAILQ_PREV "TYPE *elm" "HEADNAME" "TAILQ_ENTRY NAME"
168 .Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
171 These macros define and operate on four types of data structures:
172 singly-linked lists, singly-linked tail queues, lists, and tail queues.
173 All four structures support the following functionality:
174 .Bl -enum -compact -offset indent
176 Insertion of a new entry at the head of the list.
178 Insertion of a new entry after any element in the list.
180 O(1) removal of an entry from the head of the list.
182 O(n) removal of any entry in the list.
184 Forward traversal through the list.
187 Singly-linked lists are the simplest of the five data structures
188 and support only the above functionality.
189 Singly-linked lists are ideal for applications with large datasets
190 and few or no removals,
191 or for implementing a LIFO queue.
193 Singly-linked tail queues add the following functionality:
194 .Bl -enum -compact -offset indent
196 Entries can be added at the end of a list.
198 They may be concatenated.
201 .Bl -enum -compact -offset indent
203 All list insertions must specify the head of the list.
205 Each head entry requires two pointers rather than one.
207 Code size is about 15% greater and operations run about 20% slower
208 than singly-linked lists.
211 Singly-linked tailqs are ideal for applications with large datasets and
213 or for implementing a FIFO queue.
215 All doubly linked types of data structures (lists and tail queues)
217 .Bl -enum -compact -offset indent
219 Insertion of a new entry before any element in the list.
221 O(1) removal of any entry in the list.
224 .Bl -enum -compact -offset indent
226 Each elements requires two pointers rather than one.
228 Code size and execution time of operations (except for removal) is about
229 twice that of the singly-linked data-structures.
232 Linked lists are the simplest of the doubly linked data structures and support
233 only the above functionality over singly-linked lists.
235 Tail queues add the following functionality:
236 .Bl -enum -compact -offset indent
238 Entries can be added at the end of a list.
240 They may be traversed backwards, from tail to head.
242 They may be concatenated.
245 .Bl -enum -compact -offset indent
247 All list insertions and removals must specify the head of the list.
249 Each head entry requires two pointers rather than one.
251 Code size is about 15% greater and operations run about 20% slower
252 than singly-linked lists.
255 In the macro definitions,
257 is the name of a user defined structure,
258 that must contain a field of type
268 is the name of a user defined structure that must be declared
275 See the examples below for further explanation of how these
277 .Sh SINGLY-LINKED LISTS
278 A singly-linked list is headed by a structure defined by the
281 This structure contains a single pointer to the first element
283 The elements are singly linked for minimum space and pointer manipulation
284 overhead at the expense of O(n) removal for arbitrary elements.
285 New elements can be added to the list after an existing element or
286 at the head of the list.
289 structure is declared as follows:
290 .Bd -literal -offset indent
291 SLIST_HEAD(HEADNAME, TYPE) head;
296 is the name of the structure to be defined, and
298 is the type of the elements to be linked into the list.
299 A pointer to the head of the list can later be declared as:
300 .Bd -literal -offset indent
301 struct HEADNAME *headp;
308 are user selectable.)
311 .Nm SLIST_HEAD_INITIALIZER
312 evaluates to an initializer for the list
317 evaluates to true if there are no elements in the list.
321 declares a structure that connects the elements in
326 returns the first element in the list or NULL if the list is empty.
330 traverses the list referenced by
332 in the forward direction, assigning each element in
337 .Nm SLIST_FOREACH_SAFE
338 traverses the list referenced by
340 in the forward direction, assigning each element in
345 here it is permitted to both remove
347 as well as free it from within the loop safely without interfering with the
352 initializes the list referenced by
356 .Nm SLIST_INSERT_HEAD
357 inserts the new element
359 at the head of the list.
362 .Nm SLIST_INSERT_AFTER
363 inserts the new element
370 returns the next element in the list.
373 .Nm SLIST_REMOVE_HEAD
376 from the head of the list.
377 For optimum efficiency,
378 elements being removed from the head of the list should explicitly use
379 this macro instead of the generic
388 .Sh SINGLY-LINKED LIST EXAMPLE
390 SLIST_HEAD(slisthead, entry) head =
391 SLIST_HEAD_INITIALIZER(head);
392 struct slisthead *headp; /* Singly-linked List head. */
395 SLIST_ENTRY(entry) entries; /* Singly-linked List. */
397 } *n1, *n2, *n3, *np;
399 SLIST_INIT(&head); /* Initialize the list. */
401 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
402 SLIST_INSERT_HEAD(&head, n1, entries);
404 n2 = malloc(sizeof(struct entry)); /* Insert after. */
405 SLIST_INSERT_AFTER(n1, n2, entries);
407 SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
410 n3 = SLIST_FIRST(&head);
411 SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head. */
413 /* Forward traversal. */
414 SLIST_FOREACH(np, &head, entries)
416 /* Safe forward traversal. */
417 SLIST_FOREACH_SAFE(np, &head, entries, np_temp) {
420 SLIST_REMOVE(&head, np, entry, entries);
424 while (!SLIST_EMPTY(&head)) { /* List Deletion. */
425 n1 = SLIST_FIRST(&head);
426 SLIST_REMOVE_HEAD(&head, entries);
430 .Sh SINGLY-LINKED TAIL QUEUES
431 A singly-linked tail queue is headed by a structure defined by the
434 This structure contains a pair of pointers,
435 one to the first element in the tail queue and the other to
436 the last element in the tail queue.
437 The elements are singly linked for minimum space and pointer
438 manipulation overhead at the expense of O(n) removal for arbitrary
440 New elements can be added to the tail queue after an existing element,
441 at the head of the tail queue, or at the end of the tail queue.
444 structure is declared as follows:
445 .Bd -literal -offset indent
446 STAILQ_HEAD(HEADNAME, TYPE) head;
451 is the name of the structure to be defined, and
453 is the type of the elements to be linked into the tail queue.
454 A pointer to the head of the tail queue can later be declared as:
455 .Bd -literal -offset indent
456 struct HEADNAME *headp;
463 are user selectable.)
466 .Nm STAILQ_HEAD_INITIALIZER
467 evaluates to an initializer for the tail queue
472 concatenates the tail queue headed by
474 onto the end of the one headed by
476 removing all entries from the former.
480 evaluates to true if there are no items on the tail queue.
484 declares a structure that connects the elements in
489 returns the first item on the tail queue or NULL if the tail queue
494 traverses the tail queue referenced by
496 in the forward direction, assigning each element
501 .Nm STAILQ_FOREACH_SAFE
502 traverses the tail queue referenced by
504 in the forward direction, assigning each element
509 here it is permitted to both remove
511 as well as free it from within the loop safely without interfering with the
516 initializes the tail queue referenced by
520 .Nm STAILQ_INSERT_HEAD
521 inserts the new element
523 at the head of the tail queue.
526 .Nm STAILQ_INSERT_TAIL
527 inserts the new element
529 at the end of the tail queue.
532 .Nm STAILQ_INSERT_AFTER
533 inserts the new element
540 returns the last item on the tail queue.
541 If the tail queue is empty the return value is NULL.
545 returns the next item on the tail queue, or NULL this item is the last.
548 .Nm STAILQ_REMOVE_HEAD
549 removes the element at the head of the tail queue.
550 For optimum efficiency,
551 elements being removed from the head of the tail queue should
552 use this macro explicitly rather than the generic
561 .Sh SINGLY-LINKED TAIL QUEUE EXAMPLE
563 STAILQ_HEAD(stailhead, entry) head =
564 STAILQ_HEAD_INITIALIZER(head);
565 struct stailhead *headp; /* Singly-linked tail queue head. */
568 STAILQ_ENTRY(entry) entries; /* Tail queue. */
570 } *n1, *n2, *n3, *np;
572 STAILQ_INIT(&head); /* Initialize the queue. */
574 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
575 STAILQ_INSERT_HEAD(&head, n1, entries);
577 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
578 STAILQ_INSERT_TAIL(&head, n1, entries);
580 n2 = malloc(sizeof(struct entry)); /* Insert after. */
581 STAILQ_INSERT_AFTER(&head, n1, n2, entries);
583 STAILQ_REMOVE(&head, n2, entry, entries);
585 /* Deletion from the head. */
586 n3 = STAILQ_FIRST(&head);
587 STAILQ_REMOVE_HEAD(&head, entries);
589 /* Forward traversal. */
590 STAILQ_FOREACH(np, &head, entries)
592 /* Safe forward traversal. */
593 STAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
596 STAILQ_REMOVE(&head, np, entry, entries);
599 /* TailQ Deletion. */
600 while (!STAILQ_EMPTY(&head)) {
601 n1 = STAILQ_FIRST(&head);
602 STAILQ_REMOVE_HEAD(&head, entries);
605 /* Faster TailQ Deletion. */
606 n1 = STAILQ_FIRST(&head);
608 n2 = STAILQ_NEXT(n1, entries);
615 A list is headed by a structure defined by the
618 This structure contains a single pointer to the first element
620 The elements are doubly linked so that an arbitrary element can be
621 removed without traversing the list.
622 New elements can be added to the list after an existing element,
623 before an existing element, or at the head of the list.
626 structure is declared as follows:
627 .Bd -literal -offset indent
628 LIST_HEAD(HEADNAME, TYPE) head;
633 is the name of the structure to be defined, and
635 is the type of the elements to be linked into the list.
636 A pointer to the head of the list can later be declared as:
637 .Bd -literal -offset indent
638 struct HEADNAME *headp;
645 are user selectable.)
648 .Nm LIST_HEAD_INITIALIZER
649 evaluates to an initializer for the list
654 evaluates to true if there are no elements in the list.
658 declares a structure that connects the elements in
663 returns the first element in the list or NULL if the list
668 traverses the list referenced by
670 in the forward direction, assigning each element in turn to
674 .Nm LIST_FOREACH_SAFE
675 traverses the list referenced by
677 in the forward direction, assigning each element in turn to
681 here it is permitted to both remove
683 as well as free it from within the loop safely without interfering with the
688 initializes the list referenced by
693 inserts the new element
695 at the head of the list.
698 .Nm LIST_INSERT_AFTER
699 inserts the new element
705 .Nm LIST_INSERT_BEFORE
706 inserts the new element
713 returns the next element in the list, or NULL if this is the last.
722 LIST_HEAD(listhead, entry) head =
723 LIST_HEAD_INITIALIZER(head);
724 struct listhead *headp; /* List head. */
727 LIST_ENTRY(entry) entries; /* List. */
729 } *n1, *n2, *n3, *np, *np_temp;
731 LIST_INIT(&head); /* Initialize the list. */
733 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
734 LIST_INSERT_HEAD(&head, n1, entries);
736 n2 = malloc(sizeof(struct entry)); /* Insert after. */
737 LIST_INSERT_AFTER(n1, n2, entries);
739 n3 = malloc(sizeof(struct entry)); /* Insert before. */
740 LIST_INSERT_BEFORE(n2, n3, entries);
742 LIST_REMOVE(n2, entries); /* Deletion. */
744 /* Forward traversal. */
745 LIST_FOREACH(np, &head, entries)
748 /* Safe forward traversal. */
749 LIST_FOREACH_SAFE(np, &head, entries, np_temp) {
752 LIST_REMOVE(np, entries);
756 while (!LIST_EMPTY(&head)) { /* List Deletion. */
757 n1 = LIST_FIRST(&head);
758 LIST_REMOVE(n1, entries);
762 n1 = LIST_FIRST(&head); /* Faster List Deletion. */
764 n2 = LIST_NEXT(n1, entries);
771 A tail queue is headed by a structure defined by the
774 This structure contains a pair of pointers,
775 one to the first element in the tail queue and the other to
776 the last element in the tail queue.
777 The elements are doubly linked so that an arbitrary element can be
778 removed without traversing the tail queue.
779 New elements can be added to the tail queue after an existing element,
780 before an existing element, at the head of the tail queue,
781 or at the end of the tail queue.
784 structure is declared as follows:
785 .Bd -literal -offset indent
786 TAILQ_HEAD(HEADNAME, TYPE) head;
791 is the name of the structure to be defined, and
793 is the type of the elements to be linked into the tail queue.
794 A pointer to the head of the tail queue can later be declared as:
795 .Bd -literal -offset indent
796 struct HEADNAME *headp;
803 are user selectable.)
806 .Nm TAILQ_HEAD_INITIALIZER
807 evaluates to an initializer for the tail queue
812 concatenates the tail queue headed by
814 onto the end of the one headed by
816 removing all entries from the former.
820 evaluates to true if there are no items on the tail queue.
824 declares a structure that connects the elements in
829 returns the first item on the tail queue or NULL if the tail queue
834 traverses the tail queue referenced by
836 in the forward direction, assigning each element in turn to
841 if the loop completes normally, or if there were no elements.
844 .Nm TAILQ_FOREACH_REVERSE
845 traverses the tail queue referenced by
847 in the reverse direction, assigning each element in turn to
851 .Nm TAILQ_FOREACH_SAFE
853 .Nm TAILQ_FOREACH_REVERSE_SAFE
854 traverse the list referenced by
856 in the forward or reverse direction respectively,
857 assigning each element in turn to
859 However, unlike their unsafe counterparts,
862 .Nm TAILQ_FOREACH_REVERSE
863 permit to both remove
865 as well as free it from within the loop safely without interfering with the
870 initializes the tail queue referenced by
874 .Nm TAILQ_INSERT_HEAD
875 inserts the new element
877 at the head of the tail queue.
880 .Nm TAILQ_INSERT_TAIL
881 inserts the new element
883 at the end of the tail queue.
886 .Nm TAILQ_INSERT_AFTER
887 inserts the new element
893 .Nm TAILQ_INSERT_BEFORE
894 inserts the new element
901 returns the last item on the tail queue.
902 If the tail queue is empty the return value is NULL.
906 returns the next item on the tail queue, or NULL if this item is the last.
910 returns the previous item on the tail queue, or NULL if this item
918 .Sh TAIL QUEUE EXAMPLE
920 TAILQ_HEAD(tailhead, entry) head =
921 TAILQ_HEAD_INITIALIZER(head);
922 struct tailhead *headp; /* Tail queue head. */
925 TAILQ_ENTRY(entry) entries; /* Tail queue. */
927 } *n1, *n2, *n3, *np;
929 TAILQ_INIT(&head); /* Initialize the queue. */
931 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
932 TAILQ_INSERT_HEAD(&head, n1, entries);
934 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
935 TAILQ_INSERT_TAIL(&head, n1, entries);
937 n2 = malloc(sizeof(struct entry)); /* Insert after. */
938 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
940 n3 = malloc(sizeof(struct entry)); /* Insert before. */
941 TAILQ_INSERT_BEFORE(n2, n3, entries);
943 TAILQ_REMOVE(&head, n2, entries); /* Deletion. */
945 /* Forward traversal. */
946 TAILQ_FOREACH(np, &head, entries)
948 /* Safe forward traversal. */
949 TAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
952 TAILQ_REMOVE(&head, np, entries);
955 /* Reverse traversal. */
956 TAILQ_FOREACH_REVERSE(np, &head, tailhead, entries)
958 /* TailQ Deletion. */
959 while (!TAILQ_EMPTY(&head)) {
960 n1 = TAILQ_FIRST(&head);
961 TAILQ_REMOVE(&head, n1, entries);
964 /* Faster TailQ Deletion. */
965 n1 = TAILQ_FIRST(&head);
967 n2 = TAILQ_NEXT(n1, entries);
976 functions first appeared in