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
     1.1 --- a/ucx/list.c	Sat Dec 31 18:57:30 2011 +0100
     1.2 +++ b/ucx/list.c	Sat Dec 31 19:05:26 2011 +0100
     1.3 @@ -1,1 +1,88 @@
     1.4 +#include "list.h"
     1.5  
     1.6 +void ucx_list_free(UcxList *l) {
     1.7 +    UcxList *e = l, *f;
     1.8 +    while (e != NULL) {
     1.9 +        f = e;
    1.10 +        e = e->next;
    1.11 +        free(f);
    1.12 +    }
    1.13 +}
    1.14 +
    1.15 +UcxList *ucx_list_append(UcxList *l, void *data)  {
    1.16 +    UcxList *nl = (UcxList*) malloc(sizeof(UcxList));
    1.17 +    if (nl == NULL) return NULL;
    1.18 +    
    1.19 +    nl->data = data;
    1.20 +    nl->next = NULL;
    1.21 +    if (l == NULL) {
    1.22 +        return nl;
    1.23 +    } else {
    1.24 +        UcxList *t = ucx_list_last(l);
    1.25 +        t->next = nl;
    1.26 +        return l;
    1.27 +    }
    1.28 +}
    1.29 +
    1.30 +UcxList *ucx_list_prepend(UcxList *l, void *data) {
    1.31 +    UcxList *nl = ucx_list_append(NULL, data);
    1.32 +    if (nl == NULL) return NULL;
    1.33 +    
    1.34 +    if (l != NULL) {
    1.35 +        nl->next = l;
    1.36 +    }
    1.37 +    return nl;
    1.38 +}
    1.39 +
    1.40 +UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) {
    1.41 +    if (l1 == NULL) {
    1.42 +        return l2;
    1.43 +    } else {
    1.44 +        UcxList *last = ucx_list_last(l1);
    1.45 +        last->next = l2;
    1.46 +        return l1;
    1.47 +    }
    1.48 +}
    1.49 +
    1.50 +UcxList *ucx_list_last(UcxList *l) {
    1.51 +    if (l == NULL) return NULL;
    1.52 +    
    1.53 +    UcxList *e = l;
    1.54 +    while (e->next != NULL) {
    1.55 +        e = e->next;
    1.56 +    }
    1.57 +    return e;
    1.58 +}
    1.59 +
    1.60 +UcxList *ucx_list_get(UcxList *l, int index) {
    1.61 +    if (l == NULL) return NULL;
    1.62 +
    1.63 +    UcxList *e = l;
    1.64 +    while (e->next != NULL && index > 0) {
    1.65 +        e = e->next;
    1.66 +        index--;
    1.67 +    }
    1.68 +    
    1.69 +    return index == 0 ? e : NULL;
    1.70 +}
    1.71 +
    1.72 +size_t ucx_list_size(UcxList *l) {
    1.73 +    if (l == NULL) return 0;
    1.74 +    
    1.75 +    UcxList *e = l;
    1.76 +    size_t s = 1;
    1.77 +    while (e->next != NULL) {
    1.78 +        e = e->next;
    1.79 +        s++;
    1.80 +    }
    1.81 +
    1.82 +    return s;
    1.83 +}
    1.84 +
    1.85 +void ucx_list_foreach(UcxList *l, ucx_callback fnc, void* data) {
    1.86 +    UcxList *e = l;
    1.87 +    while (e != NULL) {
    1.88 +        fnc(e, data);
    1.89 +        e = e->next;
    1.90 +    }
    1.91 +}

mercurial