|
solidc
Robust collection of general-purpose cross-platform C libraries and data structures designed for rapid and safe development in C
|
Doubly linked list implementation with memory management. More...
#include <stddef.h>#include <stdio.h>Go to the source code of this file.
Classes | |
| struct | list_node |
| struct | list_t |
Macros | |
| #define | LIST_FOR_EACH(list, node) for (list_node_t * (node) = (list)->head; (node); (node) = (node)->next) |
| #define | LIST_FOR_EACH_REVERSE(list, node) for (list_node_t * (node) = (list)->tail; (node); (node) = (node)->prev) |
Typedefs | |
| typedef struct list_node | list_node_t |
Functions | |
| list_t * | list_new (size_t elem_size) |
| void | list_free (list_t *list) |
| void | list_clear (list_t *list) |
| size_t | list_size (const list_t *list) |
| void | list_push_back (list_t *list, void *elem) |
| void | list_pop_back (list_t *list) |
| void | list_push_front (list_t *list, void *elem) |
| void | list_pop_front (list_t *list) |
| void * | list_get (const list_t *list, size_t index) |
| int | list_index_of (const list_t *list, void *elem) |
| void | list_insert (list_t *list, size_t index, void *elem) |
| void | list_remove (list_t *list, void *elem) |
| void | list_insert_after (list_t *list, void *elem, void *after) |
| void | list_insert_before (list_t *list, void *elem, void *before) |
Doubly linked list implementation with memory management.
Definition in file list.h.
| #define LIST_FOR_EACH | ( | list, | |
| node | |||
| ) | for (list_node_t * (node) = (list)->head; (node); (node) = (node)->next) |
Forward iteration macro for traversing all nodes in a list. Iterates from head to tail. Usage: LIST_FOR_EACH(my_list, node) { ... use node->data ... }
| list | Pointer to the list to iterate over. |
| node | Name of the list_node_t* variable to use in the loop body. |
| #define LIST_FOR_EACH_REVERSE | ( | list, | |
| node | |||
| ) | for (list_node_t * (node) = (list)->tail; (node); (node) = (node)->prev) |
Reverse iteration macro for traversing all nodes in a list. Iterates from tail to head. Usage: LIST_FOR_EACH_REVERSE(my_list, node) { ... use node->data ... }
| list | Pointer to the list to iterate over. |
| node | Name of the list_node_t* variable to use in the loop body. |
| typedef struct list_node list_node_t |
A node in a doubly-linked list. Each node contains a pointer to element data and bidirectional links.
| void list_clear | ( | list_t * | list | ) |
Removes all elements from the list. Deallocates all nodes and their data, leaving the list empty.
| list | Pointer to the list to clear. Must not be NULL. |
Definition at line 47 of file list.c.
References list_t::head, list_clear(), list_node::next, list_t::size, and list_t::tail.
Referenced by list_clear(), and list_free().
| void list_free | ( | list_t * | list | ) |
Frees all resources associated with a doubly-linked list. Deallocates all nodes and their data, then the list structure itself.
| list | Pointer to the list to free. Safe to pass NULL. |
Definition at line 41 of file list.c.
References list_clear(), and list_free().
Referenced by list_free().
| void * list_get | ( | const list_t * | list, |
| size_t | index | ||
| ) |
Retrieves a pointer to the element at the specified index. Traverses the list to find the element at the given position.
| list | Pointer to the list. Must not be NULL. |
| index | Zero-based position of the element to retrieve. Must be < list size. |
Definition at line 115 of file list.c.
References list_node::data, list_t::head, list_get(), list_node::next, and list_t::size.
Referenced by list_get().
| int list_index_of | ( | const list_t * | list, |
| void * | elem | ||
| ) |
Finds the index of the first occurrence of an element in the list. Compares element addresses, not content (pointer equality).
| list | Pointer to the list. Must not be NULL. |
| elem | Pointer to the element data to find. Must not be NULL. |
Definition at line 122 of file list.c.
References list_node::data, list_t::elem_size, list_t::head, list_index_of(), and list_node::next.
Referenced by list_index_of(), list_insert_after(), list_insert_before(), and list_remove().
| void list_insert | ( | list_t * | list, |
| size_t | index, | ||
| void * | elem | ||
| ) |
Inserts an element at the specified index in the list. Creates a new node with a copy of the element data at position index. Existing elements at index and beyond are shifted right.
| list | Pointer to the list. Must not be NULL. |
| index | Zero-based position at which to insert. Must be <= list size. |
| elem | Pointer to the element data to insert. Must not be NULL. |
Definition at line 134 of file list.c.
References list_t::elem_size, list_t::head, list_insert(), list_push_back(), list_push_front(), list_node::next, list_node::prev, and list_t::size.
Referenced by list_insert(), list_insert_after(), and list_insert_before().
| void list_insert_after | ( | list_t * | list, |
| void * | elem, | ||
| void * | after | ||
| ) |
Inserts an element immediately after a specified element in the list. Creates a new node with a copy of elem and inserts it after the node containing after.
| list | Pointer to the list. Must not be NULL. |
| elem | Pointer to the element data to insert. Must not be NULL. |
| after | Pointer to the element data after which to insert. Must not be NULL. |
Definition at line 181 of file list.c.
References list_index_of(), list_insert(), and list_insert_after().
Referenced by list_insert_after().
| void list_insert_before | ( | list_t * | list, |
| void * | elem, | ||
| void * | before | ||
| ) |
Inserts an element immediately before a specified element in the list. Creates a new node with a copy of elem and inserts it before the node containing before.
| list | Pointer to the list. Must not be NULL. |
| elem | Pointer to the element data to insert. Must not be NULL. |
| before | Pointer to the element data before which to insert. Must not be NULL. |
Definition at line 187 of file list.c.
References list_index_of(), list_insert(), and list_insert_before().
Referenced by list_insert_before().
| list_t * list_new | ( | size_t | elem_size | ) |
Creates a new empty doubly-linked list.
| elem_size | Size in bytes of each element to be stored in the list. |
Definition at line 32 of file list.c.
References list_t::elem_size, list_t::head, list_new(), list_t::size, and list_t::tail.
Referenced by list_new().
| void list_pop_back | ( | list_t * | list | ) |
Removes the last element from the list. Deallocates the tail node and its data. Updates tail to the previous node.
| list | Pointer to the list. Must not be NULL. |
Definition at line 76 of file list.c.
References list_t::head, list_pop_back(), list_node::next, list_node::prev, list_t::size, and list_t::tail.
Referenced by list_pop_back().
| void list_pop_front | ( | list_t * | list | ) |
Removes the first element from the list. Deallocates the head node and its data. Updates head to the next node. Operation is O(1).
| list | Pointer to the list. Must not be NULL. |
Definition at line 103 of file list.c.
References list_t::head, list_pop_front(), list_node::next, list_node::prev, list_t::size, and list_t::tail.
Referenced by list_pop_front().
| void list_push_back | ( | list_t * | list, |
| void * | elem | ||
| ) |
Appends an element to the back of the list. Creates a new node with a copy of the element data and makes it the new tail. Operation is O(1) due to tail pointer maintenance.
| list | Pointer to the list. Must not be NULL. |
| elem | Pointer to the element data to append. Must not be NULL. |
Definition at line 61 of file list.c.
References list_t::elem_size, list_t::head, list_push_back(), list_node::next, list_node::prev, list_t::size, and list_t::tail.
Referenced by list_insert(), and list_push_back().
| void list_push_front | ( | list_t * | list, |
| void * | elem | ||
| ) |
Inserts an element at the front of the list. Creates a new node with a copy of the element data and makes it the new head. Operation is O(1).
| list | Pointer to the list. Must not be NULL. |
| elem | Pointer to the element data to insert. Must not be NULL. |
Definition at line 88 of file list.c.
References list_t::elem_size, list_t::head, list_push_front(), list_node::next, list_node::prev, list_t::size, and list_t::tail.
Referenced by list_insert(), and list_push_front().
| void list_remove | ( | list_t * | list, |
| void * | elem | ||
| ) |
Removes the first occurrence of an element from the list. Searches for the element by pointer equality and removes it. Deallocates the matching node and its data.
| list | Pointer to the list. Must not be NULL. |
| elem | Pointer to the element data to remove. Must not be NULL. |
Definition at line 159 of file list.c.
References list_t::head, list_index_of(), list_remove(), list_node::next, list_node::prev, list_t::size, and list_t::tail.
Referenced by list_remove().
| size_t list_size | ( | const list_t * | list | ) |
Returns the number of elements in the list.
| list | Pointer to the list. Must not be NULL. |
Definition at line 59 of file list.c.
References list_size(), and list_t::size.
Referenced by list_size().