diff options
Diffstat (limited to 'src/ll.c')
| -rw-r--r-- | src/ll.c | 75 |
1 files changed, 66 insertions, 9 deletions
| @@ -47,17 +47,26 @@ int dllnode_free(dllnode **n) { | |||
| 47 | return 0; | 47 | return 0; |
| 48 | } | 48 | } |
| 49 | 49 | ||
| 50 | void dlinkedlist_init(dlinkedlist **ll) { | 50 | dlinkedlist * 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 | ||
| 60 | void dlinkedlist_free(dlinkedlist **ll) { | 60 | int 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 | ||
| 73 | int dlinkedlist_firstitem(dlinkedlist * const ll, void *data, dlinkedlist_freecallback dfreecb) { | 82 | int 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 | |||
| 224 | size_t dlinkedlist_size(const dlinkedlist * const ll) { | ||
| 225 | if(!ll) | ||
| 226 | RETURNWERR(EINVAL, -1); | ||
| 227 | |||
| 228 | return ll->size; | ||
| 229 | } | ||
| 230 | |||
| 231 | int 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 | |||
| 248 | int genfree(void *data) { | ||
| 249 | free(data); | ||
| 250 | return 0; | ||
| 251 | } | ||
| 252 | |||
| 253 | int 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 | ||
