-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTable.cc
206 lines (178 loc) · 4.54 KB
/
HashTable.cc
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <math.h>
#include <utility>
#include "head/HashTable.h"
using namespace std;
int hashsize[]={11, 29, 59, 113, 223, 489, 997, 1499, 2999, 4999, 9973, 19997, 40009, 104393, 113381, 120011, 130003, 140009, 150001, 160001, 170003, 180001, 190027, 200003, 210011, 220009, 230003, 240007, 250007, 260003, 270001, 280001, 290011, 300007, 717419, 1253621, 1930801, 3125051, 4351433, 6866927, 8150399, 9458311};
HashTable::HashTable()
{}
int HashTable::Init_HashTable()
{
int i;
count = 0;
size = 0;
c = 0;
Hash_length = hashsize[0];
elem = new Elemtype[Hash_length];
if(!elem)
{
cout << "malloc fails when initialize hash table!"<<endl;
exit(1);
}
for(i = 0; i < Hash_length; i++)
elem[i] = NULLKEY;
return 1;
}
void HashTable::Destroy_HashTable()
{
delete []elem;
elem = NULL;
count = 0;
size = 0;
}
unsigned HashTable::Hash(KeyType k)
{
return k % Hash_length;
}
void HashTable::Collision(int &p,int d)
{
// cout<<"In collision, p = "<<p<<", d = "<<d<<", Hash_length = "<<Hash_length<<endl;
p = (int)((p + pow(d,2))) % Hash_length;
// p = (int)((p + d)) % Hash_length;
// cout<<"In collision, p = "<<p<<endl;
}
bool HashTable::Search_Hash(KeyType k,int &p)
{
//cout << "coming into search hash !"<<endl;
c=0;
p=Hash(k);
// cout <<"hash length = "<<Hash_length<<", In search, p = "<<p;
// cout << ", elem["<<p<<"] = "<<elem[p]<<endl;
while(elem[p]!=NULLKEY && elem[p]!=k) //elem[p] is occupied by other element.
{
c++;
p = Hash(k);
if(c < Hash_length)
{
Collision(p,c);
if( p < 0 )
return 0;
}
else
return 0;
}
if(elem[p]==k)
return 1;
else
return 0;
}
int HashTable::Insert_Hash(Elemtype e)
{
int p;
if(Search_Hash(e,p) == 1) //The element has been in the table.
return -1;
else if(c<hashsize[size]/2)
{
int i = 0;
while(i < Hash_length)
{
p = Hash(e);
Collision(p,i);
if(elem[p] == NULLKEY || elem[p] == DELKEY)
{
elem[p] = e;
count++;
return 1;
}
else i++;
}
}
else
{
ReCreate_HashTable();
return 0;
}
}
int HashTable::Get_Data(int p)
{
return elem[p];
//cout<<"number:"<<elem[p]<<endl;
}
void HashTable::Del_Data(int p)
{
if(elem[p] != NULLKEY)
elem[p] = DELKEY;
}
void HashTable::ReCreate_HashTable()
{
int i, count2 = count;
Elemtype *p, *elem2 = new Elemtype[count];
p = elem2;
for(i=0;i<Hash_length;i++)
if(elem[i]!=NULLKEY)
*p++=*(elem+i);
count=0;
size++;
Hash_length=hashsize[size];
p=new Elemtype[Hash_length];
if(!p)
{
cout<<""<<endl;
exit(0);
}
delete []elem;
elem=p;
for(i=0;i<Hash_length;i++)
elem[i]=NULLKEY;
for(p=elem2;p<elem2+count2;p++)
int result = Insert_Hash(*p);
delete []elem2;
}
void HashTable::Traverse_HashTable()
{
// cout << "Traverse the hashtable: "<<Hash_length<<endl;
for(int i=0;i<Hash_length;i++)
{
if(elem[i] == NULLKEY || elem[i] == DELKEY)
continue;
//cout<<i<<","<<NULLKEY<<endl;
else if(elem[i]!=NULLKEY)
cout <<elem[i]<<" ";
}
cout << endl;
}
int HashTable::Get_Size()
{
//int size = 0;
//for(int i = 0; i < Hash_length; i++)
// if(elem[i] != NULLKEY)
// size++;
return Hash_length;
}
int HashTable::Data_Size()
{
int data_size = 0;
for(int i = 0; i < Hash_length; i++)
if(elem[i] != NULLKEY && elem[i] != DELKEY)
data_size++;
return data_size;
}
bool HashTable::empty()
{
int size = 0;
for(int i = 0; i < Hash_length; i++)
if(elem[i] != NULLKEY && elem[i] != DELKEY)
size++;
if(size == 0)
return 1;
else
return 0;
}