#ifndef LINKED_LIST_H #define LINKED_LIST_H #include /* elem_t: Element base for linked lists This struct is the base for storing data. @data Raw pointer to any type of data @prev Pointer to the prev element in the linked list @next Pointer to the next element in the linked list */ typedef struct elem_t { void *data; struct elem_t *prev; struct elem_t *next; } elem_t; /* deleter_t: A type for deleters used during the destruction of elements During the initialisation of linked lists you can pass in argument a deleter. It is used during the destruction of an element. You can pass NULL to the initialisation to use the default deleter (kfree(elem->data)). Your deleter_t function declaration must look like this : void name_of_your_deleter(void *data); Then do what you need to destroy your data (don't forget to kfree the data itself...). You can define your function with "inline" if the deleting is short. */ typedef void (*deleter_t)(void *); /* condition_t: A type for condition used for conditionned function like linked_list_remove_if For function like linked_list_remove_if you need a condition. It have two argument which are "elem_t *" and "va_list", that is because it passes the element on the actual index and the args passed to the function. Your condition_t function declaration must look like this : bool name_of_your_condition(elem_t *elem, va_list); Then return true if you want the element to be removed from the list or false if not. You can define your function with "static inline" if the condition is short. */ typedef bool (*condition_t)(elem_t *, va_list); /* action_t: A type for action used for function like linked_list_for_each For function like linked_list_for_each you need an action to do. It have two argument which are "elem_t *" and "va_list", that is because it passes the element on the actual index and the args passed to the function. Your condition_t function declaration must look like this : void name_of_your_condition(elem_t *elem, va_list); Then do whatever you want with the element data, just don't remove it XD. You can define your function with "static inline" if the condition is short. */ typedef void (*action_t)(elem_t *, va_list); /* linked_list_t: Base of linked lists This struct is the base for creating linked lists. @data_size Size of the data stored in the linked list @first Pointer to the first element in the linked list @last Pointer to the last element in the linked list @size Size of the linked list @deleter Deleter of the data when removing an element*/ typedef struct linked_list_t { size_t data_size; elem_t *first; elem_t *last; size_t size; deleter_t elem_deleter; } linked_list_t; /* linked_list_init(): Init a linked_list_t This function is necessary if you create a linked list. @linked_list Pointer to an linked_list_t @data_size Data size for the linked list */ void linked_list_init(linked_list_t *, const size_t, deleter_t deleter); /* elem_create(): Create an elem to fill with data and insert in a linked list. This function is usefull when you want to initialise an elem with the proper data_size. WARNING : Don't change the "data" ptr after calling this function or it will lead to memory leaks ! Instead do something like this : *(int *)elem->data = 42; @linked_list Pointer to an linked_list_t Return adress of initialised elem, NULL on error. */ elem_t *elem_create(const linked_list_t *); /* destroy_elem(): Destroy the given element and its data. This function is usefull when you want to free an element. The deleter is used for the data only. @elem Pointer to an elem_t Free the given elem. */ void destroy_elem(elem_t *, const deleter_t); /* linked_list_push_back_elem(): Insert an elem_t from the back in a linked_list_t. @linked_list Pointer to an linked_list_t @elem Pointer to an elem_t Insert elem in the back of the linked list, do nothing on error. */ void linked_list_push_back_elem(linked_list_t *, elem_t *); /* linked_list_push_front_elem(): Insert an elem_t from the front in a linked_list_t. @linked_list Pointer to an linked_list_t @elem Pointer to an elem_t Insert elem in the front of the linked list, do nothing on error. */ void linked_list_push_front_elem(linked_list_t *, elem_t *); /* linked_list_push_back_elem(): Insert an elem_t filled with data from the back in a linked_list_t. @linked_list Pointer to an linked_list_t @data Pointer to any data Insert elem filled with data in the back of the linked list, do nothing on error. */ void linked_list_push_back(linked_list_t *, void *); /* linked_list_push_front_elem(): Insert an elem_t filled with data from the front in a linked_list_t. @linked_list Pointer to an linked_list_t @data Pointer to any data Insert elem filled with data in the front of the linked list, do nothing on error. */ void linked_list_push_front(linked_list_t *, void *); /* linked_list_pop_back(): Delete the last elem in the given linked list @linked_list Pointer to an linked_list_t Delete last elem of the linked list, do nothing on error. */ void linked_list_pop_back(linked_list_t *); /* linked_list_pop_front(): Delete the first elem in the given linked list @linked_list Pointer to an linked_list_t Delete first elem of the linked list, do nothing on error. */ void linked_list_pop_front(linked_list_t *); /* linked_list_clear(): Clear the given linked list @linked_list Pointer to an linked_list_t Clear linked list. */ void linked_list_clear(linked_list_t *); /* linked_list_is_empty(): Check if the given linked list is empty @linked_list Pointer to an linked_list_t Return true if the given linked list is empty else false. */ bool linked_list_is_empty(const linked_list_t *); /* linked_list_is_in_bound(): Check if the given index is whithin the range of the linked list @linked_list Pointer to an linked_list_t Return true if the given linked list is empty else false. */ bool linked_list_is_in_bound(const linked_list_t *, const size_t); /* linked_list_get_elem(): Get element at the given index in the linked list @linked_list Pointer to an linked_list_t @index Index of the wanted element Return the element, NULL on error. */ elem_t *linked_list_get_elem(const linked_list_t *, const size_t); /* linked_list_get(): Get data in the element at the given index in the linked list @linked_list Pointer to an linked_list_t @index Index of the wanted element Return the data, NULL on error. */ void *linked_list_get(const linked_list_t *, const size_t); /* linked_list_insert_elem(): Insert element at the given index in the linked list @linked_list Pointer to an linked_list_t @elem Pointer to an element @index Index of the wanted element Insert the element, do nothing on error. */ void linked_list_insert_elem(linked_list_t *, elem_t *, const size_t); /* linked_list_insert(): Insert element with data at the given index in the linked list @linked_list Pointer to an linked_list_t @data Data to put in the new element @index Index of the wanted element Return the element, do nothing on error. */ void linked_list_insert(linked_list_t *, void *, const size_t); /* linked_list_remove_elem(): Remove the given elem in the linked list @linked_list Pointer to an linked_list_t @elem Element to remove Remove the element, do nothing on error. */ void linked_list_remove_elem(linked_list_t *, elem_t *); /* linked_list_remove(): Remove element at the given index in the linked list @linked_list Pointer to an linked_list_t @index Index of the element Remove the element, do nothing on error. */ void linked_list_remove(linked_list_t *, const size_t); /* linked_list_get_elem_if(): Return first element that the condition verify @linked_list Pointer to an linked_list_t @condition Condition to verify @... Argument to pass to the condition Return the element, NULL on error. */ elem_t *linked_list_get_elem_if(const linked_list_t *, const condition_t, ...); /* linked_list_get_if(): Return data in the first element that the condition verify @linked_list Pointer to an linked_list_t @condition Condition to verify @... Argument to pass to the condition Return the element data, NULL on error. */ void *linked_list_get_if(const linked_list_t *, const condition_t, ...); /* linked_list_remove_if(): Remove element(s) that the condition verify @linked_list Pointer to an linked_list_t @condition Condition to verify @... Argument to pass to the condition Remove the element(s), do nothing on error. */ void linked_list_remove_if(linked_list_t *, const condition_t, ...); /* linked_list_for_each(): Apply the condition to every elements in the list Useful for changing a value in every single element data. The condition can be anything that dont destroy the element. @linked_list Pointer to an linked_list_t @condition Condition to apply @... Argument to pass to the condition */ void linked_list_for_each(const linked_list_t *, const action_t, ...); #endif // LINKED_LIST_H