Implementation of Hash Function | Write Program for Hash Function

How to write Data structure Program for implementation of Hash Function using for loop with output

#include
#include

int hash(int no)

{
return no%10;
}

void main()

{
int arr[10],i,pos,cnt,no,ch;
clrscr();
for(i=0;i<10;i++)
arr[i]=-1;
do
{
printf("\n\n Enter the no. that you want to insert:- ");
scanf("%d",&no);
pos=hash(no);
if(pos>10)
pos=hash(pos);
if(arr[pos]==-1)
arr[pos]=no;
else
{
i=1;
cnt=0;
while(arr[pos+i]!=-1)
{
i++;
cnt++;
if(cnt==10)
break;
}
if(arr[pos+i]==-1)
arr[pos+i]=no;
}
if(cnt==10)
printf("\n Memory is full.");
printf("\n\n The elements in memory with address:- ");
for(i=0;i<10;i++)
printf("\n[%2d]=>%2d",i,arr[i]);
printf("\n Do you want to continue??(Yes=1/No=0):- ");
scanf("%d",&ch);
}while(ch);
getch();
}

/*

Output:-


Enter the no. that you want to insert:- 4


The elements in memory with address:-

[ 0]=>-1
[ 1]=>-1
[ 2]=>-1
[ 3]=>-1
[ 4]=> 4
[ 5]=>-1
[ 6]=>-1
[ 7]=>-1
[ 8]=>-1
[ 9]=>-1
Do you want to continue??(Yes=1/No=0):- 1

Enter the no. that you want to insert:- 67


The elements in memory with address:-

[ 0]=>-1
[ 1]=>-1
[ 2]=>-1
[ 3]=>-1
[ 4]=> 4
[ 5]=>-1
[ 6]=>-1
[ 7]=>67
[ 8]=>-1
[ 9]=>-1
Do you want to continue??(Yes=1/No=0):- 1

Enter the no. that you want to insert:- 29


The elements in memory with address:-

[ 0]=>-1
[ 1]=>-1
[ 2]=>-1
[ 3]=>-1
[ 4]=> 4
[ 5]=>-1
[ 6]=>-1
[ 7]=>67
[ 8]=>-1
[ 9]=>29
Do you want to continue??(Yes=1/No=0):- 1

Enter the no. that you want to insert:- 60


The elements in memory with address:-

[ 0]=>60
[ 1]=>-1
[ 2]=>-1
[ 3]=>-1
[ 4]=> 4
[ 5]=>-1
[ 6]=>-1
[ 7]=>67
[ 8]=>-1
[ 9]=>29
Do you want to continue??(Yes=1/No=0):- 0

*/

0 comments:

Post a Comment