-
Notifications
You must be signed in to change notification settings - Fork 0
/
practicalOne.cpp
305 lines (266 loc) · 6.86 KB
/
practicalOne.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
Consider telephone book database of N clients. Make use of a hash table implementation
to quickly look up client‘s telephone number. Make use of two collision handling
techniques and compare them using number of comparisons required to find a set of
telephone numbers
*/
#include "bits/stdc++.h"
using namespace std;
#define SIZE 10
class HashTable
{
private:
vector<pair<string, string>> table;
int getHash(string key)
{
hash<string> hasher;
return hasher(key) % SIZE;
}
public:
HashTable()
{
table.resize(SIZE);
}
void insertLinear(string key, string value)
{
int index = getHash(key);
while (!table[index].first.empty())
{
index = (index + 1) % SIZE;
}
table[index] = make_pair(key, value);
}
void insertQuadratic(string key, string value)
{
int index = getHash(key);
int i = 0;
while (!table[index].first.empty())
{
i++;
index = (index + i * i) % SIZE;
}
table[index] = make_pair(key, value);
}
pair<string, int> searchLinear(string key)
{
int index = getHash(key);
int comparisons = 0;
while (table[index].first != key && !table[index].first.empty())
{
index = (index + 1) % SIZE;
comparisons++;
}
return make_pair(table[index].second, comparisons);
}
pair<string, int> searchQuadratic(string key)
{
int index = getHash(key);
int i = 0;
int comparisons = 0;
while (table[index].first != key && !table[index].first.empty())
{
i++;
index = (index + i * i) % SIZE;
comparisons++;
}
return make_pair(table[index].second, comparisons);
}
void remove(string key)
{
int index = getHash(key);
while (table[index].first != key && !table[index].first.empty())
{
index = (index + 1) % SIZE;
}
table[index].first = "";
table[index].second = "";
}
void display()
{
for (int i = 0; i < SIZE; i++)
{
if (!table[i].first.empty())
{
cout << "Index: " << i << ", Key: " << table[i].first << ", Value: " << table[i].second << endl;
}
}
}
};
int main()
{
HashTable ht;
int type = 0, ch = 0;
do
{
cout << "1. Insert" << endl;
cout << "2. Search" << endl;
cout << "3. Display" << endl;
cout << "4. Remove" << endl;
cout << "5. Exit" << endl;
cin >> ch;
switch (ch)
{
case 1:
{
int n = 0;
cout << "Choose any of the following \n 1. Linear Probing \n 2. Quadratic Probing" << endl;
cin >> type;
cout << "Enter the number of elements : ";
cin >> n;
if (n > SIZE)
{
cout << "Overflow" << endl;
break;
}
while (n--)
{
cout << "Enter key and value: ";
string key, val;
cin >> key >> val;
if (type == 1)
{
ht.insertLinear(key, val);
}
else if (type == 2)
{
ht.insertQuadratic(key, val);
}
else
cout << "Wrong input" << endl;
}
break;
}
case 2:
{
if (type == 1)
{
string key;
cout << "Enter an Element to be searched (Linear): ";
cin >> key;
cout << "Searching..." << endl;
if (ht.searchLinear(key).first == "")
{
cout << "Value: NA" << endl;
cout << "No of Comparisions: " << ht.searchLinear(key).second << endl;
}
else
{
cout << "Value: " << ht.searchLinear(key).first << endl;
cout << "No of Comparisions: " << ht.searchLinear(key).second << endl;
}
}
else if (type == 2)
{
string key;
cout << "Enter an Element to be searched (Quadratic): ";
cin >> key;
cout << "Searching..." << endl;
if (ht.searchQuadratic(key).first == "")
{
cout << "Value: NA" << endl;
cout << "No of Comparisions: " << ht.searchQuadratic(key).second << endl;
}
else
{
cout << "Value: " << ht.searchQuadratic(key).first << endl;
cout << "No of Comparisions: " << ht.searchQuadratic(key).second << endl;
}
}
else
{
cout << "Invalid Type" << endl;
}
break;
}
case 3:
ht.display();
break;
case 4:
{
string el;
cout << "Enter element to be removed: ";
cin >> el;
ht.remove(el);
break;
}
}
} while (ch != 5);
return 0;
}
/*
---------------------------------------------------------------------------------------------------
Output
---------------------------------------------------------------------------------------------------
1. Insert
2. Search
3. Display
4. Remove
5. Exit
1
Choose any of the following
1. Linear Probing
2. Quadratic Probing
1
Enter the number of elements : 6
Enter key and value: Joshua 339-191-391
Enter key and value: Jose 398-191-198
Enter key and value: Homer 938-938-398
Enter key and value: Tommy 999-181-598
Enter key and value: Trevor 983-193-118
Enter key and value: James 410-198-398
1. Insert
2. Search
3. Display
4. Remove
5. Exit
3
Index: 0, Key: Trevor, Value: 983-193-118
Index: 1, Key: Tommy, Value: 999-181-598
Index: 2, Key: Joshua, Value: 339-191-391
Index: 5, Key: Jose, Value: 398-191-198
Index: 6, Key: Homer, Value: 938-938-398
Index: 9, Key: James, Value: 410-198-398
1. Insert
2. Search
3. Display
4. Remove
5. Exit
2
Enter an Element to be searched (Linear): James
Searching...
Value: 410-198-398
No of Comparisions: 0
1. Insert
2. Search
3. Display
4. Remove
5. Exit
Enter an Element to be searched (Linear): Lama
Searching...
Value: NA
No of Comparisions: 2
1. Insert
2. Search
3. Display
4. Remove
5. Exit
4
Enter element to be removed: Joshua
1. Insert
2. Search
3. Display
4. Remove
5. Exit
3
Index: 0, Key: Trevor, Value: 983-193-118
Index: 1, Key: Tommy, Value: 999-181-598
Index: 5, Key: Jose, Value: 398-191-198
Index: 6, Key: Homer, Value: 938-938-398
Index: 9, Key: James, Value: 410-198-398
1. Insert
2. Search
3. Display
4. Remove
5. Exit
5
*/