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
     1.1 --- a/ucx/dlist.c	Sat Dec 31 18:18:03 2011 +0100
     1.2 +++ b/ucx/dlist.c	Sat Dec 31 18:46:48 2011 +0100
     1.3 @@ -1,15 +1,50 @@
     1.4  #include "dlist.h"
     1.5  
     1.6 +void ucx_dlist_free(UcxDlist *l) {
     1.7 +    UcxDlist *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  UcxDlist *ucx_dlist_append(UcxDlist *l, void *data)  {
    1.16 +    UcxDlist *nl = (UcxDlist*) malloc(sizeof(UcxDlist));
    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 +        UcxDlist *t = ucx_dlist_last(l);
    1.25 +        t->next = nl;
    1.26 +        nl->prev = t;
    1.27 +        return l;
    1.28 +    }
    1.29  }
    1.30  
    1.31  UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data) {
    1.32 +    UcxDlist *nl = ucx_dlist_append(NULL, data);
    1.33 +    if (nl == NULL) return NULL;
    1.34      
    1.35 +    if (l != NULL) {
    1.36 +        nl->next = l;
    1.37 +        l->prev = nl;
    1.38 +    }
    1.39 +    return nl;
    1.40  }
    1.41  
    1.42  UcxDlist *ucx_dlist_concat(UcxDlist *l1, UcxDlist *l2) {
    1.43 -    
    1.44 +    if (l1 == NULL) {
    1.45 +        return l2;
    1.46 +    } else {
    1.47 +        UcxDlist *last = ucx_dlist_last(l1);
    1.48 +        last->next = l2;
    1.49 +        l2->prev = last;
    1.50 +        return l1;
    1.51 +    }
    1.52  }
    1.53  
    1.54  UcxDlist *ucx_dlist_last(UcxDlist *l) {
    1.55 @@ -23,7 +58,15 @@
    1.56  }
    1.57  
    1.58  UcxDlist *ucx_dlist_get(UcxDlist *l, int index) {
    1.59 +    if (l == NULL) return NULL;
    1.60 +
    1.61 +    UcxDlist *e = l;
    1.62 +    while (e->next != NULL && index > 0) {
    1.63 +        e = e->next;
    1.64 +        index--;
    1.65 +    }
    1.66      
    1.67 +    return index == 0 ? e : NULL;
    1.68  }
    1.69  
    1.70  size_t ucx_dlist_size(UcxDlist *l) {
    1.71 @@ -40,10 +83,20 @@
    1.72  }
    1.73  
    1.74  void ucx_dlist_foreach(UcxDlist *l, ucx_callback fnc, void* data) {
    1.75 -    
    1.76 +    UcxDlist *e = l;
    1.77 +    while (e != NULL) {
    1.78 +        fnc(e, data);
    1.79 +        e = e->next;
    1.80 +    }
    1.81  }
    1.82  
    1.83  /* dlist specific functions */
    1.84  UcxDlist *ucx_dlist_first(UcxDlist *l) {
    1.85 +    if (l == NULL) return NULL;
    1.86      
    1.87 +    UcxDlist *e = l;
    1.88 +    while (e->prev != NULL) {
    1.89 +        e = e->prev;
    1.90 +    }
    1.91 +    return e;
    1.92  }
    1.93 \ No newline at end of file
     2.1 --- a/ucx/dlist.h	Sat Dec 31 18:18:03 2011 +0100
     2.2 +++ b/ucx/dlist.h	Sat Dec 31 18:46:48 2011 +0100
     2.3 @@ -19,6 +19,7 @@
     2.4      UcxDlist *prev;
     2.5  };
     2.6  
     2.7 +void ucx_dlist_free(UcxDlist *l);
     2.8  UcxDlist *ucx_dlist_append(UcxDlist *l, void *data);
     2.9  UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data);
    2.10  UcxDlist *ucx_dlist_concat(UcxDlist *l1, UcxDlist *l2);

mercurial