diff th_datastruct.c @ 754:d91d1174cfd8

Add th_llist_reverse() for reversing a linked list.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 01 Feb 2023 13:02:35 +0200
parents 64cb2b1777a9
children 3f84521fdd31
line wrap: on
line diff
--- 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;