-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubleHasting.cpp
91 lines (79 loc) · 2.14 KB
/
DoubleHasting.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
#include <iostream>
#include <vector>
class DoubleHastingHashTable {
private:
std::vector<int> table;
int table_size;
int num_elements;
const int EMPTY = -1;
const int DELETED = -2;
int hash1(int key){
return key % table_size;
}
int hash2(int key){
return 7 - (key % 7);
}
public:
DoubleHastingHashTable (int size) : table_size(size), num_elements(0) {
table.resize(size, EMPTY);
}
bool insert(int key) {
if (num_elements >= table_size) {
std::cerr << "Hash table is full!" << std::endl;
return false;
}
int h1 = hash1(key);
int h2 = hash2(key);
int i =0;
while (table[(h1 + i * h2) % table_size] != EMPTY &&
table[(h1 + i * h2) % table_size] != DELETED &&
i< table_size){
i++;
}
int index = (h1 + i * h2) % table_size;
if (table[index] == EMPTY || table[index] == DELETED) {
table[index] = key;
num_elements++;
return true;
}
return false;
}
bool search(int key) {
int h1 = hash1(key);
int h2 = hash2(key);
int i = 0;
while (table[(h1 + i * h2) % table_size] !=EMPTY && i << table_size) {
if (table[(h1 +i * h2) % table_size] == key) {
return true;
}
i++;
}
return false;
}
void display(){
for (int i = 0; i < table_size; i++){
if (table[i] == EMPTY){
std::cout << "EMPTY";
}else if (table[i] == DELETED){
std::cout << "DELETED";
}else {
std::cout << table[i] << " ";
}
}
std::cout << std::endl;
}
};
int main(){
DoubleHastingHashTable hashTable(11);
hashTable.insert(27);
hashTable.insert(18);
hashTable.insert(29);
hashTable.insert(28);
hashTable.insert(39);
hashTable.insert(13);
std::cout << " Hash table contents: ";
hashTable.display();
std::cout << "Seatching for 29: " << (hashTable.search(29) ? "Found" : "Not Found") << std::endl;
hashTable.display();
return 0;
}