-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhash.c
More file actions
159 lines (138 loc) · 3.19 KB
/
hash.c
File metadata and controls
159 lines (138 loc) · 3.19 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* Hashing- Linear and Quadratic Probing */
#include<stdio.h>
#include<conio.h>
int table[100],size;
int hash(int key)
{
int pos;
pos=key%size;
return pos;
}
int linearhash(int key,int j)
{
int hashv,poslh;
hashv=hash(key);
poslh=(hashv+j)%size;
return poslh;
}
int quadhash(int key,int j)
{
int rhashv,posqh;
rhashv=hash(key);
posqh=(rhashv+j*j)%size;
return posqh;
}
void initlz()
{
int i;
for(i=0;i<100;i++)
{
table[i]=-1;
}
}
void main()
{
int i,j,n,prob,emppos,val[100];
clrscr();
printf("\nEnter the size of the table: ");
scanf("%d",&size);
printf("\nEnter the number of entries: ");
scanf("%d",&n);
printf("\n\nNote: You cannot enter negative numbers(-1....) as the array value\n\n");
printf("Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&val[i]);
}
do
{
printf("\n\n1.Linear Probing \n2.Quadratic Probing \n3.Exit \nEnter the option: ");
scanf("%d",&prob);
switch(prob)
{
case 1:
initlz();
for(i=0;i<n;i++)
{
emppos=hash(val[i]);
j=1;
while(table[emppos]!=-1)
{
emppos=linearhash(val[i],j);
j++;
}
table[emppos]=val[i];
}
printf("\n******Displaying the data-Linear probing******");
printf("\nNote: '-1' Represents Empty Location\n");
for(i=0;i<size;i++)
{
printf("\nTable[%d]=%d",i,table[i]);
}
break;
case 2:
initlz();
for(i=0;i<n;i++)
{
emppos=hash(val[i]);
j=1;
while(table[emppos]!=-1)
{
emppos=quadhash(val[i],j);
j++;
}
table[emppos]=val[i];
}
printf("\n******Displaying the data-Quadratic Probing******");
printf("\nNote: '-1' Represents Empty Location\n");
for(i=0;i<size;i++)
{
printf("\nTable[%d]=%d",i,table[i]);
}
break;
}
}while(prob!=3);
getch();
}
/* OUTPUT
Enter the size of the table: 10
Enter the number of entries: 8
Note: You cannot enter negative numbers(-1....) as the array value
Enter the elements: 72 27 36 24 63 81 92 101
1.Linear Probing
2.Quadratic Probing
3.Exit
Enter the option: 1
******Displaying the data-Linear probing******
Note: '-1' Represents Empty Location
Table[0]=-1
Table[1]=81
Table[2]=72
Table[3]=63
Table[4]=24
Table[5]=92
Table[6]=36
Table[7]=27
Table[8]=101
Table[9]=-1
1.Linear Probing
2.Quadratic Probing
3.Exit
Enter the option: 2
******Displaying the data-Quadratic Probing******
Note: '-1' Represents Empty Location
Table[0]=-1
Table[1]=81
Table[2]=72
Table[3]=63
Table[4]=24
Table[5]=101
Table[6]=36
Table[7]=27
Table[8]=92
Table[9]=-1
1.Linear Probing
2.Quadratic Probing
3.Exit
Enter the option: 3
*/