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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
#include <iostream>
using
namespace
std;
const
int
sz = 5;
struct
data{
int
id;
int
val;
};
class
Hashtable{
data dt[sz];
int
numel;
public
:
Hashtable();
int
hash(
int
&id);
int
rehash(
int
&id);
int
insert(data &d);
int
remove
(data &d);
int
retrieve(
int
&id);
void
output();
};
Hashtable::Hashtable(){
for
(
int
i=0;i<sz;i++){
dt[i].id = -1;
dt[i].val = 0;
}
numel = 0;
}
int
Hashtable::hash(
int
&id){
return
id%sz;
}
int
Hashtable::rehash(
int
&id){
return
(id+1)%sz;
}
int
Hashtable::insert(data &d){
if
(numel<sz){
int
hashid = hash(d.id);
if
(hashid>=0 && hashid < sz){
if
(dt[hashid].id==-1 || dt[hashid].id==-2){
dt[hashid].id = d.id;
dt[hashid].val = d.val;
numel++;
return
0;
}
else
{
cout <<
"collision! rehashing..."
<<endl;
int
i=0;
while
(i<sz){
hashid = rehash(hashid);
if
(dt[hashid].id==-1 || dt[hashid].id==-2){
dt[hashid].id = d.id;
dt[hashid].val = d.val;
numel++;
return
0;
}
if
(i==sz){
return
-1;}
i++;
}
}
}
}
else
{
return
-1;}
}
int
Hashtable::
remove
(data &d){
int
hashid = hash(d.id);
if
(hashid>=0 && hashid < sz){
if
(dt[hashid].id==d.id){
dt[hashid].id = -2;
dt[hashid].val = 0;
numel--;
return
0;
}
else
{
int
i=0;
while
(i<sz){
hashid = rehash(hashid);
if
(dt[hashid].id==d.id){
dt[hashid].id = -2;
dt[hashid].val = 0;
numel--;
return
0;
}
if
(i==sz){
return
-1;}
i++;
}
}
}
}
int
Hashtable::retrieve(
int
&id){
int
hashid = hash(id);
if
(hashid>=0 && hashid < sz){
if
(dt[hashid].id==id){
return
dt[hashid].val;
}
else
{
int
i=0;
while
(i<sz){
hashid = rehash(hashid);
if
(dt[hashid].id==id){
return
dt[hashid].val;
}
if
(i==sz){
return
0;}
i++;
}
}
}
}
void
Hashtable::output(){
cout <<
"idx id val"
<< endl;
for
(
int
i=0;i<sz;i++){
cout << i <<
" "
<< dt[i].id <<
" "
<< dt[i].val << endl;
}
}
int
main(){
Hashtable hashtable;
data d;
d.id = 27;
d.val = 27;
hashtable.insert(d);
hashtable.output();
d.id = 99;
d.val = 99;
hashtable.insert(d);
hashtable.output();
d.id = 32;
d.val = 32;
hashtable.insert(d);
hashtable.output();
d.id = 77;
d.val = 77;
hashtable.insert(d);
hashtable.output();
//retrieve data
int
id = 77;
int
val = hashtable.retrieve(id);
cout << endl;
cout <<
"Retrieving ... "
<< endl;
cout <<
"hashtable["
<< id<<
"]="
<< val << endl;
cout << endl;
//delete element
d.id = 32;
d.val = 32;
hashtable.
remove
(d);
hashtable.output();
d.id = 77;
d.val = 77;
hashtable.
remove
(d);
hashtable.output();
return
0;
}
|
原文地址如下:
http://yucoding.blogspot.com/2013/08/re-viewhash-table-basics.html