Given a File of N employee records with a set K of Keys (4-digit) which uniquely determine the records in file F. Assume that file F is maintained in memory by a Hash Table (HT) of m memory locations with L as the set of memory addresses (2-digit) of locations in HT. Let the keys in K and addresses in L are Integers. Develop a Program in C that uses Hash function H: K →L as H(K)=K mod m (remainder method), and implement hashing technique to map a given key K to the address space L. Resolve the collision (if any) using linear probing.
#include <stdio.h>
#define SIZE 10
int hashTable[SIZE];
void init() {
for (int i = 0; i < SIZE; i++)
hashTable[i] = -1;
}
void insert(int key) {
int index = key % SIZE;
int start = index;
while (hashTable[index] != -1) {
index = (index + 1) % SIZE;
if (index == start) {
printf("Hash Table is Full!\n");
return;
}
}
hashTable[index] = key;
}
void display() {
printf("\nHash Table:\n");
for (int i = 0; i < SIZE; i++)
printf("%d => %d\n", i, hashTable[i]);
}
int main() {
int n, key;
init();
printf("Enter number of employee keys: ");
scanf("%d", &n);
printf("Enter %d 4-digit keys:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &key);
insert(key);
}
display();
return 0;
}
Enter number of employee keys: 5
Enter 5 4-digit keys:
1234
2345
3456
4567
5678
Hash Table:
0 => -1
1 => -1
2 => -1
3 => -1
4 => 1234
5 => 2345
6 => 3456
7 => 4567
8 => 5678
9 => -1
Replay !
Share Your Thoughts