-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashTable.cpp
More file actions
182 lines (171 loc) · 3.92 KB
/
hashTable.cpp
File metadata and controls
182 lines (171 loc) · 3.92 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Hash Table
// 1. Hash Table is a data structure that stores data in an associative manner.
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <numeric>
#include <cmath>
#include <map>
#include <unordered_map>
#include <set>
using namespace std;
class HashTable
{
private:
static const int hashGroups = 10;
list<pair<int, string>> table[hashGroups];
public:
bool isEmpty() const;
int hashFunction(int key);
void insertItem(int key, string value);
void removeItem(int key);
string searchTable(int key);
void printTable();
};
bool HashTable::isEmpty() const
{
int sum{};
for (int i = 0; i < hashGroups; i++)
{
sum += table[i].size();
}
if (!sum)
{
return true;
}
return false;
}
int HashTable::hashFunction(int key)
{
return key % hashGroups;
}
void HashTable::insertItem(int key, string value)
{
int hashValue = hashFunction(key);
auto &cell = table[hashValue];
auto bItr = begin(cell);
bool keyExists = false;
for (; bItr != end(cell); bItr++)
{
if (bItr->first == key)
{
keyExists = true;
bItr->second = value;
cout << "Key exists. Value replaced" << endl;
break;
}
}
if (!keyExists)
{
cell.emplace_back(key, value);
}
return;
}
void HashTable::removeItem(int key)
{
int hashValue = hashFunction(key);
auto &cell = table[hashValue];
auto bItr = begin(cell);
bool keyExists = false;
for (; bItr != end(cell); bItr++)
{
if (bItr->first == key)
{
keyExists = true;
bItr = cell.erase(bItr);
cout << "Key removed" << endl;
break;
}
}
if (!keyExists)
{
cout << "Key not found" << endl;
}
return;
}
string HashTable::searchTable(int key)
{
int hashValue = hashFunction(key);
auto &cell = table[hashValue];
auto bItr = begin(cell);
bool keyExists = false;
for (; bItr != end(cell); bItr++)
{
if (bItr->first == key)
{
keyExists = true;
return bItr->second;
}
}
if (!keyExists)
{
return "No value found";
}
};
void HashTable::printTable()
{
for (int i = 0; i < hashGroups; i++)
{
if (table[i].size() == 0)
{
continue;
}
auto bItr = table[i].begin();
for (; bItr != table[i].end(); bItr++)
{
cout << "Key: " << bItr->first << " Value: " << bItr->second << endl;
}
}
}
int main()
{
HashTable HT;
int key{};
string value{};
int choice{};
do
{
cout << "1. Insert item into the table" << endl;
cout << "2. Remove item from the table" << endl;
cout << "3. Search the table" << endl;
cout << "4. Print the table" << endl;
cout << "5. Clear the screen" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter key and value to be inserted: ";
cin >> key >> value;
HT.insertItem(key, value);
break;
case 2:
cout << "Enter key of the item to be deleted: ";
cin >> key;
HT.removeItem(key);
break;
case 3:
cout << "Enter key of the item to be searched: ";
cin >> key;
cout << "Value at key " << key << " is " << HT.searchTable(key) << endl;
break;
case 4:
HT.printTable();
break;
case 5:
system("cls");
break;
case 6:
cout << "Exiting..." << endl;
break;
default:
cout << "Enter proper choice" << endl;
}
} while (choice != 6);
return 0;
}