-
Notifications
You must be signed in to change notification settings - Fork 0
/
practicalTwo.cpp
226 lines (194 loc) · 4.34 KB
/
practicalTwo.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
/*
Implement all the functions of a dictionary (ADT) using hashing and handle collisions
using chaining with / without replacement.
Data: Set of (key, value) pairs, Keys are mapped to values, Keys must be comparable, Keys
must be unique
Standard Operations: Insert(key, value), Find(key), Delete(key) */
#include <iostream>
#include <list>
#include <string>
#define SIZE 10
using namespace std;
class HashTable {
private:
list<pair<string, string>>* table;
int getHash(string key) {
int hash = 0;
for (char c : key) {
hash += c;
}
return hash % SIZE;
}
public:
HashTable() {
table = new list<pair<string, string>>[SIZE];
}
void insert(string key, string value) {
int index = getHash(key);
table[index].push_back(make_pair(key, value));
}
string search(string key) {
int index = getHash(key);
for (auto it = table[index].begin(); it != table[index].end(); it++) {
if (it->first == key) {
return it->second;
}
}
return "";
}
void remove(string key) {
int index = getHash(key);
for (auto it = table[index].begin(); it != table[index].end(); it++) {
if (it->first == key) {
table[index].erase(it);
break;
}
}
}
void display() {
for (int i = 0; i < SIZE; i++) {
cout << "Index: " << i << endl;
for (auto it = table[i].begin(); it != table[i].end(); it++) {
cout << "Key: " << it->first << ", Value: " << it->second << endl;
}
}
}
};
int main() {
HashTable ht;
string key, value;
int choice;
do {
cout << "Menu:\n";
cout << "1. Insert\n";
cout << "2. Search\n";
cout << "3. Remove\n";
cout << "4. Display\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter key: ";
cin >> key;
cout << "Enter value: ";
cin >> value;
if(ht.search(key) != "") {
cout << "Value already Present" << endl;
}else {
ht.insert(key, value);
}
break;
case 2:
cout << "Enter key: ";
cin >> key;
value = ht.search(key);
if (value.empty()) {
cout << "Key not found\n";
} else {
cout << "Value: " << value << endl;
}
break;
case 3:
cout << "Enter key: ";
cin >> key;
ht.remove(key);
break;
case 4:
ht.display();
break;
case 5:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice\n";
}
} while (choice != 5);
return 0;
}
/*
---------------------------------------------------------------------------------------------------
Output
---------------------------------------------------------------------------------------------------
Menu:
1. Insert
2. Search
3. Remove
4. Display
5. Exit
Enter your choice: 1
Enter key: word
Enter value: a_element_of_speech
Menu:
1. Insert
2. Search
3. Remove
4. Display
5. Exit
Enter your choice: 1
Enter key: hello
Enter value: used_for_greeting
Menu:
1. Insert
2. Search
3. Remove
4. Display
5. Exit
Enter your choice: 1
Enter key: car
Enter value: a_four_wheeled_vehical
Menu:
1. Insert
2. Search
3. Remove
4. Display
5. Exit
Enter your choice: 4
Index: 0
Key: car, Value: a_four_wheeled_vehical
Index: 1
Index: 2
Key: hello, Value: used_for_greeting
Index: 3
Index: 4
Key: word, Value: a_element_of_speech
Index: 5
Index: 6
Index: 7
Index: 8
Index: 9
Menu:
1. Insert
2. Search
3. Remove
4. Display
5. Exit
Enter your choice: 2
Enter key: car
Value: a_four_wheeled_vehical
Menu:
1. Insert
2. Search
3. Remove
4. Display
5. Exit
Enter your choice: 3
Enter key: car
Menu:
1. Insert
2. Search
3. Remove
4. Display
5. Exit
Enter your choice: 2
Enter key: car
Key not found
Menu:
1. Insert
2. Search
3. Remove
4. Display
5. Exit
Enter your choice: 5
Exiting...
*/