Friday, September 24, 2010

DSA EX.NO:8 IMPLEMENTATION OF HASH TABLE

EX.NO:8                      IMPLEMENTATION OF HASH TABLE


AIM:

    To write a c-program to implement Hash table.

ALGORITHM:

Step 1. Start the program.
Step 2. Declare the size of hash table and variables for creating the hash
             table.
Step 3. Depending upon the choice, call the appropriate function.
 
Step 4:INSERT:
*This  function  inserts  the element in the hash table.
*Read the element from the user and insert using hash function key mod 10.
*If the array size exceeds the limit, print statement ”Hash table is full”.

Step 4. SEARCH:
*Read the element which is to be searched from user.
*Using hash function, find out the location of the element to be searched.
*If found print the element found and its position else not found.

Step 5. DELETE:
*This function is used to delete an element from table.
*Get the value to be deleted from the user.
*Call the search function() to find  whether the element to be deleted  is present  in the table or not.
*If found delete the element else print not found.

Step 6. DISPLAY:
*This function is used to display the created hash table.
Step 7. Stop the program.


PROGRAM:

#include<stdio.h>
int ar[10],i,g=0,key;
void insert();
void search();
void delet();
void display();
void main()
{
int ch;
clrscr();
for(i=0;i<10;i++)
ar[i]=-1;
do
{
printf("\n\t\t\tMENU\n1.insert\n2.search\n3.delete\n4.display\n5.exit");
printf(“\n Enter the choice: ”);
scanf("%d",&ch);
switch(ch)
{
case 1:insert();
break;
case 2:search();
break;
case 3:delet();
break;
case 4:display();
break;
case 5:exit(0);
break;
default:printf("\ninvalid choice entry!!!!\n");
exit(0);
}
}
while(ch!=5);
}
void insert()
{
int item,f=0;
printf("\nenter the element tobe inserted:");
scanf("%d",&item);
key=(item%10);
if(ar[key]==-1)
{
ar[key]=item;
f=1;
}
else
{
if(key<9)
{
for(i=key+1;i<10;i++)
{
if(ar[i]==-1)
{
f=1;
ar[i]=item;
break;
}
}
}
if(f==0)
{
for(i=0;i<key;i++)
{
if(ar[i]==-1)
{
f=1;
ar[i]=item;
break;
}
}
}
}
if(f==0)
printf("\nhash table is full\n");
}
void display()
{
for(i=0;i<10;i++)
if(ar[i]!=-1)
{
g=1;
break;
}
if(g==1)
{
printf("\nindex value");
for(i=0;i<10;i++)
{
if(ar[i]==-1)
printf("\n%d\t____",i);
else
printf("\n%d\t%d",i,ar[i]);
}
}
else
printf("\n the hash table is empty!!!");
printf("\n");
}
void search()
{
int item,f=0;
g=0;
printf("\nentry the element to be searched:");
scanf("%d",&item);
key=(item%10);
for(i=0;i<10;i++)
{
if(ar[i]!=-1)
{
g=1;
break;
}
}
if(g==1)
{
if(ar[key]==item)
{
f=1;
}
else
{
if(key<9)
{
for(i=key+1;i<10;i++)
{
if(ar[i]==item)
{
f=1;
key=i;
break;
}
}
}
if(f==0)
{
for(i=0;i<key;i++)
{
if(ar[i]==item)
{
f=1;
key=i;
break;
}
}
}
}
if(f==1)
printf("\nthe item searched was found inthe hash table at position%d!",key);
else{
key=-1;
printf("\nthe item searched was not found inthe hash table");
}
}
else
printf("\nhash table is empty!!!\n");
}


void delet()
{
search();
if(g!=0)
{
if(key!=-1)
{
printf("\nthe element deleted is %d",ar[key]);
ar[key]=-1;
}
}
}