-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash_Table.cpp
262 lines (232 loc) · 6.37 KB
/
Hash_Table.cpp
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
Title : Program to implement Hash Table Using Linear Probing Without Replacement.
*/
#include <iostream>
using namespace std;
#define MAX 10
#define SIZE 10
struct hashtable
{
int chain;
long key;
string name;
} s[MAX];
class hasht
{
public:
int hash(int telno);
};
int hasht::hash(int telno)
{
int h;
h = telno % MAX;
return h;
}
int main()
{
int i, flag1 = 0, ch, ch1, no1, h, c, index, k, temp, ch3;
int data[SIZE], flag[SIZE], chain[SIZE], j, x;
string sname;
char name1[SIZE];
hasht t;
for (i = 0; i < MAX; i++)
{
s[i].key = -1;
s[i].chain = -1;
s[i].name = '-';
}
do
{
for (j = 0; j < MAX; j++)
{
if (s[j].key == -1)
break;
}
if (j == MAX)
{
cout << "Hash Table is Full";
break;
}
cout << "Enter The Name -->> ";
cin >> sname;
cout << "Enter The Age -->> ";
cin >> no1;
h = t.hash(no1);
c = -1;
index = h;
do
{
if (s[h].key == -1)
{
flag1 = 1;
s[h].key = no1;
s[h].name = sname;
if (c != -1)
s[c].chain = h;
}
else if (t.hash(s[h].key) == index)
c = h;
h = t.hash(h + 1);
} while (flag1 == 0 && h < MAX);
flag1 = 0;
cout << "\nINDEX"
<< "\t"
<< "AGE"
<< "\t"
<< "NAME"
<< "\t"
<< "CHAIN"
<< "\t" << endl;
for (i = 0; i < MAX; i++)
cout << i << "\t" << s[i].key << "\t\t" << s[i].name << "\t" << s[i].chain << "\n";
cout << "__________________________________________________________________________" << endl;
cout << "Do you wish to continue? Press(1/2) " << endl;
cin >> ch;
} while (ch == 1);
return 0;
}
/* OutPut -->>
Enter The Name -->> Abhi
Enter The Age -->> 31
INDEX AGE NAME CHAIN
0 -1 - -1
1 31 Abhi -1
2 -1 - -1
3 -1 - -1
4 -1 - -1
5 -1 - -1
6 -1 - -1
7 -1 - -1
8 -1 - -1
9 -1 - -1
__________________________________________________________________________
Do you wish to continue? Press(1/2)
1
Enter The Name -->> Abhishek
Enter The Age -->> 21
INDEX AGE NAME CHAIN
0 -1 - -1
1 31 Abhi 2
2 21 Abhishek -1
3 -1 - -1
4 -1 - -1
5 -1 - -1
6 -1 - -1
7 -1 - -1
8 -1 - -1
9 -1 - -1
__________________________________________________________________________
Do you wish to continue? Press(1/2)
1
Enter The Name -->> Kabhi
Enter The Age -->> 02
INDEX AGE NAME CHAIN
0 -1 - -1
1 31 Abhi 2
2 21 Abhishek -1
3 2 Kabhi -1
4 -1 - -1
5 -1 - -1
6 -1 - -1
7 -1 - -1
8 -1 - -1
9 -1 - -1
__________________________________________________________________________
Do you wish to continue? Press(1/2)
1
Enter The Name -->> Meinbhi
Enter The Age -->> 55
INDEX AGE NAME CHAIN
0 -1 - -1
1 31 Abhi 2
2 21 Abhishek -1
3 2 Kabhi -1
4 -1 - -1
5 55 Meinbhi -1
6 -1 - -1
7 -1 - -1
8 -1 - -1
9 -1 - -1
__________________________________________________________________________
Do you wish to continue? Press(1/2)
1
Enter The Name -->> Lazy
Enter The Age -->> 5
INDEX AGE NAME CHAIN
0 -1 - -1
1 31 Abhi 2
2 21 Abhishek -1
3 2 Kabhi -1
4 -1 - -1
5 55 Meinbhi 6
6 5 Lazy -1
7 -1 - -1
8 -1 - -1
9 -1 - -1
__________________________________________________________________________
Do you wish to continue? Press(1/2)
1
Enter The Name -->> tubhi
Enter The Age -->> 98
INDEX AGE NAME CHAIN
0 -1 - -1
1 31 Abhi 2
2 21 Abhishek -1
3 2 Kabhi -1
4 -1 - -1
5 55 Meinbhi 6
6 5 Lazy -1
7 -1 - -1
8 98 tubhi -1
9 -1 - -1
__________________________________________________________________________
Do you wish to continue? Press(1/2)
1
Enter The Name -->> yehbhi
Enter The Age -->> 8
INDEX AGE NAME CHAIN
0 -1 - -1
1 31 Abhi 2
2 21 Abhishek -1
3 2 Kabhi -1
4 -1 - -1
5 55 Meinbhi 6
6 5 Lazy -1
7 -1 - -1
8 98 tubhi 9
9 8 yehbhi -1
__________________________________________________________________________
Do you wish to continue? Press(1/2)
1
Enter The Name -->> kuchbhi
Enter The Age -->> 84
INDEX AGE NAME CHAIN
0 -1 - -1
1 31 Abhi 2
2 21 Abhishek -1
3 2 Kabhi -1
4 84 kuchbhi -1
5 55 Meinbhi 6
6 5 Lazy -1
7 -1 - -1
8 98 tubhi 9
9 8 yehbhi -1
__________________________________________________________________________
Do you wish to continue? Press(1/2)
1
Enter The Name -->> aapbhi
Enter The Age -->> 4
INDEX AGE NAME CHAIN
0 -1 - -1
1 31 Abhi 2
2 21 Abhishek -1
3 2 Kabhi -1
4 84 kuchbhi 7
5 55 Meinbhi 6
6 5 Lazy -1
7 4 aapbhi -1
8 98 tubhi 9
9 8 yehbhi -1
__________________________________________________________________________
Do you wish to continue? Press(1/2)
*/