-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch11.cpp
98 lines (76 loc) · 1.68 KB
/
ch11.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
92
93
94
95
96
97
98
// clrs p270 - ch11 - hashing by open addressing (linear probing)
#include <iostream>
class Solution {
public:
Solution(int n) {
length = n;
table = new SLOT [n];
for(int i = 0; i < n; i++) {
table[i].stat = INVALID;
}
}
~Solution() {
delete [] table;
}
int HashInsert(int key) {
int h_prime = key % length; // auxiliary hash function
for(int i = 0; i < length; i++) {
int h = (i + h_prime) % length; // hash function - linear probing
if(table[h].stat == INVALID || table[h].stat == DELETED) {
table[h].key = key;
table[h].stat = OCCUPIED;
return h; // hash function is also the index of hash table
}
}
return -1;
}
int HashSearch(int key) {
int h_prime = key % length;
for(int i = 0; i < length; i++) {
int h = (i + h_prime) % length;
if(table[h].stat == OCCUPIED && table[h].key == key) {
return h;
}
if(table[h].stat == INVALID || table[h].stat == DELETED) {
return -1;
}
}
return -1;
}
bool HashDelete(int key) {
int h_prime = key % length;
for(int i = 0; i < length; i++) {
int h = (i + h_prime) % length;
if(table[h].stat == OCCUPIED && table[h].key == key) {
table[h].stat = DELETED;
return true;
}
if(table[h].stat == INVALID || table[h].stat == DELETED)
return false;
}
return false;
}
private:
enum STATUS {
INVALID,
DELETED,
OCCUPIED,
};
struct SLOT {
int key;
STATUS stat;
};
SLOT* table;
int length;
};
int main() {
Solution sol(10);
int index1, index2, index3, index4, index5;
index1 = sol.HashInsert(5);
index2 = sol.HashInsert(0);
index3 = sol.HashInsert(33);
index4 = sol.HashSearch(5);
sol.HashDelete(5);
index5 = sol.HashSearch(5);
return 0;
}