This repository has been archived by the owner on Dec 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhash_table_driver.cpp
327 lines (310 loc) · 14.6 KB
/
hash_table_driver.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/**
* file: hash_table_driver.cpp
* type: C++ (source file)
* date: 08_JULY_2023
* author: karbytes
* license: PUBLIC_DOMAIN
*/
#include "hash_table.h" // Include the C++ header file which contains preprocessing directives, variable declarations, and function prototypes for the HASH_TABLE class.
/* function prototypes */
void unit_test_0(std::ostream & output);
void unit_test_1(std::ostream & output);
void unit_test_2(std::ostream & output);
void unit_test_3(std::ostream & output);
void unit_test_4(std::ostream & output);
void unit_test_5(std::ostream & output);
/**
* Unit Test # 0: HASH_TABLE constructor, print method, and destructor.
*/
void unit_test_0(std::ostream & output)
{
output << "\n\n************************************************";
output << "\nUnit Test # 0: HASH_TABLE constructor, print method, and destructor.";
output << "\n************************************************";
output << "\nHASH_TABLE hash_table;";
output << "\nhash_table.print(output);";
HASH_TABLE hash_table;
hash_table.print(output);
}
/**
* Unit Test # 1: HASH_TABLE constructor, insert method, print method, and destructor.
*/
void unit_test_1(std::ostream & output)
{
output << "\n\n************************************************";
output << "\nUnit Test # 1: HASH_TABLE constructor, insert method, print method, and destructor.";
output << "\n************************************************";
output << "\nHASH_TABLE hash_table;";
output << "\nNODE node;";
output << "\nnode.key = \"unit_test_1\";";
output << "\nnode.next = NULL;";
output << "\nhash_table.insert_node(&node);";
output << "\nhash_table.print(output);";
HASH_TABLE hash_table;
NODE node;
node.key = "unit_test_1";
node.next = NULL;
hash_table.insert_node(&node);
hash_table.print(output);
}
/**
* Unit Test # 2: HASH_TABLE constructor, insert method, print method, and destructor.
*/
void unit_test_2(std::ostream & output)
{
output << "\n\n************************************************";
output << "\nUnit Test # 2: HASH_TABLE constructor, insert method, print method, and destructor.";
output << "\n************************************************";
output << "\nHASH_TABLE hash_table;";
output << "\nNODE node_A = { key : \"node_A\", next : NULL };";
output << "\nNODE node_B = { key : \"node_B\", next : NULL };";
output << "\nNODE node_C = { key : \"node_C\", next : NULL };";
output << "\nhash_table.insert_node(&node_A);";
output << "\nhash_table.insert_node(&node_B);";
output << "\nhash_table.insert_node(&node_C);";
output << "\noutput << hash_table; // functionally identical to hash_table.print(output)";
HASH_TABLE hash_table;
NODE node_A = { key : "node_A", next : NULL };
NODE node_B = { key : "node_B", next : NULL };
NODE node_C = { key : "node_C", next : NULL };
hash_table.insert_node(&node_A);
hash_table.insert_node(&node_B);
hash_table.insert_node(&node_C);
output << hash_table; // functionally identical to hash_table.print(output);
}
/**
* Unit Test # 3: HASH_TABLE constructor, insert method, number of linked lists method, number of node method, print method, and destructor.
*/
void unit_test_3(std::ostream & output)
{
output << "\n\n************************************************";
output << "\nUnit Test # 3: HASH_TABLE constructor, insert method, number of linked lists method, number of node method, print method, and destructor.";
output << "\n************************************************";
output << "\nHASH_TABLE hash_table = HASH_TABLE(5);";
output << "\nNODE node_A = { key : \"node_A\", next : NULL };";
output << "\nNODE node_B = { key : \"node_B\", next : NULL };";
output << "\nNODE node_C = { key : \"node_C\", next : NULL };";
output << "\nNODE node_AA = { key : \"node_AA\", next : NULL };";
output << "\nNODE node_BB = { key : \"node_BB\", next : NULL };";
output << "\nNODE node_CC = { key : \"node_CC\", next : NULL };";
output << "\nNODE node_Z = { key : \"node_Z\", next : NULL };";
output << "\nNODE node_666 = { key : \"node_666\", next : NULL };";
output << "\nhash_table.insert_node(&node_A);";
output << "\nhash_table.insert_node(&node_B);";
output << "\nhash_table.insert_node(&node_C);";
output << "\nhash_table.insert_node(&node_AA);";
output << "\nhash_table.insert_node(&node_BB);";
output << "\nhash_table.insert_node(&node_CC);";
output << "\nhash_table.insert_node(&node_Z);";
output << "\nhash_table.insert_node(&node_666);";
output << "\noutput << hash_table; // functionally identical to hash_table.print(output)";
HASH_TABLE hash_table = HASH_TABLE(5);
NODE node_A = { key : "node_A", next : NULL };
NODE node_B = { key : "node_B", next : NULL };
NODE node_C = { key : "node_C", next : NULL };
NODE node_AA = { key : "node_AA", next : NULL };
NODE node_BB = { key : "node_BB", next : NULL };
NODE node_CC = { key : "node_CC", next : NULL };
NODE node_Z = { key : "node_Z", next : NULL };
NODE node_666 = { key : "node_666", next : NULL };
hash_table.insert_node(&node_A);
hash_table.insert_node(&node_B);
hash_table.insert_node(&node_C);
hash_table.insert_node(&node_AA);
hash_table.insert_node(&node_BB);
hash_table.insert_node(&node_CC);
hash_table.insert_node(&node_Z);
hash_table.insert_node(&node_666);
output << "\nhash_table.get_number_of_linked_lists_in_hash_table() = " << hash_table.get_number_of_linked_lists_in_hash_table() << ".";
output << "\nhash_table.get_number_of_nodes_in_hash_table()= " << hash_table.get_number_of_nodes_in_hash_table() << ".";
output << hash_table; // functionally identical to hash_table.print(output);
}
/**
* Unit Test # 4: HASH_TABLE constructor, insert method, remove method, print method, and destructor.
*/
void unit_test_4(std::ostream & output)
{
output << "\n\n************************************************";
output << "\nUnit Test # 4: HASH_TABLE constructor, insert method, remove method, print method, and destructor.";
output << "\n************************************************";
output << "\nHASH_TABLE hash_table;";
output << "\nNODE node_0 = { key : \"XXX\", next : NULL };";
output << "\nNODE node_1 = { key : \"YYY\", next : NULL };";
output << "\nNODE node_2 = { key : \"ZZZ\", next : NULL };";
output << "\nNODE node_3 = { key : \"XXX\", next : NULL };";
output << "\nNODE node_4 = { key : \"YYY\", next : NULL };";
output << "\nNODE node_5 = { key : \"ZZZ\", next : NULL };";
output << "\nNODE node_6 = { key : \"XXX\", next : NULL };";
output << "\nNODE node_7 = { key : \"YYY\", next : NULL };";
output << "\nNODE node_8 = { key : \"ZZZ\", next : NULL };";
output << "\nNODE node_9 = { key : \"XXX\", next : NULL };";
output << "\nNODE node_10 = { key : \"YYY\", next : NULL };";
output << "\nNODE node_11 = { key : \"ZZZ\", next : NULL };";
output << "\nhash_table.insert_node(&node_0);";
output << "\nhash_table.insert_node(&node_1);";
output << "\nhash_table.insert_node(&node_2);";
output << "\nhash_table.insert_node(&node_3);";
output << "\nhash_table.insert_node(&node_4);";
output << "\nhash_table.insert_node(&node_5);";
output << "\nhash_table.insert_node(&node_6);";
output << "\nhash_table.insert_node(&node_7);";
output << "\nhash_table.insert_node(&node_8);";
output << "\nhash_table.insert_node(&node_9);";
output << "\nhash_table.insert_node(&node_10);";
output << "\nhash_table.insert_node(&node_11);";
output << "\nhash_table.print(output);";
output << "\nhash_table.remove_nodes_with_key(\"XXX\");";
output << "\nhash_table.print(output);";
HASH_TABLE hash_table;
NODE node_0 = { key : "XXX", next : NULL };
NODE node_1 = { key : "YYY", next : NULL };
NODE node_2 = { key : "ZZZ", next : NULL };
NODE node_3 = { key : "XXX", next : NULL };
NODE node_4 = { key : "YYY", next : NULL };
NODE node_5 = { key : "ZZZ", next : NULL };
NODE node_6 = { key : "XXX", next : NULL };
NODE node_7 = { key : "YYY", next : NULL };
NODE node_8 = { key : "ZZZ", next : NULL };
NODE node_9 = { key : "XXX", next : NULL };
NODE node_10 = { key : "YYY", next : NULL };
NODE node_11 = { key : "ZZZ", next : NULL };
hash_table.insert_node(&node_0);
hash_table.insert_node(&node_1);
hash_table.insert_node(&node_2);
hash_table.insert_node(&node_3);
hash_table.insert_node(&node_4);
hash_table.insert_node(&node_5);
hash_table.insert_node(&node_6);
hash_table.insert_node(&node_7);
hash_table.insert_node(&node_8);
hash_table.insert_node(&node_9);
hash_table.insert_node(&node_10);
hash_table.insert_node(&node_11);
hash_table.print(output);
hash_table.remove_nodes_with_key("XXX");
hash_table.print(output);
}
/**
* HASH_TABLE constructor, insert method, get nodes with key method, print method, and destructor.
*/
void unit_test_5(std::ostream & output)
{
output << "\n\n************************************************";
output << "\nUnit Test # 5: HASH_TABLE constructor, insert method, get nodes with key method, print method, and destructor.";
output << "\n************************************************";
output << "\nHASH_TABLE hash_table = HASH_TABLE(6);";
output << "\nNODE node_0 = { key : \"AAAA\", next : NULL };";
output << "\nNODE node_1 = { key : \"ABAB\", next : NULL };";
output << "\nNODE node_2 = { key : \"AABB\", next : NULL };";
output << "\nNODE node_3 = { key : \"CCCC\", next : NULL };";
output << "\nNODE node_4 = { key : \"ABAB\", next : NULL };";
output << "\nNODE node_5 = { key : \"CCCC\", next : NULL };";
output << "\nNODE node_6 = { key : \"BBBB\", next : NULL };";
output << "\nNODE node_7 = { key : \"ABAB\", next : NULL };";
output << "\nNODE node_8 = { key : \"AAAA\", next : NULL };";
output << "\nNODE node_9 = { key : \"CCCC\", next : NULL };";
output << "\nNODE node_10 = { key : \"DDDD\", next : NULL };";
output << "\nNODE node_11 = { key : \"AABB\", next : NULL };";
output << "\nNODE node_12 = { key : \"EEEE\", next : NULL };";
output << "\nNODE node_13 = { key : \"DDDD\", next : NULL };";
output << "\nNODE node_14 = { key : \"ABAB\", next : NULL };";
output << "\nhash_table.insert_node(&node_0);";
output << "\nhash_table.insert_node(&node_1);";
output << "\nhash_table.insert_node(&node_2);";
output << "\nhash_table.insert_node(&node_3);";
output << "\nhash_table.insert_node(&node_4);";
output << "\nhash_table.insert_node(&node_5);";
output << "\nhash_table.insert_node(&node_6);";
output << "\nhash_table.insert_node(&node_7);";
output << "\nhash_table.insert_node(&node_8);";
output << "\nhash_table.insert_node(&node_9);";
output << "\nhash_table.insert_node(&node_10);";
output << "\nhash_table.insert_node(&node_11);";
output << "\nhash_table.insert_node(&node_12);";
output << "\nhash_table.insert_node(&node_13);";
output << "\nhash_table.insert_node(&node_14);";
output << "\noutput << hash_table; // functionally identical to hash_table.print(output)";
output << "\nLINKED_LIST search_results = hash_table.get_nodes_with_key(\"AAAA\");";
output << "\noutput << search_results; // functionally identical to search_results.print(output);";
HASH_TABLE hash_table = HASH_TABLE(6);
NODE node_0 = { key : "AAAA", next : NULL };
NODE node_1 = { key : "ABAB", next : NULL };
NODE node_2 = { key : "AABB", next : NULL };
NODE node_3 = { key : "CCCC", next : NULL };
NODE node_4 = { key : "ABAB", next : NULL };
NODE node_5 = { key : "CCCC", next : NULL };
NODE node_6 = { key : "BBBB", next : NULL };
NODE node_7 = { key : "ABAB", next : NULL };
NODE node_8 = { key : "AAAA", next : NULL };
NODE node_9 = { key : "CCCC", next : NULL };
NODE node_10 = { key : "DDDD", next : NULL };
NODE node_11 = { key : "AABB", next : NULL };
NODE node_12 = { key : "EEEE", next : NULL };
NODE node_13 = { key : "DDDD", next : NULL };
NODE node_14 = { key : "ABAB", next : NULL };
hash_table.insert_node(&node_0);
hash_table.insert_node(&node_1);
hash_table.insert_node(&node_2);
hash_table.insert_node(&node_3);
hash_table.insert_node(&node_4);
hash_table.insert_node(&node_5);
hash_table.insert_node(&node_6);
hash_table.insert_node(&node_7);
hash_table.insert_node(&node_8);
hash_table.insert_node(&node_9);
hash_table.insert_node(&node_10);
hash_table.insert_node(&node_11);
hash_table.insert_node(&node_12);
hash_table.insert_node(&node_13);
hash_table.insert_node(&node_14);
output << hash_table; // functionally identical to hash_table.print(output);
LINKED_LIST search_results = hash_table.get_nodes_with_key("AAAA");
output << search_results; // functionally identical to search_results.print(output);
}
/* program entry point */
int main()
{
// Declare a file output stream object.
std::ofstream file;
/**
* If hash_table_driver_output.txt does not already exist in the same directory as hash_table_driver.cpp,
* create a new file named hash_table_driver_output.txt.
*
* Open the plain-text file named hash_table_driver_output.txt
* and set that file to be overwritten with program data.
*/
file.open("hash_table_driver_output.txt");
// Print an opening message to the command line terminal.
std::cout << "\n\n--------------------------------";
std::cout << "\nStart Of Program";
std::cout << "\n--------------------------------";
// Print an opening message to the file output stream.
file << "--------------------------------";
file << "\nStart Of Program";
file << "\n--------------------------------";
// Implement a series of unit tests which demonstrate the functionality of LINKED_LIST class variables.
unit_test_0(std::cout);
unit_test_0(file);
unit_test_1(std::cout);
unit_test_1(file);
unit_test_2(std::cout);
unit_test_2(file);
unit_test_3(std::cout);
unit_test_3(file);
unit_test_4(std::cout);
unit_test_4(file);
unit_test_5(std::cout);
unit_test_5(file);
// Print a closing message to the command line terminal.
std::cout << "\n\n--------------------------------";
std::cout << "\nEnd Of Program";
std::cout << "\n--------------------------------\n\n";
// Print a closing message to the file output stream.
file << "\n\n--------------------------------";
file << "\nEnd Of Program";
file << "\n--------------------------------";
// Close the file output stream.
file.close();
// Exit the program.
return 0;
}