Hashing 12ex
Hashing 12ex
Aim
To implement hash table using a C program.
Algorithm
1. Createastructure,data(hashtableitem)withkeyandvalueasdata.
2. Nowcreateanarrayofstructure,dataofsomecertainsize(10,inthiscase).But, the
size of array must be immediately updated to a prime number just greater
than initial array capacity (i.e 10, in thiscase).
3. A menu is displayed on thescreen.
4. Usermustchooseoneoptionfromfourchoicesgiveninthemenu
5. Perform all theoperations
6. Stop
Program
/* Open hashing */
#include <stdio.h>
#include<stdlib.h>
#define MAX 10
main()
{
int a[MAX], num, key,i;
char ans;
int create(int);
voidlinearprobing(int[],int,int);
voiddisplay(int[]);
printf("\nCollisionhandlingbylinearprobing\n\n");
for(i=0; i<MAX;i++)
a[i] = -1;
do
{
printf("\n Enternumber:");
scanf("%d", &num);
key = create(num);
linearprobing(a, key,num);
printf("\nwish tocontinue?(y/n):");
ans = getch();
} while( ans =='y');
display(a);
}
Collisionhandlingbylinearprobing
Enternumber:1
wish to continue?(y/n):
Enter number:26
wish to continue?(y/n):
Enter number:62
wish to continue?(y/n):
Enter number:93
wish to continue?(y/n):
Enter number:84
wish to continue?(y/n):
Enter number:15
wish to continue?(y/n):
Enter number:76
wish to continue?(y/n):
Enter number:98
wish to continue?(y/n):
Enter number:26
wish to continue?(y/n):
Enter number:199
wish tocontinue?(y/n):
Enter number:1234
wish tocontinue?(y/n):
Enternumber:5678
hash table isfull
Hash table is:
0 1234
1 1
2 62
3 93
4 84
5 15
6 26
7 76
8 98
9 199
Result
Thus hashing has been performed successfully.