added list implementation

Sat, 31 Dec 2011 19:05:26 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 31 Dec 2011 19:05:26 +0100
changeset 10
df8140ba781c
parent 9
013c5c4b7e44
child 11
4f6082f99bd7

added list implementation

ucx/list.c file | annotate | diff | comparison | revisions
--- a/ucx/list.c	Sat Dec 31 18:57:30 2011 +0100
+++ b/ucx/list.c	Sat Dec 31 19:05:26 2011 +0100
@@ -1,1 +1,88 @@
+#include "list.h"
 
+void ucx_list_free(UcxList *l) {
+    UcxList *e = l, *f;
+    while (e != NULL) {
+        f = e;
+        e = e->next;
+        free(f);
+    }
+}
+
+UcxList *ucx_list_append(UcxList *l, void *data)  {
+    UcxList *nl = (UcxList*) malloc(sizeof(UcxList));
+    if (nl == NULL) return NULL;
+    
+    nl->data = data;
+    nl->next = NULL;
+    if (l == NULL) {
+        return nl;
+    } else {
+        UcxList *t = ucx_list_last(l);
+        t->next = nl;
+        return l;
+    }
+}
+
+UcxList *ucx_list_prepend(UcxList *l, void *data) {
+    UcxList *nl = ucx_list_append(NULL, data);
+    if (nl == NULL) return NULL;
+    
+    if (l != NULL) {
+        nl->next = l;
+    }
+    return nl;
+}
+
+UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) {
+    if (l1 == NULL) {
+        return l2;
+    } else {
+        UcxList *last = ucx_list_last(l1);
+        last->next = l2;
+        return l1;
+    }
+}
+
+UcxList *ucx_list_last(UcxList *l) {
+    if (l == NULL) return NULL;
+    
+    UcxList *e = l;
+    while (e->next != NULL) {
+        e = e->next;
+    }
+    return e;
+}
+
+UcxList *ucx_list_get(UcxList *l, int index) {
+    if (l == NULL) return NULL;
+
+    UcxList *e = l;
+    while (e->next != NULL && index > 0) {
+        e = e->next;
+        index--;
+    }
+    
+    return index == 0 ? e : NULL;
+}
+
+size_t ucx_list_size(UcxList *l) {
+    if (l == NULL) return 0;
+    
+    UcxList *e = l;
+    size_t s = 1;
+    while (e->next != NULL) {
+        e = e->next;
+        s++;
+    }
+
+    return s;
+}
+
+void ucx_list_foreach(UcxList *l, ucx_callback fnc, void* data) {
+    UcxList *e = l;
+    while (e != NULL) {
+        fnc(e, data);
+        e = e->next;
+    }
+}

mercurial