#include #include #include #include #define SIZE 20 struct DataItem { int data; int key; }; struct DataItem* hashArray[SIZE]; int hashCode(int key){ return key % SIZE; } struct DataItem *search(int key){ //get the hash int hashIndex = hashCode(key); //move in array until an empty while(hashArray[hashIndex] != NULL){ if(hashArray[hashIndex]->key == key) return hashArray[hashIndex]; //go to next cell ++hashIndex; //wrap around the table hashIndex %= SIZE; } return NULL; } void insert(int key,int data){ struct DataItem *item = malloc(sizeof(struct DataItem)); item->data = data; item->key = key; //get the hash int hashIndex = hashCode(key); //move in array until an empty or deleted cell while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1){ //go to next cell ++hashIndex; //wrap around the table hashIndex %= SIZE; } hashArray[hashIndex] = item; } void display(){ int i = 0; for(i = 0; ikey,hashArray[i]->data); else printf(" ~~ "); } printf("\n"); } int main(){ insert(1, 20); insert(2, 70); insert(42, 80); insert(4, 25); insert(12, 44); insert(14, 32); insert(17, 11); insert(13, 78); insert(37, 97); display(); struct DataItem* item; item = search(37); if(item != NULL){ printf("Element found: %d\n", item->data); }else { printf("Element not found\n"); } item = search(36); if(item != NULL){ printf("Element found: %d\n", item->data); }else { printf("Element not found\n"); } for (int i = 0; i < SIZE; i++){ free(hashArray[i]); } }