diff -r 18f964adad1b -r 1bd4b8c28722 src/cx/linked_list.h --- a/src/cx/linked_list.h Fri Oct 08 18:58:49 2021 +0200 +++ b/src/cx/linked_list.h Fri Oct 08 19:47:31 2021 +0200 @@ -110,6 +110,18 @@ void *cx_linked_list_last(void *begin, ptrdiff_t loc_next); /** + * Finds the predecessor of a node in case it is not linked. + * + * \remark If \p node is not contained in the list starting with \p begin, the behavior is undefined. + * + * @param begin the node where to start the search + * @param loc_next the location of the \c next pointer + * @param node the successor of the node to find + * @return the node or \c NULL if \p node has no predecessor + */ +void *cx_linked_list_prev(void *begin, ptrdiff_t loc_next, void *node); + +/** * Adds a new node to a linked list. * * \remark One of the pointers \p begin and \p end may be \c NULL, but not both. @@ -117,12 +129,38 @@ * @param begin a pointer to the begin node pointer (if your list has one) * @param end a pointer to the end node pointer (if your list has one) * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one) - * @param loc_next the location of a \c next pointer within your node struct (negative if your struct does not have one) + * @param loc_next the location of a \c next pointer within your node struct (required) * @param new_node a pointer to the node that shall be appended */ void cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node); /** + * Removes a node from the linked list. + * + * If the node to remove is the begin (resp. end) node of the list and if \p begin (resp. \p end) + * addresses are provided, the pointers are adjusted accordingly. + * + * The following combinations of arguments are valid (more arguments are optional): + * \li \p loc_next and \p loc_prev + * \li \p loc_next and \p begin + * + * This function returns an adjacent node according to the following rules: + * \li if the node has only one adjacent node, that one is returned + * \li otherwise, the former \c prev node is returned + * + * \remark The \c next and \c prev pointers of the removed node are cleared by this function. + * + * @param begin a pointer to the begin node pointer (optional) + * @param end a pointer to the end node pointer (optional) + * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one) + * @param loc_next the location of a \c next pointer within your node struct (required) + * @param node the node to remove + * @return an adjacent node or \c NULL, if this was the last node + */ +void *cx_linked_list_remove(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *node); + + +/** * Determines the size of a linked list starting with \p node. * @param node the first node * @param loc_next the location of the \c next pointer within the node struct @@ -162,6 +200,17 @@ void cx_linked_list_sort(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, ptrdiff_t loc_data, int follow_ptr, CxListComparator cmp_func); + +/** + * Reverses the order of the nodes in a linked list. + * + * @param begin a pointer to the begin node pointer (required) + * @param end a pointer to the end node pointer (optional) + * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one) + * @param loc_next the location of a \c next pointer within your node struct (required) + */ +void cx_linked_list_reverse(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next); + #ifdef __cplusplus } /* extern "C" */ #endif