DSA Program 12
DSA Program 12
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. Design and 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.
C Program:
#include<stdio.h>
#include<string.h>
struct employee
{
int key;
int id;
char name[20];
};
int a[max];
int count=0;
void linearprobe(int key,int id,char name[20]);
void display();
void main()
{
int choice,i,key,id;
char name[20];
clrscr();
for(i=0;i<max;i++)
a[i]=-1;
do
{
printf("Enter employee key ,id and name\n");
scanf("%d%d%s",&key,&id,&name);
linearprobe(key,id,name);
display();
printf("\ndo you want to continue (1 for Continue & 0 for Exit)\n");
scanf("%d",&choice);
}while(choice);
}
void linearprobe(int key,int id,char name[20])
{
int rem,i;
rem=key%max;
if(count==max)
{
printf("hash table is full\n");
getch();
exit(0);
}
else
{
if(a[rem]==-1)
{
a[rem]=1;
e[rem].key=key;
e[rem].id=id;
strcpy(e[rem].name,name);
count++;
}
else
{
printf("\nCollision Detected\n");
for(i=rem+1; i!=rem;i=(i+1)%max)
{
if(a[i]==-1)
{
count++;
a[i]=1;
e[i].id=id;
e[i].key=key;
strcpy(e[i].name,name);
break;
}
}
}
}
}
void display()
{
int i;
printf("\nHASH TABLE\n");
printf("___________________________________________\n");
printf("Address\t Key\t Id\t Name\n");
printf("___________________________________________\n");
for(i=0;i<max;i++)
if(a[i]==1)
printf("\n[%d]\t %d\t %d\t %s\n",i,e[i].key,e[i].id,e[i].name);
printf("___________________________________________\n");
}
OUTPUTS:
Enter employee key, id and name
1234 20 Arun
HASH TABLE
_______________________________________________
Address Key Id Name
_______________________________________________
[34] 1234 20 Arun
_______________________________________________
do you want to continue (1 for Continue & 0 for Exit)
1
Enter employee key, id and name
2356 30 Bhanu
HASH TABLE
_______________________________________________
Address Key Id Name
_______________________________________________
[34] 1234 20 Arun
[56] 2356 30 Bhanu
_______________________________________________
do you want to continue (1 for Continue & 0 for Exit)
1
Enter employee key, id and name
5634 40 Harish
Collision Detected
HASH TABLE
_______________________________________________
Address Key Id Name
_______________________________________________
[34] 1234 20 Arun
[35] 5634 40 Harish
[56] 2356 30 Bhanu
_______________________________________________
do you want to continue (1 for Continue & 0 for Exit)
1
Enter employee key, id and name
3456 50 Karthik
Collision Detected
HASH TABLE
_______________________________________________
Address Key Id Name
_______________________________________________
[34] 1234 20 Arun
[35] 5634 40 Harish
[56] 2356 30 Bhanu
[57] 3456 50 Karthik
_______________________________________________
do you want to continue (1 for Continue & 0 for Exit)
0