#include "dico.h" #include #include typedef struct list *list; struct list { void *elem; list next; }; struct dico { int (*equal)(void *, void *); list elements; }; dico dico_create(int (*equal)(void *, void *)) { dico temp = malloc(sizeof(struct dico)); temp -> equal = equal; temp -> elements = NULL; return temp; } /* positions at the element if it exists, or at the end if not */ static list * position(list *l, void *obj, int (*equal)(void *, void *)) { for(; *l && !equal(*l, obj); l = &((*l) -> next)) ; return l; } int dico_member(dico d, void *object) { return *position(&(d -> elements), object, d -> equal) != NULL; } void dico_insert(dico d, void *object) { list temp; list *pos = position(&(d -> elements), object, d -> equal); assert(!(*pos)); temp = malloc(sizeof(struct list)); temp -> elem = object; temp -> next = *pos; *pos = temp; } void dico_delete(dico d, void *object) { list temp; list *pos = position(&(d -> elements), object, d -> equal); assert(*pos); temp = *pos; *pos = (*pos) -> next; free(temp); }