LIST(3) Library Functions Manual LIST(3)

LIST, LIST_ENTRY, LIST_INIT, LIST_FIRST, LIST_LAST, LIST_PREV, LIST_NEXT, LIST_EMPTY, LIST_LINKED, LIST_INSERT_HEAD, LIST_INSERT_TAIL, LIST_INSERT_AFTER, LIST_INSERT_BEFORE, LIST_REMOVE, LIST_CONCAT, LIST_FOREACH, LIST_FOREACH_SAFE, LIST_FOREACH_REVERSE, LIST_FOREACH_REVERSE_SAFEdoubly linked list structure

#include <libks/list.h>

LIST(list_type, element_type);

LIST_ENTRY(list_type, element_type);

struct element_type *
LIST_FIRST(struct list_type *list);

struct element_type *
LIST_LAST(struct list_type *list);

struct element_type *
LIST_PREV(struct element_type *element);

struct element_type *
LIST_NEXT(struct element_type *element);

int
LIST_EMPTY(struct list_type *list);

int
LIST_LINKED(struct element_type *element);

void
LIST_INSERT_HEAD(struct list_type *list, struct element_type *element);

void
LIST_INSERT_TAIL(struct list_type *list, struct element_type *element);

void
LIST_INSERT_AFTER(struct list_type *list, struct element_type *after, struct element_type *element);

void
LIST_INSERT_BEFORE(struct element_type *before, struct element_type *element);

void
LIST_REMOVE(struct list_type *list, struct element_type *element);

void
LIST_CONCAT(struct list_type *list1, struct element_type *list2);

LIST_FOREACH(struct element_type *element, struct list_type *list);

LIST_FOREACH_SAFE(struct element_type *element, struct list_type *list, struct element_type *tmp);

LIST_FOREACH_REVERSE(struct element_type *element, struct list_type *list);

LIST_FOREACH_REVERSE_SAFE(struct element_type *element, struct list_type *list, struct element_type *tmp);

The list structure stores elements in a doubly linked list, allowing constant time insertion and deletion.

The () macro declares a new list structure named list_type capable of holding elements of the given element_type.

The () macro declares a structure field making the enclosing structure of type element_type capable of being part of list_type.

The () macro returns the first element in list.

The () macro returns the last element in list.

The () macro returns the previous element relative to element.

The () macro returns the next element relative to element.

The () returns non-zero if list is empty.

The () returns non-zero if element is part of a list.

The () macro inserts element at the start of list.

The () macro inserts element at the end of list.

The () macro inserts element after after.

The () macro inserts element before before.

The () macro removes element from list.

The () macro moves all elements from list2 to list1 effectively making list2 empty.

The () iterates over all elements in list starting from the first element.

The () behaves like LIST_FOREACH() but allows the element currently being iterated over to be removed from list.

The () iterates over all elements in list starting from the last element.

The () behaves like LIST_FOREACH_REVERSE() but allows the element currently being iterated over to be removed from list.

The following example demonstrates how to create a token_list structure capable of storing struct token elements.

LIST(token_list, token);

struct token {
	LIST_ENTRY(token_list, token);
};

int
main(void)
{
	struct token_list list;
	struct token tail = {0};
	struct token *token;

	LIST_INIT(&list);
	LIST_INSERT_TAIL(&list, &tail);

	LIST_FOREACH(token, &list)
		continue;

	return 0;
}

Anton Lindqvist <anton@basename.se>

OpenBSD 7.8 June 28, 2024 LIST(3)