-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhash_table.cpp
72 lines (61 loc) · 1.55 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
#include <iostream>
#include <vector>
using namespace std;
int TABLE_SIZE;
struct HashElement{
string name, place;
};
class HashTable{
private:
vector<HashElement> *map;
int hashFunction(string key){
int sum = 0;
for(int i = 0; i < key.length(); i++)
sum = sum + int(key[i]);
return sum % TABLE_SIZE;
}
public:
HashTable(){
map = new vector<HashElement>[TABLE_SIZE];
}
void insert(string name, string place){
HashElement new_el;
new_el.name = name;
new_el.place = place;
int i = hashFunction(name);
map[i].push_back(new_el);
}
void search(string name){
int i = hashFunction(name);
bool found = false;
for(int j = 0; j < map[i].size(); j++){
if(map[i][j].name == name){
found = true;
cout << map[i][j].place << endl;
break;
}
}
if(!found)
cout << "-" << endl;
}
};
int main() {
int operations_num;
char operation_type;
string name, place;
cin >> operations_num;
TABLE_SIZE = operations_num;
HashTable h_table;
for(int i = 0; i < operations_num; i++){
cin >> operation_type;
if(operation_type == 'A'){
cin >> name >> place;
h_table.insert(name, place);
}
else if(operation_type == 'S'){
cin >> name;
h_table.search(name);
}
}
return 0;
}