1 /* |
|
2 * File: list.c |
|
3 * Author: olaf |
|
4 * |
|
5 * Created on 18. Dezember 2010, 11:23 |
|
6 */ |
|
7 |
|
8 #include <stdio.h> |
|
9 #include <stdlib.h> |
|
10 |
|
11 #include "list.h" |
|
12 |
|
13 /* add functions */ |
|
14 sdlist_t* sdlist_add(sdlist_t *list, void *data) { |
|
15 sdlist_t *elm = malloc(sizeof(sdlist_t)); |
|
16 elm->data = data; |
|
17 elm->next = NULL; |
|
18 |
|
19 return sdlist_addl(list, elm); |
|
20 } |
|
21 |
|
22 sdlist_t* sdlist_addl(sdlist_t *list, sdlist_t *l) { |
|
23 if (list != NULL) { |
|
24 sdlist_t *end = sdlist_getlast(list); |
|
25 end->next = l; |
|
26 return list; |
|
27 } else { |
|
28 return l; |
|
29 } |
|
30 } |
|
31 |
|
32 |
|
33 |
|
34 /* remove functions */ |
|
35 sdlist_t* sdlist_remove(sdlist_t *list, void *data) { |
|
36 sdlist_t *s = list; |
|
37 |
|
38 sdlist_t *prev = NULL; |
|
39 while (list != NULL) { |
|
40 if (list->data == data) { |
|
41 sdlist_t *nl = sdlist_remove_elm(s, prev, list); |
|
42 free(list); |
|
43 return nl; |
|
44 } |
|
45 |
|
46 prev = list; |
|
47 list = list->next; |
|
48 } |
|
49 |
|
50 return s; |
|
51 } |
|
52 |
|
53 sdlist_t* sdlist_removei(sdlist_t *list, int index) { |
|
54 sdlist_t *s = list; |
|
55 |
|
56 sdlist_t *prev = NULL; |
|
57 int i = 0; |
|
58 while (list != NULL) { |
|
59 if (i == index) { |
|
60 sdlist_t *nl = sdlist_remove_elm(s, prev, list); |
|
61 free(list); |
|
62 return nl; |
|
63 } |
|
64 |
|
65 prev = list; |
|
66 list = list->next; |
|
67 i++; |
|
68 } |
|
69 |
|
70 return s; |
|
71 } |
|
72 |
|
73 sdlist_t* sdlist_removel(sdlist_t *list, sdlist_t *l) { |
|
74 sdlist_t *s = list; |
|
75 |
|
76 sdlist_t *prev = NULL; |
|
77 while (list != NULL) { |
|
78 if (list == l) { |
|
79 return sdlist_remove_elm(s, prev, list); |
|
80 } |
|
81 |
|
82 prev = list; |
|
83 list = list->next; |
|
84 } |
|
85 |
|
86 return s; |
|
87 } |
|
88 |
|
89 |
|
90 sdlist_t *sdlist_remove_elm(sdlist_t *list, sdlist_t *prev, sdlist_t *elm) { |
|
91 if (elm == NULL) { |
|
92 return list; |
|
93 } |
|
94 |
|
95 if (prev == NULL) { |
|
96 return elm->next; |
|
97 } |
|
98 |
|
99 prev->next = elm->next; |
|
100 |
|
101 return list; |
|
102 } |
|
103 |
|
104 |
|
105 |
|
106 /* insert functions */ |
|
107 void sdlist_insert(sdlist_t *elm, void *data) { |
|
108 sdlist_t *newelm = malloc(sizeof(sdlist_t)); |
|
109 newelm->data = data; |
|
110 sdlist_insertl(elm, newelm); |
|
111 } |
|
112 |
|
113 void sdlist_insertl(sdlist_t *elm, sdlist_t *l) { |
|
114 if (elm == NULL || l == NULL) { |
|
115 return; |
|
116 } |
|
117 |
|
118 l->next = elm->next; |
|
119 elm->next = l; |
|
120 } |
|
121 |
|
122 |
|
123 /* get functions */ |
|
124 sdlist_t* sdlist_get(sdlist_t *list, void *data) { |
|
125 while (list != NULL) { |
|
126 if (list->data == data) { |
|
127 return list; |
|
128 } |
|
129 list = list->next; |
|
130 } |
|
131 return NULL; |
|
132 } |
|
133 |
|
134 sdlist_t* sdlist_geti(sdlist_t *list, int index) { |
|
135 for (int i=0;i<index;i++) { |
|
136 if (list == NULL) { |
|
137 return NULL; |
|
138 } |
|
139 list = list->next; |
|
140 } |
|
141 return list; |
|
142 } |
|
143 |
|
144 sdlist_t* sdlist_getlast(sdlist_t *list) { |
|
145 while(list->next != NULL) { |
|
146 list = list->next; |
|
147 } |
|
148 return list; |
|
149 } |
|
150 |
|
151 |
|
152 |
|
153 /* miscellaneous functions */ |
|
154 size_t sdlist_length(sdlist_t *list) { |
|
155 int i = 0; |
|
156 while(list->next != NULL) { |
|
157 list = list->next; |
|
158 i++; |
|
159 } |
|
160 return i; |
|
161 } |
|
162 |
|
163 void sdlist_free(sdlist_t *elm) { |
|
164 free(elm); |
|
165 } |
|
166 |
|
167 void sdlist_foreach(sdlist_t *list, sdlist_iterator_func f, void *data) { |
|
168 while(list != NULL) { |
|
169 int r = f(list, data); |
|
170 if (r) { |
|
171 return; |
|
172 } |
|
173 list = list->next; |
|
174 } |
|
175 } |
|