#include #include #include #include #define SIZE 20 // remove code for deleting items struct DataItem { char * key; }; struct DataItem* hashArray[SIZE]; struct DataItem* item; bool debugflag = true; void * mymalloc(size_t s){ void * ptr = malloc(s); memset(ptr, 0, s); if (debugflag) { printf("Mymalloc: %zu bytes: %p\n", s, ptr); } return ptr; } void * myrealloc(void *ptr, size_t s){ void * p = realloc(ptr, s); if (debugflag) { printf("Myrealloc: %zu bytes: %p oldp: %p\n", s, p, ptr); } return p; } void myfree(void * p){ if (debugflag) { printf("Myfree: %p \n", p); } free(p); } int hashCode(char * key){ int sum = 0; for(char * s = key; *s != '\0'; s++){ sum += *s; } return sum % SIZE; } struct DataItem *search(char * key){ //get the hash int hashIndex = hashCode(key); //move in array until an empty while(hashArray[hashIndex] != NULL){ if(strcmp(hashArray[hashIndex]->key, key) == 0) return hashArray[hashIndex]; //go to next cell ++hashIndex; //wrap around the table hashIndex %= SIZE; } return NULL; } void insert(char * key){ struct DataItem *item = (struct DataItem*) mymalloc(sizeof(struct DataItem)); item->key = key; //get the hash int hashIndex = hashCode(key); //move in array until an empty while(hashArray[hashIndex] != NULL){ //go to next cell ++hashIndex; //wrap around the table hashIndex %= SIZE; } hashArray[hashIndex] = item; } void display(){ int i = 0; for(i = 0; ikey); } printf("\n"); } int main(){ insert("one"); insert("two"); insert("three"); insert("four"); insert("five"); insert("six"); display(); item = search("three"); if(item != NULL){ printf("Element found: %s\n", item->key); }else { printf("Element not found\n"); } item = search("ten"); if(item != NULL){ printf("Element found: %s\n", item->key); }else { printf("Element not found\n"); } for (int i = 0; i < SIZE; i++){ myfree(hashArray[i]); } }