Question
In C, how would i add linear probing to handle collisions in the following code? #include #include #define HASH_TABLE_SIZE 1057 typedef struct Entry Entry, *EntryPtr;
In C, how would i add linear probing to handle collisions in the following code?
#include
#include
#define HASH_TABLE_SIZE 1057
typedef struct Entry Entry, *EntryPtr;
struct Entry {
char * string;
int key;
int count;
};
Entry hash_table[HASH_TABLE_SIZE];
int key = 0;
int hashCode(int key) {
return key % HASH_TABLE_SIZE;
}
void add(char * tag)
{
Entry *item = (struct Entry*) malloc(sizeof(struct Entry));
item->string = tag;
item->key = key;
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty or deleted cell
while(hash_table[hashIndex].count != 0) {
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= HASH_TABLE_SIZE;
}
hash_table[hashIndex] = *item;
key ++;
item->count ++;
printf("Inserted %s into the hash table. ", tag);
}
void display() {
int i = 0;
printf(" The has table contents are: ");
for(i = 0; i
printf(" (%d -> %s) ",hash_table[i].key, hash_table[i].string);
}
printf(" ");
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started