# HG changeset patch # User Matti Hamalainen # Date 1675253352 -7200 # Node ID 4bd82aca5e989d60f7050363fd9dea269ae40b3f # Parent 3f84521fdd31fcb9abfbb67398bcd91bbf2b5a8a Fix list length when reversing. diff -r 3f84521fdd31 -r 4bd82aca5e98 th_datastruct.c --- a/th_datastruct.c Wed Feb 01 13:17:26 2023 +0200 +++ b/th_datastruct.c Wed Feb 01 14:09:12 2023 +0200 @@ -227,26 +227,42 @@ th_llist_t *f_curr = *list, *f_prev = NULL, *b_curr = *list, *b_prev = NULL; + size_t count = 0; + // NULL list? + if (f_curr == NULL) + return; + + // Set initial previous backward pointer if (b_curr != NULL) b_prev = b_curr->prev; + // Reverse the list while (f_curr != NULL) { th_llist_t - *f_next = f_curr->next, + *f_next = f_curr->next, // next = current->next *b_next = b_curr->prev; - f_curr->next = f_prev; - f_prev = f_curr; - f_curr = f_next; + count++; + f_curr->num = 0; + + f_curr->next = f_prev; // current->next = previous + f_prev = f_curr; // previous = current + f_curr = f_next; // current = next b_curr->prev = b_prev; b_prev = b_curr; b_curr = b_next; } + // Update count + f_prev->num = count; + + // Set list pointer *list = f_prev; + + // Fix last previous pointer if (b_curr != NULL) b_curr->prev = b_prev; }