changeset 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
files th_datastruct.c th_datastruct.h
diffstat 2 files changed, 19 insertions(+), 0 deletions(-) [+]
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;
--- 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);