1 #include "dlist.h" |
1 #include "dlist.h" |
2 |
2 |
|
3 void ucx_dlist_free(UcxDlist *l) { |
|
4 UcxDlist *e = l, *f; |
|
5 while (e != NULL) { |
|
6 f = e; |
|
7 e = e->next; |
|
8 free(f); |
|
9 } |
|
10 } |
|
11 |
3 UcxDlist *ucx_dlist_append(UcxDlist *l, void *data) { |
12 UcxDlist *ucx_dlist_append(UcxDlist *l, void *data) { |
|
13 UcxDlist *nl = (UcxDlist*) malloc(sizeof(UcxDlist)); |
|
14 if (nl == NULL) return NULL; |
4 |
15 |
|
16 nl->data = data; |
|
17 nl->next = NULL; |
|
18 if (l == NULL) { |
|
19 return nl; |
|
20 } else { |
|
21 UcxDlist *t = ucx_dlist_last(l); |
|
22 t->next = nl; |
|
23 nl->prev = t; |
|
24 return l; |
|
25 } |
5 } |
26 } |
6 |
27 |
7 UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data) { |
28 UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data) { |
|
29 UcxDlist *nl = ucx_dlist_append(NULL, data); |
|
30 if (nl == NULL) return NULL; |
8 |
31 |
|
32 if (l != NULL) { |
|
33 nl->next = l; |
|
34 l->prev = nl; |
|
35 } |
|
36 return nl; |
9 } |
37 } |
10 |
38 |
11 UcxDlist *ucx_dlist_concat(UcxDlist *l1, UcxDlist *l2) { |
39 UcxDlist *ucx_dlist_concat(UcxDlist *l1, UcxDlist *l2) { |
12 |
40 if (l1 == NULL) { |
|
41 return l2; |
|
42 } else { |
|
43 UcxDlist *last = ucx_dlist_last(l1); |
|
44 last->next = l2; |
|
45 l2->prev = last; |
|
46 return l1; |
|
47 } |
13 } |
48 } |
14 |
49 |
15 UcxDlist *ucx_dlist_last(UcxDlist *l) { |
50 UcxDlist *ucx_dlist_last(UcxDlist *l) { |
16 if (l == NULL) return NULL; |
51 if (l == NULL) return NULL; |
17 |
52 |
21 } |
56 } |
22 return e; |
57 return e; |
23 } |
58 } |
24 |
59 |
25 UcxDlist *ucx_dlist_get(UcxDlist *l, int index) { |
60 UcxDlist *ucx_dlist_get(UcxDlist *l, int index) { |
|
61 if (l == NULL) return NULL; |
|
62 |
|
63 UcxDlist *e = l; |
|
64 while (e->next != NULL && index > 0) { |
|
65 e = e->next; |
|
66 index--; |
|
67 } |
26 |
68 |
|
69 return index == 0 ? e : NULL; |
27 } |
70 } |
28 |
71 |
29 size_t ucx_dlist_size(UcxDlist *l) { |
72 size_t ucx_dlist_size(UcxDlist *l) { |
30 if (l == NULL) return 0; |
73 if (l == NULL) return 0; |
31 |
74 |
38 |
81 |
39 return s; |
82 return s; |
40 } |
83 } |
41 |
84 |
42 void ucx_dlist_foreach(UcxDlist *l, ucx_callback fnc, void* data) { |
85 void ucx_dlist_foreach(UcxDlist *l, ucx_callback fnc, void* data) { |
43 |
86 UcxDlist *e = l; |
|
87 while (e != NULL) { |
|
88 fnc(e, data); |
|
89 e = e->next; |
|
90 } |
44 } |
91 } |
45 |
92 |
46 /* dlist specific functions */ |
93 /* dlist specific functions */ |
47 UcxDlist *ucx_dlist_first(UcxDlist *l) { |
94 UcxDlist *ucx_dlist_first(UcxDlist *l) { |
|
95 if (l == NULL) return NULL; |
48 |
96 |
|
97 UcxDlist *e = l; |
|
98 while (e->prev != NULL) { |
|
99 e = e->prev; |
|
100 } |
|
101 return e; |
49 } |
102 } |