# HG changeset patch # User Matti Hamalainen # Date 1675249355 -7200 # Node ID d91d1174cfd813763b00a5223eb9472dde0930f8 # Parent 64cb2b1777a968d2935eaf16ce6938b0171098a3 Add th_llist_reverse() for reversing a linked list. diff -r 64cb2b1777a9 -r d91d1174cfd8 th_datastruct.c --- a/th_datastruct.c Thu Jan 26 07:23:12 2023 +0200 +++ b/th_datastruct.c Wed Feb 01 13:02:35 2023 +0200 @@ -222,6 +222,24 @@ } +void th_llist_reverse(th_llist_t **list) +{ + th_llist_t *curr = *list, *prev = NULL; + + while (curr != NULL) + { + th_llist_t *next = curr->next; + curr->next = prev; + prev = curr; + curr = next; + if (curr != NULL) + curr->prev = prev; + } + + *list = prev; +} + + void th_llist_foreach(th_llist_t *list, void (*func)(th_llist_t *node, void *userdata), void *data) { th_llist_t *curr = list; diff -r 64cb2b1777a9 -r d91d1174cfd8 th_datastruct.h --- a/th_datastruct.h Thu Jan 26 07:23:12 2023 +0200 +++ b/th_datastruct.h Wed Feb 01 13:02:35 2023 +0200 @@ -42,6 +42,7 @@ th_llist_t * th_llist_get_nth(th_llist_t *list, const size_t n); size_t th_llist_length(const th_llist_t *list); ssize_t th_llist_position(const th_llist_t *list, const th_llist_t *node); +void th_llist_reverse(th_llist_t **list); void th_llist_foreach(th_llist_t *list, void (*func)(th_llist_t *node, void *userdata), void *data); int th_llist_foreach_cond(th_llist_t *list, int (*func)(th_llist_t *node, void *userdata), void *data, th_llist_t **res);