Fri, 30 Dec 2011 14:06:56 +0100
Added some safs
/* * File: list.c * Author: olaf * * Created on 18. Dezember 2010, 11:23 */ #include <stdio.h> #include <stdlib.h> #include "list.h" /* add functions */ sdlist_t* sdlist_add(sdlist_t *list, void *data) { sdlist_t *elm = malloc(sizeof(sdlist_t)); elm->data = data; elm->next = NULL; return sdlist_addl(list, elm); } sdlist_t* sdlist_addl(sdlist_t *list, sdlist_t *l) { if (list != NULL) { sdlist_t *end = sdlist_getlast(list); end->next = l; return list; } else { return l; } } /* remove functions */ sdlist_t* sdlist_remove(sdlist_t *list, void *data) { sdlist_t *s = list; sdlist_t *prev = NULL; while (list != NULL) { if (list->data == data) { sdlist_t *nl = sdlist_remove_elm(s, prev, list); free(list); return nl; } prev = list; list = list->next; } return s; } sdlist_t* sdlist_removei(sdlist_t *list, int index) { sdlist_t *s = list; sdlist_t *prev = NULL; int i = 0; while (list != NULL) { if (i == index) { sdlist_t *nl = sdlist_remove_elm(s, prev, list); free(list); return nl; } prev = list; list = list->next; i++; } return s; } sdlist_t* sdlist_removel(sdlist_t *list, sdlist_t *l) { sdlist_t *s = list; sdlist_t *prev = NULL; while (list != NULL) { if (list == l) { return sdlist_remove_elm(s, prev, list); } prev = list; list = list->next; } return s; } sdlist_t *sdlist_remove_elm(sdlist_t *list, sdlist_t *prev, sdlist_t *elm) { if (elm == NULL) { return list; } if (prev == NULL) { return elm->next; } prev->next = elm->next; return list; } /* insert functions */ void sdlist_insert(sdlist_t *elm, void *data) { sdlist_t *newelm = malloc(sizeof(sdlist_t)); newelm->data = data; sdlist_insertl(elm, newelm); } void sdlist_insertl(sdlist_t *elm, sdlist_t *l) { if (elm == NULL || l == NULL) { return; } l->next = elm->next; elm->next = l; } /* get functions */ sdlist_t* sdlist_get(sdlist_t *list, void *data) { while (list != NULL) { if (list->data == data) { return list; } list = list->next; } return NULL; } sdlist_t* sdlist_geti(sdlist_t *list, int index) { for (int i=0;i<index;i++) { if (list == NULL) { return NULL; } list = list->next; } return list; } sdlist_t* sdlist_getlast(sdlist_t *list) { while(list->next != NULL) { list = list->next; } return list; } /* miscellaneous functions */ size_t sdlist_length(sdlist_t *list) { int i = 0; while(list->next != NULL) { list = list->next; i++; } return i; } void sdlist_free(sdlist_t *elm) { free(elm); } void sdlist_foreach(sdlist_t *list, sdlist_iterator_func f, void *data) { while(list != NULL) { int r = f(list, data); if (r) { return; } list = list->next; } }