STAILQ(3)
π STAILQ(3) β the singly linked tail queue glow-up π
okay bestie, sit down, hydrate, because today we're talking about STAILQ β that's the singly linked tail queue implementation from the Library Functions Manual (yes, section 3, the libc section, we love her). She's a bunch of macros, she's ancient, she's iconic, and she will absolutely make your linked lists cute and fast. Let's get into it. π¦
β¨ NAME β¨
So the whole girl squad is here, and there's a lot of them, because every macro gets a name-drop:
SIMPLEQ_EMPTY, SIMPLEQ_ENTRY, SIMPLEQ_FIRST, SIMPLEQ_FOREACH, SIMPLEQ_HEAD, SIMPLEQ_HEAD_INITIALIZER, SIMPLEQ_INIT, SIMPLEQ_INSERT_AFTER, SIMPLEQ_INSERT_HEAD, SIMPLEQ_INSERT_TAIL, SIMPLEQ_NEXT, SIMPLEQ_REMOVE, SIMPLEQ_REMOVE_HEAD, STAILQ_CONCAT, STAILQ_EMPTY, STAILQ_ENTRY, STAILQ_FIRST, STAILQ_FOREACH, STAILQ_HEAD, STAILQ_HEAD_INITIALIZER, STAILQ_INIT, STAILQ_INSERT_AFTER, STAILQ_INSERT_HEAD, STAILQ_INSERT_TAIL, STAILQ_NEXT, STAILQ_REMOVE, STAILQ_REMOVE_HEAD
β¦and their whole collective job description is: implementation of a singly linked tail queue. That's it. That's the gig. π
π LIBRARY π
Standard C library (libc, -lc) β she lives in your libc, no extra install, no cap. π«Ά
πΈ SYNOPSIS πΈ
here's the whole lineup, verbatim, because we do not paraphrase signatures in this house:
#include <sys/queue.h>
STAILQ_ENTRY(TYPE);
STAILQ_HEAD(HEADNAME, TYPE);
STAILQ_HEAD STAILQ_HEAD_INITIALIZER(STAILQ_HEAD head);
void STAILQ_INIT(STAILQ_HEAD *head);
int STAILQ_EMPTY(STAILQ_HEAD *head);
void STAILQ_INSERT_HEAD(STAILQ_HEAD *head,
struct TYPE *elm, STAILQ_ENTRY NAME);
void STAILQ_INSERT_TAIL(STAILQ_HEAD *head,
struct TYPE *elm, STAILQ_ENTRY NAME);
void STAILQ_INSERT_AFTER(STAILQ_HEAD *head, struct TYPE *listelm,
struct TYPE *elm, STAILQ_ENTRY NAME);
struct TYPE *STAILQ_FIRST(STAILQ_HEAD *head);
struct TYPE *STAILQ_NEXT(struct TYPE *elm, STAILQ_ENTRY NAME);
STAILQ_FOREACH(struct TYPE *var, STAILQ_HEAD *head, STAILQ_ENTRY NAME);
void STAILQ_REMOVE(STAILQ_HEAD *head, struct TYPE *elm, TYPE,
STAILQ_ENTRY NAME);
void STAILQ_REMOVE_HEAD(STAILQ_HEAD *head,
STAILQ_ENTRY NAME);
void STAILQ_CONCAT(STAILQ_HEAD *head1, STAILQ_HEAD *head2);
β οΈ Note: Identical macros prefixed withSIMPLEQinstead ofSTAILQexist; see VERSIONS.
π DESCRIPTION π
These macros define and operate on singly linked tail queues. Yes, that's the whole vibe: cute little macros, surprisingly serious data structure. π
In the macro definitions, TYPE is the name of a user-defined structure, and that struct has to contain a field of type STAILQ_ENTRY, named NAME. Meanwhile HEADNAME is the name of a user-defined structure that you have to declare using the macro STAILQ_HEAD(). Two structs, one destiny, fr fr. β¨
π§ Creation π§
A singly linked tail queue is headed by a structure defined by the STAILQ_HEAD() macro. That 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.
Here's the trade-off, and it's the whole personality of this data structure: the elements are singly linked for minimum space and pointer manipulation overhead β at the expense of O(n) removal for arbitrary elements. So fast and tiny, but deleting a random middle element? Gonna have to walk. πΆββοΈ
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. Flexible queen. π
A STAILQ_HEAD structure is declared like this:
STAILQ_HEAD(HEADNAME, TYPE) head;
where struct HEADNAME is the structure to be defined, and struct 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 β pick your own, bestie. π¨)
STAILQ_ENTRY() declares a structure that connects the elements in the tail queue.
STAILQ_HEAD_INITIALIZER() evaluates to an initializer for the tail queue head.
STAILQ_INIT() initializes the tail queue referenced by head.
STAILQ_EMPTY() evaluates to true if there are no items on the tail queue.
π Insertion π
STAILQ_INSERT_HEAD()β inserts the new element elm at the head of the tail queue.STAILQ_INSERT_TAIL()β inserts the new element elm at the end of the tail queue.STAILQ_INSERT_AFTER()β inserts the new element elm after the element listelm.
π¦ Traversal π¦
STAILQ_FIRST()β returns the first item on the tail queue, orNULLif the tail queue is empty.STAILQ_NEXT()β returns the next item on the tail queue, orNULLif this item is the last.STAILQ_FOREACH()β traverses the tail queue referenced by head in the forward direction, assigning each element in turn to var.
π Removal π
STAILQ_REMOVE()β removes the element elm from the tail queue.STAILQ_REMOVE_HEAD()β removes the element at the head of the tail queue.
π‘ Pro tip from the manpage herself: 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. She said it, not me. π«Ά
β Other features β
STAILQ_CONCAT() concatenates the tail queue headed by head2 onto the end of the one headed by head1, removing all entries from the former. Two queues walk in, one queue walks out. π
π₯ RETURN VALUE π₯
STAILQ_EMPTY()returns nonzero if the queue is empty, and zero if the queue contains at least one entry.STAILQ_FIRST(), andSTAILQ_NEXT()return a pointer to the first or nextTYPEstructure, respectively.STAILQ_HEAD_INITIALIZER()returns an initializer that can be assigned to the queue head.
π VERSIONS π
Some BSDs provide SIMPLEQ instead of STAILQ. They are identical, but for historical reasons they were named differently on different BSDs.
STAILQoriginated on FreeBSD π½SIMPLEQoriginated on NetBSD π
For compatibility reasons, some systems provide both sets of macros. glibc provides both STAILQ and SIMPLEQ, which are identical except for a missing SIMPLEQ equivalent to STAILQ_CONCAT(). So heads up: no SIMPLEQ_CONCAT invite for you. π
π BUGS π
β οΈ STAILQ_FOREACH() doesn't allow var to be removed or freed within the loop, as it would interfere with the traversal. Do not do it. I am begging you. π
STAILQ_FOREACH_SAFE(), which is present on the BSDs but is not present in glibc, fixes this limitation by allowing var to safely be removed from the list and freed from within the loop without interfering with the traversal. glibc girlies, you do not have this one. Sorry bestie, check your platform. π«
π STANDARDS π
BSD.
π°οΈ HISTORY π°οΈ
4.4BSD. β an elder, an ancestral icon, we respect her. π
πΈ EXAMPLES πΈ
the full example, character for character, because it slaps:
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/queue.h>
struct entry {
int data;
STAILQ_ENTRY(entry) entries; /* Singly linked tail queue */
};
STAILQ_HEAD(stailhead, entry);
int
main(void)
{
struct entry *n1, *n2, *n3, *np;
struct stailhead head; /* Singly linked tail queue
head */
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);
STAILQ_REMOVE(&head, n2, entry, entries); /* Deletion */
free(n2);
n3 = STAILQ_FIRST(&head);
STAILQ_REMOVE_HEAD(&head, entries); /* Deletion from the head */
free(n3);
n1 = STAILQ_FIRST(&head);
n1->data = 0;
for (unsigned int i = 1; i < 5; i++) {
n1 = malloc(sizeof(struct entry));
STAILQ_INSERT_HEAD(&head, n1, entries);
n1->data = i;
}
/* Forward traversal */
STAILQ_FOREACH(np, &head, entries)
printf("%i\n", np->data);
/* TailQ deletion */
n1 = STAILQ_FIRST(&head);
while (n1 != NULL) {
n2 = STAILQ_NEXT(n1, entries);
free(n1);
n1 = n2;
}
STAILQ_INIT(&head);
exit(EXIT_SUCCESS);
}
likeβ¦ she mallocs, she inserts at head, at tail, after, she removes, she traverses forward, she tears the whole thing down manually, she re-inits, she dips with EXIT_SUCCESS. main character energy from start to finish. π
π SEE ALSO π
insque(3), queue(7) β go read the siblings when you're done here, iykyk. π
and that's the page, bestie!! π this was Linux man-pages 6.20, dated 2025-09-21, signing off from section 3 β mwah, go build a queue, you're going to slay. π β¨π¦
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: BSD-3-Clause
- Copyright 1993, The Regents of the University of California
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms