-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.cpp
More file actions
236 lines (155 loc) · 4.9 KB
/
Copy pathHashTable.cpp
File metadata and controls
236 lines (155 loc) · 4.9 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
File Name: HashTable.cpp
*/
#include <iostream>
#include "HashTable.h"
using namespace std;
HashTable::HashTable(int range) : table_size(range)
{
linearProbingCollision.resize(range, -1); //This will initialize linear probing table when range is -1 size.
chaining.resize(range); //This will Initialize chaining table when the range is considered an empty lists.
}
int HashTable::hash(int pair)
{
return pair % table_size; //This is the define of a hash function.
}
//This is a linear probing function to find an available index for insertion.
int HashTable::linearProbing(int key, int value, bool num)
{
int linearIndex = value; //This will put linear index for reference.
int probingCollisions = 0; //This can track the number of probing collisions attempts.
while (linearProbingCollision[value] != -1 && linearProbingCollision[value] != key)
{
value = (value + 1) % table_size;
probingCollisions++;
if (probingCollisions >= table_size)
{
return -1; //This returns the table as too many collisions.
}
if (value == linearIndex)
{
return -1; //This returns as too many collisions or full cycle.
}
}
return value;
}
//This is an insertion function of an element using linear probing strategy.
void HashTable::insertLinearProbing(int pair)
{
int value = hash(pair);
int insertIndex = linearProbing(pair, value, true);
if (insertIndex == -1)
{
cout << "The Insertion is rejected due to too many collisions." << endl;
}
else if (linearProbingCollision[insertIndex] == -1)
{
linearProbingCollision[insertIndex] = pair;
cout << pair << " has been inserted successfully!" << endl;
}
else
{
cout << "The insertion failed due to collision." << endl;
}
}
//This is a search function of an element using linear probing strategy.
void HashTable::searchLinearProbing(int pair)
{
int value = hash(pair);
int searchIndex = linearProbing(pair, value);
if (searchIndex != -1 && linearProbingCollision[searchIndex] == pair)
{
cout << "Element " << pair << " exists" << endl;
}
else
{
cout << "Element " << pair << " does not exist" << endl;
}
}
//This is a delete function of an element using linear probing strategy.
void HashTable::deleteLinearProbing(int pair)
{
int index = hash(pair);
int deleteIndex = linearProbing(pair, index);
if (deleteIndex != -1 && linearProbingCollision[deleteIndex] == pair)
{
linearProbingCollision[deleteIndex] = -1;
cout << "Element " << pair << " exists and was removed" << endl;
}
else
{
cout << "Element " << pair << " does not exist" << endl;
}
}
//This outputs the entire hash table of the linear probing strategy.
void HashTable::outputLinearProbing()
{
for (int i = 0; i < table_size; i++)
{
if (linearProbingCollision[i] != -1)
{
cout << "Index " << i << ": " << linearProbingCollision[i] << endl;
}
else
{
cout << "Index " << i << ": [empty]" << endl;
}
}
}
//This is an insertion function of an element using chaining strategy.
void HashTable::insertChaining(int pair)
{
int insertIndex = hash(pair);
chaining[insertIndex].push_back(pair);
cout << pair << " has been inserted successfully!" << endl;
}
//This is a sesarch function of an element using chaining strategy.
void HashTable::searchChaining(int pair)
{
int searchIndex = hash(pair);
for (int val : chaining[searchIndex])
{
if (val == pair)
{
cout << "Element " << pair << " exists" << endl;
return;
}
}
cout << "Element " << pair << " does not exist" << endl;
}
//This is a delete function of an element using chaining strategy.
void HashTable::deleteChaining(int pair)
{
int deleteIndex = hash(pair);
for (auto num = chaining[deleteIndex].begin(); num != chaining[deleteIndex].end(); ++num)
{
if (*num == pair)
{
chaining[deleteIndex].erase(num);
cout << "Element " << pair << " exists and was removed" << endl;
return;
}
}
cout << "Element " << pair << " does not exist" << endl;
}
//This outputs the entire hash table of the chaining strategy.
void HashTable::outputChaining()
{
for (int i = 0; i < table_size; i++)
{
cout << "Index " << i << ": ";
if (chaining[i].empty())
{
cout << "[empty]";
}
else
{
for (int val : chaining[i])
{
cout << val << " -> ";
}
cout << "NULL";
}
cout << endl;
}
}