-
Notifications
You must be signed in to change notification settings - Fork 1
/
Double Hashing.c
115 lines (102 loc) · 2.13 KB
/
Double Hashing.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<stdio.h>
#include<stdlib.h>
#define SIZE 13
int arr[SIZE]={};
int count;
int CalculateHash(int key)
{
return key%SIZE;
}
void Insert(int element)
{
if(count==SIZE){
printf("Error.\nTable if FULL\n");
exit(EXIT_FAILURE);
}
int probe=CalculateHash(element);
int offset=8-(element%8);
while(arr[probe]!=NULL && arr[probe]!=-1)
{
probe=(probe+offset)%SIZE;
}
arr[probe]=element;
count++;
}
void Delete(int element)
{
if(count==0){
printf("Error.\nTable if EMPTY\n");
exit(EXIT_FAILURE);
}
int loc=Search(element);
if(loc!=-1)
{
arr[loc]=-1;
count--;
}
}
int Search(int element)
{
if(count==0){
printf("Error.\nTable if EMPTY\n");
exit(EXIT_FAILURE);
}
int probe=CalculateHash(element);
int offset=8-(element%8);
while(arr[probe]!=0)
{
if(arr[probe]==element)
return probe;
probe=(probe+offset)%SIZE;
}
printf("\nElement %d not found.\n",element);
return -1;
}
void PrintList()
{
int i;
for(i=0;i<SIZE;i++)
printf("%d%c",arr[i],' ');
}
int main()
{
int ch,item;
printf("Enter Elements to insert in hash table :");
printf("\nTo STOP, enter 0\n\n");
do{
scanf("%d",&item);
if(item)
Insert(item);
}while(item!=0);
PrintList();
printf("\nEnter the elements you want to delete :");
printf("\nTo STOP, enter 0\n\n");
do
{
scanf("%d",&item);
if(item)
Delete(item);
}while(item);
PrintList();
printf("\nEnter the elements you want to search :");
printf("\nTo STOP, enter 0\n\n");
do
{
scanf("%d",&item);
if(item){
int loc;
loc=Search((item));
if(loc!=-1)
printf("\nElement %d Found at location %d",item,loc);
}
}while(item);
printf("\nEnter Elements to insert in hash table :");
printf("\nTo STOP, enter 0\n\n");
do{
scanf("%d",&item);
if(item)
Insert(item);
}while(item!=0);
PrintList();
return 0;
}