implement linked list find

Mon, 27 Sep 2021 17:49:23 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 27 Sep 2021 17:49:23 +0200
changeset 437
9d4971ea0625
parent 436
ca9ce2297c29
child 438
cd3069757010

implement linked list find

src/linked_list.c file | annotate | diff | comparison | revisions
--- a/src/linked_list.c	Mon Sep 27 17:00:19 2021 +0200
+++ b/src/linked_list.c	Mon Sep 27 17:49:23 2021 +0200
@@ -98,7 +98,7 @@
 } cx_linked_list;
 
 int cx_ll_add(cx_list_s *list, void *elem) {
-    cx_linked_list *linkedlist = (cx_linked_list *) list;
+    cx_linked_list *ll = (cx_linked_list *) list;
 
     struct cx_linked_list_node *node = cxMalloc(list->allocator,
                                                 sizeof(struct cx_linked_list_node) + list->itemsize);
@@ -109,8 +109,7 @@
     memcpy(node->payload, elem, list->itemsize);
 
     int ret = cx_linked_list_add(
-            &linkedlist->begin,
-            &linkedlist->end,
+            &ll->begin, &ll->end,
             offsetof(struct cx_linked_list_node, prev),
             offsetof(struct cx_linked_list_node, next),
             node
@@ -124,21 +123,31 @@
 }
 
 int cx_ll_insert(cx_list_s *list, size_t index, void *elem) {
-    cx_linked_list *linkedList = (cx_linked_list *) list;
+    cx_linked_list *ll = (cx_linked_list *) list;
     // TODO: implement using low level API
     return 1;
 }
 
 void *cx_ll_remove(cx_list_s *list, size_t index) {
-    cx_linked_list *linkedList = (cx_linked_list *) list;
+    cx_linked_list *ll = (cx_linked_list *) list;
     // TODO: implement using low level API
     return NULL;
 }
 
 size_t cx_ll_find(cx_list_s *list, void *elem) {
-    cx_linked_list *linkedList = (cx_linked_list *) list;
-    // TODO: implement using low level API
-    return 0;
+    CxListComparator cmp = list->cmpfunc;
+    cx_linked_list *ll = (cx_linked_list *) list;
+
+    size_t index;
+    struct cx_linked_list_node* node = ll->begin;
+    for (index = 0 ; index < list->size ; index++) {
+        void* current = node->payload;
+        if (cmp(current, elem) == 0) {
+            return index;
+        }
+        node = node->next;
+    }
+    return index;
 }
 
 void *cx_ll_last(cx_list_s *list) {

mercurial