changeset 756:4bd82aca5e98

Fix list length when reversing.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 01 Feb 2023 14:09:12 +0200
parents 3f84521fdd31
children 2ab2fece83ea
files th_datastruct.c
diffstat 1 files changed, 20 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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;
 }