HASH FUNCTION
QUESTION:
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.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int *ht,index;
int count = 0;
{
index =
key % m;
while(ht[index] != -1)
{
index = (index+1)%m;
}
ht[index]
= key;
count++;
}
void display()
{
int i;
if(count ==
0)
{
printf("\nHash Table is empty");
return;
}
for(i=0;
i<m; i++)
printf("\n T[%d] --> %d ", i, ht[i]);
}
{
int i;
printf("\nEnter the number of employee records (N) : ");
scanf("%d", &n);
scanf("%d", &m);
for(i=0;
i<m; i++)
ht[i] = -1;
for(i=0;
i<n; i++)
scanf("%d", &key[i]);
{
if(count == m)
{
printf("\n~~~Hash table is full. Cannot insert the record %d
key~~~",i+1);
break;
}
insert(key[i]);
}
display();
}
OUTPUT:
No comments:
Post a Comment