Static Hashing
AIM:
To implement a Static Hashing (Use Linear probing for collision resolution)
Description:
In this program, Static Hashing is done using Linear probing for collision resolution.
Program:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,i,size,key,flag,Hash_Table[1000];
printf("\n Enter Size of Hash Table : ");
scanf("%d",&size);
for(i=0;i<size;i++)
{
Hash_Table[i]=-1;
}
printf("\n Enter Elements in Hash Table : ");
for(i=0;i<size;i++)
{
scanf("%d",&key);
n=key%size;
while(Hash_Table[n]!=-1)
{
n++;
if(n>=size)
{
n=0;
}
}
Hash_Table[n]=key;
}
printf("\n Final Hash Table : \n");
for(i=0;i<size;i++)
{
printf(" %d\n",Hash_Table[i]);
}
printf("\n Enter element to be searched for : ");
scanf("%d",&key);
n=key%size;
flag=1;
while(key!=Hash_Table[n]&&flag==1)
{
n++;
if(n>=size)
{
n=0;
}
if(n==key%size)
{
flag=0;
}
}
if(flag==1)
{
printf("\n Element %d found at position at : %d",key,n+1);
}
else
{
printf("\n Element Not found");
}
return 0;
}
OUTPUT
Enter Size of Hash Table : 7
Enter Elements in Hash Table : 50 700 76 85 92 73 101
Final Hash Table :
700
50
85
92
73
101
76
Enter element to be searched for : 92
Element 92 found at position at : 4
--------------------------------
Process exited after 10.5 seconds with return value 0
Press any key to continue . . .
No comments:
Post a Comment