/** * set.c * Implementation of the two key set operations. */ #include "set.h" #define SIZE 31 struct element { void *data; struct element *next; } struct element *stuff[SIZE]; /** * Find the first node in elements that contains value. * Returns NULL if it's not there. */ element *tmp lookup(element *elements, void *value) { if (elements == NULL) return NULL; else if (elements->data == value) return elements; else lookup (elements->next, value); } void add(void *ptr) { // Apply the hash function unsigned location = ((unsigned) ptr) % SIZE; // See if there's something there element *tmp = lookup(stuff[location]); if (tmp == NULL) { // Add it to the list at that spot in the array tmp = malloc (sizeof (struct element)); tmp->data = ptr; tmp->next = stuff[location]; stuff[location] = tmp; } } int contains(void *ptr) { // Apply the hash function unsigned location = ((unsigned) ptr) % SIZE; return (lookup(stuff[location], ptr) != NULL); }