diff --git a/src/headers/linked_list.h b/src/headers/linked_list.h index e571286..454ad1a 100644 --- a/src/headers/linked_list.h +++ b/src/headers/linked_list.h @@ -274,4 +274,16 @@ int linked_list_remove_if(linked_list_t *linked_list, const condition_t conditio */ int linked_list_for_each(const linked_list_t *linked_list, const action_t action, ...); +/** + * @brief Apply an action to every elements in the list. + * The action can be anything that doesn't destroy the element. + * + * @param linked_list Pointer to an linked list + * @param action Action to apply + * @param args Argument to pass to the action in the form of a va_list + * + * @return EXIT_SUCCESS, EXIT_FAILURE on error. +*/ +int linked_list_for_eachv(const linked_list_t *linked_list, const action_t action, va_list args); + #endif // LINKED_LIST_H diff --git a/src/linked_list.c b/src/linked_list.c index 83986b7..051be62 100644 --- a/src/linked_list.c +++ b/src/linked_list.c @@ -478,3 +478,31 @@ int linked_list_for_each(const linked_list_t *linked_list, const action_t action return EXIT_SUCCESS; } + +int linked_list_for_eachv(const linked_list_t *linked_list, const action_t action, va_list args) +{ + assert("Linked list cannot be NULL" && linked_list); + assert("Action cannot be NULL" && action); + + elem_t *current_elem = linked_list->first; + + while(current_elem) + { + va_list args_copy; + va_copy(args_copy, args); + + if(action(current_elem, args_copy)) + { + va_end(args_copy); + va_end(args); + + return EXIT_FAILURE; + } + + va_end(args_copy); + + current_elem = current_elem->next; + } + + return EXIT_SUCCESS; +}