0% found this document useful (0 votes)
7 views5 pages

Hashing 12ex

Uploaded by

ssivarani78
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Hashing 12ex

Uploaded by

ssivarani78
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Ex.No.

12 Implementation of Open Addressing Hashing collision Technique


Date:

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);
}

int create(int num)


{
int key;
key = num % 10;
return key;
}

void linearprobing(int a[MAX], int key, intnum)


{
int flag, i, count =0;
void display(int a[]);
flag = 0;
if(a[key] == -1)
a[key] = num;
else
{
i=0;
while(i < MAX)
{
if(a[i] != -1)
count++;
i++;
}
if(count == MAX)
{
printf("hash table isfull");
display(a);
getch();
exit(1);
}
for(i=key+1; i<MAX;i++)
if(a[i] == -1)
{
a[i] = num;
flag = 1;
break;
}
for(i=0;i<key&&flag==0;i++)
if(a[i] ==-1)
{
a[i] = num;
flag = 1;
break;
}
}
}

void display(int a[MAX])


{
int i;
printf("\n Hash tableis:");
for(i=0; i<MAX; i++)
printf("\n %d\t\t%d",i,a[i]);
}
Output

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.

You might also like