Sat, 31 Dec 2011 22:21:45 +0100
Fixed realloc
1 #include "list.h"
3 void ucx_list_free(UcxList *l) {
4 UcxList *e = l, *f;
5 while (e != NULL) {
6 f = e;
7 e = e->next;
8 free(f);
9 }
10 }
12 UcxList *ucx_list_append(UcxList *l, void *data) {
13 UcxList *nl = (UcxList*) malloc(sizeof(UcxList));
14 if (nl == NULL) return NULL;
16 nl->data = data;
17 nl->next = NULL;
18 if (l == NULL) {
19 return nl;
20 } else {
21 UcxList *t = ucx_list_last(l);
22 t->next = nl;
23 return l;
24 }
25 }
27 UcxList *ucx_list_prepend(UcxList *l, void *data) {
28 UcxList *nl = ucx_list_append(NULL, data);
29 if (nl == NULL) return NULL;
31 if (l != NULL) {
32 nl->next = l;
33 }
34 return nl;
35 }
37 UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) {
38 if (l1 == NULL) {
39 return l2;
40 } else {
41 UcxList *last = ucx_list_last(l1);
42 last->next = l2;
43 return l1;
44 }
45 }
47 UcxList *ucx_list_last(UcxList *l) {
48 if (l == NULL) return NULL;
50 UcxList *e = l;
51 while (e->next != NULL) {
52 e = e->next;
53 }
54 return e;
55 }
57 UcxList *ucx_list_get(UcxList *l, int index) {
58 if (l == NULL) return NULL;
60 UcxList *e = l;
61 while (e->next != NULL && index > 0) {
62 e = e->next;
63 index--;
64 }
66 return index == 0 ? e : NULL;
67 }
69 size_t ucx_list_size(UcxList *l) {
70 if (l == NULL) return 0;
72 UcxList *e = l;
73 size_t s = 1;
74 while (e->next != NULL) {
75 e = e->next;
76 s++;
77 }
79 return s;
80 }
82 void ucx_list_foreach(UcxList *l, ucx_callback fnc, void* data) {
83 UcxList *e = l;
84 while (e != NULL) {
85 fnc(e, data);
86 e = e->next;
87 }
88 }