-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.cpp
More file actions
94 lines (72 loc) · 1.92 KB
/
Copy pathdev.cpp
File metadata and controls
94 lines (72 loc) · 1.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
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <map>
using namespace std;
class SimpleHashTable {
private:
static const int TABLE_SIZE = 100;
vector<list<pair<int, string>>>table;
int hashFunction(int key) {
return key % TABLE_SIZE;
}
public:
SimpleHashTable(): table(TABLE_SIZE) {}
void insert(int key, const string& value) {
int index = hashFunction(key);
for (auto& p : table[index]) {
if (p.first == key) {
p.second = value;
return;
}
}
table[index].push_back({key, value});
}
string get(int key) {
int index = hashFunction(key);
for (const auto& p : table[index]) {
if (p.first == key) {
return p.second;
}
}
return "Key not found";
}
bool remove(int key) {
int index = hashFunction(key);
for (auto it = table[index].begin(); it != table[index].end(); ++it) {
if (it->first == key) {
table[index].erase(it);
return true;
}
}
return false;
}
bool contains(int key) {
int index = hashFunction(key);
for (const auto& p : table[index]) {
if (p.first == key) {
return true;
}
}
return false;
}
};
int main() {
switch (1)
{
case 1:
/* code */
break;
default:
break;
}
// SimpleHashTable s;
// s.insert(1, "Akrom");
// s.insert(101, "Collision");
// cout << s.get(1) << endl;
// cout << s.get(101) << endl;
// s.remove(1);
// cout << s.contains(1) << endl;
return 0;
}