HASHING TECHNIQUE
Aim:
To implement a program using
Hashing technique.
Algorithm:
Step1: Include necessary header
files
Step2: Declare necessary variables
Step3: Check the value of *S
Then call Insert( )
Print “Enter the string”
Read S
Step4: Check the value of *S
Step5: Then print S by calling
hGetVal( )
Step6: Call PrintHash( )
Step7: End
Algorithm For hINSERT( ):
Step1: Allocate memory to pointer
Step2:
Assign index hGetIndex ( )
Step3: Assign Ptr Key Strdup(key)
Ptr Val
Val
Ptr next h[index]
h[index] Ptr
Step4: Print “h[index]=key”
Step5: Return
Algorithm For hGETVALUE( ):
Step1:
[Ptr=h[hGetIndex(key)]]
Step2:
If[Ptr && strcmp(Ptr key)]
Then Ptr Ptr next
Step3:
If[Ptr],Check the value of Ptr
[Return Ptr Val]
Step4:
[Return -1]
Algorithm For PRINTHASH( ):
Step1: Initialise i=0
Step2: If [i < Hash size]
Then Print i
Assign
Ptr h[i]
Check the value of Ptr
If[Ptr!=0]
Then Ptr Ptr
next
Print “Ptr key=Ptr Val”
Step3:
[Return]
Coding:
#include<conio.h>
#include<stdio.h>
void main()
{
int
a[10]={0,0,0,0,0,0,0,0,0,0};
int
n,value,temp,hashvalue;
clrscr();
printf("Enter
the value of n (table size) ::");
scanf("%d",&n);
do
{
printf("\nEnter
the hash value ::");
scanf("%d",&value);
hashvalue=value%n;
if(a[hashvalue]==0)
{
a[hashvalue]=value;
printf("\na[%d]
The value %d is stored",hashvalue,value);
}
else
{
for(hashvalue++;hashvalue<n;hashvalue++)
{
if(a[hashvalue]==0)
{
printf("Space is allocated!!!Give
another value!!!");
a[hashvalue]=value;
printf("\na[%d] The value %d is
stored",hashvalue,value);
goto
a;
}
}
hashvalue=0;
for(hashvalue;hashvalue<n;hashvalue++)
{
if(a[hashvalue]==0)
{
printf("Space is allocated!!!Give
another value!!!");
a[hashvalue]=value;
printf("\na[%d] The value %d is
stored",hashvalue,value);
goto
a;
}
}
}
a:printf("\nDo
you want to enter more? :: ");
scanf("%d",&temp);
}while(temp==1);
getch();
}
OUTPUT:
Enter the value of n (table size) ::10
Enter the hash value ::10
a[0] The value 10 is stored
Do you want to enter more? :: 1
Enter the hash value ::11
a[1] The value 11 is stored
Do you want to enter more? :: 1
Enter the hash value ::21
Space is allocated!!!Give another
value!!!
a[2] The value 21 is stored
Do you want to enter more? :: 1
Enter the hash value ::4
a[4] The value 4 is stored
Do you want to enter more? :: 1
Enter the hash value ::24
Space is allocated!!!Give another
value!!!
a[5] The value 24 is stored
Do you want to enter more? :: 1
Enter the hash value ::19
a[9] The value 19 is stored
Do you want to enter more? :: 1
Enter the hash value ::29
Space is allocated!!!Give another
value!!!
a[3] The value 29 is stored
Do you want to enter more? :: 1
Enter the hash value ::13
Space is allocated!!!Give another
value!!!
a[6] The value 13 is stored
Do you want to enter more? :: 0
No comments:
Post a Comment