completed dlist

Sat, 31 Dec 2011 18:46:48 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 31 Dec 2011 18:46:48 +0100
changeset 8
9cd2b2460db0
parent 7
68091406d1cf
child 9
013c5c4b7e44

completed dlist

ucx/dlist.c file | annotate | diff | comparison | revisions
ucx/dlist.h file | annotate | diff | comparison | revisions
--- a/ucx/dlist.c	Sat Dec 31 18:18:03 2011 +0100
+++ b/ucx/dlist.c	Sat Dec 31 18:46:48 2011 +0100
@@ -1,15 +1,50 @@
 #include "dlist.h"
 
+void ucx_dlist_free(UcxDlist *l) {
+    UcxDlist *e = l, *f;
+    while (e != NULL) {
+        f = e;
+        e = e->next;
+        free(f);
+    }
+}
+
 UcxDlist *ucx_dlist_append(UcxDlist *l, void *data)  {
+    UcxDlist *nl = (UcxDlist*) malloc(sizeof(UcxDlist));
+    if (nl == NULL) return NULL;
     
+    nl->data = data;
+    nl->next = NULL;
+    if (l == NULL) {
+        return nl;
+    } else {
+        UcxDlist *t = ucx_dlist_last(l);
+        t->next = nl;
+        nl->prev = t;
+        return l;
+    }
 }
 
 UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data) {
+    UcxDlist *nl = ucx_dlist_append(NULL, data);
+    if (nl == NULL) return NULL;
     
+    if (l != NULL) {
+        nl->next = l;
+        l->prev = nl;
+    }
+    return nl;
 }
 
 UcxDlist *ucx_dlist_concat(UcxDlist *l1, UcxDlist *l2) {
-    
+    if (l1 == NULL) {
+        return l2;
+    } else {
+        UcxDlist *last = ucx_dlist_last(l1);
+        last->next = l2;
+        l2->prev = last;
+        return l1;
+    }
 }
 
 UcxDlist *ucx_dlist_last(UcxDlist *l) {
@@ -23,7 +58,15 @@
 }
 
 UcxDlist *ucx_dlist_get(UcxDlist *l, int index) {
+    if (l == NULL) return NULL;
+
+    UcxDlist *e = l;
+    while (e->next != NULL && index > 0) {
+        e = e->next;
+        index--;
+    }
     
+    return index == 0 ? e : NULL;
 }
 
 size_t ucx_dlist_size(UcxDlist *l) {
@@ -40,10 +83,20 @@
 }
 
 void ucx_dlist_foreach(UcxDlist *l, ucx_callback fnc, void* data) {
-    
+    UcxDlist *e = l;
+    while (e != NULL) {
+        fnc(e, data);
+        e = e->next;
+    }
 }
 
 /* dlist specific functions */
 UcxDlist *ucx_dlist_first(UcxDlist *l) {
+    if (l == NULL) return NULL;
     
+    UcxDlist *e = l;
+    while (e->prev != NULL) {
+        e = e->prev;
+    }
+    return e;
 }
\ No newline at end of file
--- a/ucx/dlist.h	Sat Dec 31 18:18:03 2011 +0100
+++ b/ucx/dlist.h	Sat Dec 31 18:46:48 2011 +0100
@@ -19,6 +19,7 @@
     UcxDlist *prev;
 };
 
+void ucx_dlist_free(UcxDlist *l);
 UcxDlist *ucx_dlist_append(UcxDlist *l, void *data);
 UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data);
 UcxDlist *ucx_dlist_concat(UcxDlist *l1, UcxDlist *l2);

mercurial