#include "hash_table.h" #include #include struct animal_count { char name[32]; int count; }; size_t name_hash(char *name){ size_t hashval = 1; for(char *l = name; *l; l++){ hashval *= *l; } return hashval; } size_t ac_hash(void *p){ struct animal_count *ac = p; return name_hash(ac->name); } int main(){ struct table_description animal_table; init(&animal_table, sizeof(struct animal_count), 20, ac_hash); struct animal_count new_item = {"Squirrel", 0}; insert_item(&animal_table, &new_item); strcpy(new_item.name, "Chicken"); insert_item(&animal_table, &new_item); strcpy(new_item.name, "Cat"); insert_item(&animal_table, &new_item); struct animal_count *ref; ref = lookup_item(&animal_table, name_hash("Chicken")); ref->count = 47; ref = lookup_item(&animal_table, name_hash("Cat")); ref->count = 5; ref = lookup_item(&animal_table, name_hash("Chicken")); printf("Found %s : %d\n", ref->name, ref->count); while(ref = iterate(&animal_table)) printf("Iterating: %s : %d\n", ref->name, ref->count); return 0; }