summaryrefslogtreecommitdiff
path: root/src/ll.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ll.c')
-rw-r--r--src/ll.c75
1 files changed, 66 insertions, 9 deletions
diff --git a/src/ll.c b/src/ll.c
index ae62e89..4819226 100644
--- a/src/ll.c
+++ b/src/ll.c
@@ -47,17 +47,26 @@ int dllnode_free(dllnode **n) {
47 return 0; 47 return 0;
48} 48}
49 49
50void dlinkedlist_init(dlinkedlist **ll) { 50dlinkedlist * dlinkedlist_init(void) {
51 (*ll) = xcalloc(1, sizeof(**ll)); 51 dlinkedlist *ll = xcalloc(1, sizeof(*ll));
52 52
53 (*ll)->start = NULL; 53 ll->start = NULL;
54 (*ll)->end = NULL; 54 ll->end = NULL;
55 (*ll)->size = 0; 55 ll->size = 0;
56 56
57 return; 57 return ll;
58} 58}
59 59
60void dlinkedlist_free(dlinkedlist **ll) { 60int dlinkedlist_free(dlinkedlist **ll) {
61 if(!ll) {
62 errno = EINVAL;
63 return -1;
64 }
65 if(!(*ll)) {
66 errno = EINVAL;
67 return -1;
68 }
69
61 dllnode *p = (*ll)->start, *n; 70 dllnode *p = (*ll)->start, *n;
62 while(p != NULL) { 71 while(p != NULL) {
63 n = p->next; 72 n = p->next;
@@ -67,7 +76,7 @@ void dlinkedlist_free(dlinkedlist **ll) {
67 76
68 free(*ll); 77 free(*ll);
69 (*ll) = NULL; 78 (*ll) = NULL;
70 return; 79 return 0;
71} 80}
72 81
73int dlinkedlist_firstitem(dlinkedlist * const ll, void *data, dlinkedlist_freecallback dfreecb) { 82int dlinkedlist_firstitem(dlinkedlist * const ll, void *data, dlinkedlist_freecallback dfreecb) {
@@ -210,4 +219,52 @@ int dlinkedlist_remove(dlinkedlist * const ll, size_t index) {
210 ll->size--; 219 ll->size--;
211 220
212 return 0; 221 return 0;
213} \ No newline at end of file 222}
223
224size_t dlinkedlist_size(const dlinkedlist * const ll) {
225 if(!ll)
226 RETURNWERR(EINVAL, -1);
227
228 return ll->size;
229}
230
231int dlinkedlist_isempty(const dlinkedlist * const ll) {
232 if(!ll)
233 RETURNWERR(EINVAL, -1);
234
235 return (ll->size == 0);
236}
237
238
239
240
241// Sample code
242#define COMPILE_SAMPLE
243#ifdef COMPILE_SAMPLE
244
245#include <stdio.h>
246#include <string.h>
247
248int genfree(void *data) {
249 free(data);
250 return 0;
251}
252
253int main() {
254 dlinkedlist *dll = dlinkedlist_init();
255 dlinkedlist_append(dll, (void*)strdup("THIS"), genfree);
256 dlinkedlist_append(dll, (void*)strdup("IS"), genfree);
257 dlinkedlist_append(dll, (void*)strdup("A"), genfree);
258 dlinkedlist_append(dll, (void*)strdup("STRING"), genfree);
259
260 // This is crashing as of right now
261 char *retval;
262 for(int i = 0; i < dlinkedlist_size(dll); i++)
263 printf("%s ", (((retval = (char*)dlinkedlist_get(dll, i)) != NULL) ? retval : "null") );
264 printf("\n");
265
266 dlinkedlist_free(&dll);
267
268 return 0;
269}
270#endif \ No newline at end of file