hash table
hash table:
//IMPLEMENTATION OF STATIC HASH TABLE WITH LINEAR PROBING:
#include <stdio.h>
#include<stdlib.h>
int h[10]={NULL};
void insert()
{
int key,index,i,hkey;
printf("\nHey plz enter a value to insert into hash table:\n");
scanf("%d",&key);
hkey=key%10;
for(i=0;i<10;i++)
{
index=(hkey+i)%10;
if(h[index] == NULL)
{
h[index]=key;
break;
}
}
if(i == 10)
printf("\nOhh..element cannot be inserted\n");
}
int search()
{
int key,index,i, hkey;
printf("\nPlz enter search element:\n");
scanf("%d",&key);
hkey=key%10;
for(i=0;i<10; i++)
{
index=(hkey+i)%10;
if(h[index]==key)
{
printf("value is found at index %d",index);
break;
}
}
if(i == 10)
printf("\n value is not found\n");
}
int deletekey()
{
int key,index,i, hkey;
printf("\nenter deleted element:\n");
scanf("%d",&key);
hkey=key%10;
for(i=0;i<10; i++)
{
index=(hkey+i)%10;
if(h[index]==key)
{
printf("\n In index = %d value = %d is found",index,key);
printf("\n Now,value = %d is deleted...",key );
h[index]=0;
break;
}
}
if(i==10)
printf("\nOhh no..element is not found!!!");
}
void display()
{
int i;
printf("\nelements in the hash table are: \n");
for(i=0;i< 10; i++)
printf("%d [%d]\n",i,h[i]);
}
int main()
{
int ch,i,key;
do
{
printf("\nPress:\n1.Insert\n2.Display\n3.Search\n4.delete\n5.Exit \n");
printf("\nPlz..Enter ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:
deletekey();
break;
case 5:exit(0);
default:
printf("Ohh..u enter invalid key!!!");
}
}while(ch!=5);
}
s
Comments
Post a Comment
hey