-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTable.cpp
62 lines (52 loc) · 1.48 KB
/
HashTable.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
#ifndef HASHTABLE_CPP
#define HASHTABLE_CPP
#include "SList.cpp"
template<class Keys, class T>
class HashTable{
private:
int m;
SingleList<Keys, T> *table;
public:
HashTable() : m(0), table(nullptr) {}
HashTable(int n) : m(n), table(new SingleList<Keys, T>[m]) {}
~HashTable(){
delete[] table;
}
int getSize() {
return m;
}
SingleList<Keys, T> *getTable(){
return table;
}
Node<Keys, T>* Add(Keys key, T e, int (*hash)(Keys, int)) {
int h = hash(key, m);
Node<Keys, T>* p = table[h].getNode(key);
if (p == NULL) {
return table[h].pushback(key, e);
}
return NULL;
}
void Remove(Keys key, int (*hash)(Keys, int)) {
int h = hash(key, m);
Node<Keys, T>* p = table[h].getNode(key);
if (p != NULL) {
table[h].remove(p);
}
}
Node<Keys, T> *Find(Keys key, int (*hash)(Keys, int)){
int h = hash(key, m);
return table[h].getNode(key);
}
bool Contains(Keys key, int (*hash)(Keys, int)){
int h = hash(key, m);
return table[h].getNode(key) != NULL;
}
int Count(){
int t = 0;
for(int i = 0; i < m; i++){
t = t + table[i].size();
}
return t;
}
};
#endif