Wednesday 22 August 2012

Logivc of Implementation of Hash Function | Data Structure

How to write Data structure  Program for implementation of Hash Function using for loop with output
#include<stdio.h>
#include<conio.h>

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

*/
Share This
Previous Post
Next Post

FYJC XI standard online admisson Process and declaraton of Merit list . Cut off List For prevous year also . 10 Th Results onlne declaraton Maharashtra Region .

0 comments: