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 pathP16_HASH_TABLE.html
3000 lines (2812 loc) · 211 KB
/
P16_HASH_TABLE.html
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<hr>
<p><strong>HASH_TABLE</strong></p>
<hr>
<figure class="wp-block-image size-full"><img data-attachment-id="21638" data-permalink="https://karlinaobject.wordpress.com/hash_table/hash_table_image/" data-orig-file="https://karlinaobject.wordpress.com/wp-content/uploads/2023/07/hash_table_image.png" data-orig-size="960,720" data-comments-opened="1" data-image-meta="{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0"}" data-image-title="hash_table_image" data-image-description="" data-image-caption="" data-medium-file="https://karlinaobject.wordpress.com/wp-content/uploads/2023/07/hash_table_image.png?w=300" data-large-file="https://karlinaobject.wordpress.com/wp-content/uploads/2023/07/hash_table_image.png?w=960" role="button" width="960" height="720" src="https://karlinaobject.wordpress.com/wp-content/uploads/2023/07/hash_table_image.png" alt="" class="wp-image-21638" /></figure>
<hr>
<p>image_link: <a style="background:#000000;color:#ff9000" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table_image.png" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table_image.png</a></p>
<hr>
<figure class="wp-block-image size-full"><img data-attachment-id="21623" data-permalink="https://karlinaobject.wordpress.com/linked_list/linked_list_image/" data-orig-file="https://karlinaobject.wordpress.com/wp-content/uploads/2023/07/linked_list_image.png" data-orig-size="960,720" data-comments-opened="1" data-image-meta="{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0"}" data-image-title="linked_list_image" data-image-description="" data-image-caption="" data-medium-file="https://karlinaobject.wordpress.com/wp-content/uploads/2023/07/linked_list_image.png?w=300" data-large-file="https://karlinaobject.wordpress.com/wp-content/uploads/2023/07/linked_list_image.png?w=960" role="button" width="960" height="720" src="https://karlinaobject.wordpress.com/wp-content/uploads/2023/07/linked_list_image.png" alt="" class="wp-image-21623" /></figure>
<hr>
<p>image_link: <a style="background:#000000;color:#ff9000" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/linked_list_image.png" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/linked_list_image.png</a></p>
<hr>
<p><span style="background: #ffff00">The <a style="background: #ff9000;color: #000000" href="https://en.wikipedia.org/wiki/C%2B%2B" target="_blank" rel="noopener">C++</a> program featured in this tutorial web page demonstrates the concept of Object Oriented Programming (<a style="background: #ff9000;color: #000000" href="https://en.wikipedia.org/wiki/Object-oriented_programming" target="_blank" rel="noopener">OOP</a>). The program implements a user defined data type for instantiating <strong>HASH_TABLE</strong> type objects. Each HASH_TABLE type object represents an array whose elements are <a style="background: #000000;color: #00ff00" href="https://karlinaobject.wordpress.com/linked_list/" target="_blank" rel="noopener">LINKED_LIST</a> type objects (and each LINKED_LIST object represents a singly-linked list whose elements are <strong>NODE</strong> type <a style="background: #ff9000;color: #000000" href="https://en.wikipedia.org/wiki/Struct_(C_programming_language)" target="_blank" rel="noopener">struct</a> variables). A NODE can be inserted into the HASH_TABLE using a <a style="background: #ff9000;color: #000000" href="https://en.wikipedia.org/wiki/Hash_function" target="_blank" rel="noopener">hash function</a> which takes that NODE’s key value as the function input and returns an index of the HASH_TABLE array in which to store that NODE as the last element of the LINKED_LIST which is located at that particular array index.</span></p>
<p><em>To view hidden text inside each of the preformatted text boxes below, scroll horizontally.</em></p>
<pre>
array_length := HASH_TABLE.N. // array_length is a nonnegative integer.
array_index := HASH_TABLE.hash(NODE.key). // array_index is a nonnegative integer which is smaller than array_length.
</pre>
<hr>
<p><strong>SOFTWARE_APPLICATION_COMPONENTS</strong></p>
<hr>
<p>C++_header_file: <a style="background: #000000;color: #00ff00" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/public_linked_list.h" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/public_linked_list.h</a></p>
<p>C++_source_file: <a style="background: #000000;color: #00ff00" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/public_linked_list.cpp" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/public_linked_list.cpp</a></p>
<p>C++_header_file: <a style="background: #000000;color: #00ff00" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table.h" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table.h</a></p>
<p>C++_source_file: <a style="background: #000000;color: #00ff00" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table.cpp" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table.cpp</a></p>
<p>C++_source_file: <a style="background: #000000;color: #00ff00" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table_driver.cpp" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table_driver.cpp</a></p>
<p>plain-text_file: <a style="background: #000000;color: #ff9000" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table_driver_output.txt" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table_driver_output.txt</a></p>
<hr>
<p><strong>PROGRAM_COMPILATION_AND_EXECUTION</strong></p>
<hr>
<p>STEP_0: Copy and paste the C++ code from the files named <a style="background: #000000;color: #00ff00" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/public_linked_list.h" target="_blank" rel="noopener">public_linked_list.h</a>, <a style="background: #000000;color: #00ff00" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/public_linked_list.cpp" target="_blank" rel="noopener">public_linked_list.cpp</a>, <a style="background: #000000;color: #00ff00" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table.h" target="_blank" rel="noopener">hash_table.h</a>, <a style="background: #000000;color: #00ff00" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table.cpp" target="_blank" rel="noopener">hash_table.cpp</a>, and <a style="background: #000000;color: #00ff00" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table_driver.cpp" target="_blank" rel="noopener">hash_table_driver.cpp</a> into their own new text editor documents and save those documents using their corresponding file names:</p>
<pre>public_linked_list.h</pre>
<pre>public_linked_list.cpp</pre>
<pre>hash_table.h</pre>
<pre>hash_table.cpp</pre>
<pre>hash_table_driver.cpp</pre>
<p>STEP_1: Open a <a style="background: #ff9000;color: #000000" href="https://en.wikipedia.org/wiki/Unix" target="_blank" rel="noopener">Unix</a> command line terminal application and set the current directory to wherever the C++ program files are collectively located on the local machine (e.g. Desktop).</p>
<pre>cd Desktop</pre>
<p>STEP_2: Compile the C++ files into machine-executable instructions (i.e. object file) and then into an executable piece of software named <strong>app</strong> using the following command:</p>
<pre>g++ hash_table_driver.cpp hash_table.cpp public_linked_list.cpp -o app</pre>
<p>STEP_3: If the program compilation command does not work, then use the following commands (in top-down order) to install the C/C++ compiler (which is part of the <a style="background: #ff9000;color: #000000" href="https://en.wikipedia.org/wiki/GNU_Compiler_Collection" target="_blank" rel="noopener">GNU Compiler Collection (GCC)</a>):</p>
<pre>sudo apt install build-essential</pre>
<pre>sudo apt-get install g++</pre>
<p>STEP_4: After running the <strong>g++</strong> command, run the executable file using the following command:</p>
<pre>./app</pre>
<p>STEP_5: Observe program results on the command line terminal and in the <a style="background: #000000;color: #ff9000" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table_driver_output.txt" target="_blank" rel="noopener">output file</a>.</p>
<hr>
<p><strong>LINKED_LIST_CLASS_HEADER</strong></p>
<hr>
<p>The following header file contains the preprocessing directives and function prototypes of the LINKED_LIST class.</p>
<p>(Note that the text inside of each of the the preformatted text boxes below appears on this web page (while rendered correctly by the <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/Web_browser" target="_blank" rel="noopener">web browser</a>) to be identical to the content of that preformatted text box text’s respective <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/Plain_text" target="_blank" rel="noopener">plain-text</a> file or <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/Source_code" target="_blank" rel="noopener">source code</a> output file (whose <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/URL" target="_blank" rel="noopener">Uniform Resource Locator</a> is displayed as the <strong style="background:#000000;color:#00ff00">green</strong> <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/Hyperlink" target="_blank" rel="noopener">hyperlink</a> immediately above that preformatted text box (if that hyperlink points to a <strong>source code file</strong>) or whose Uniform Resource Locator is displayed as the <strong style="background:#000000;color:#ff9000">orange</strong> hyperlink immediately above that preformatted text box (if that hyperlink points to a <strong>plain-text file</strong>))).</p>
<p>A <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/Computer" target="_blank" rel="noopener">computer</a> interprets a C++ source code as a series of programmatic instructions (i.e. <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/Software" target="_blank" rel="noopener">software</a>) which govern how the <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/Computer_hardware" target="_blank" rel="noopener">hardware</a> of that computer behaves).</p>
<p><em>(Note that angle brackets which resemble <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/HTML" target="_blank" rel="noopener">HTML</a> tags (i.e. an “is less than” symbol (i.e. ‘<‘) followed by an “is greater than” symbol (i.e. ‘>’)) displayed on this web page have been replaced (at the source code level of this web page) with the Unicode symbols <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/Less-than_sign" target="_blank" rel="noopener">U+003C</a> (which is rendered by the web browser as ‘<‘) and <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/Greater-than_sign" target="_blank" rel="noopener">U+003E</a> (which is rendered by the web browser as ‘>’). That is because the <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/WordPress.com" target="_blank" rel="noopener">WordPress</a> web page editor or web browser interprets a plain-text version of an “is less than” symbol followed by an “is greater than” symbol as being an opening HTML tag (which means that the WordPress web page editor or web browser deletes or fails to display those (plain-text) inequality symbols and the content between those (plain-text) inequality symbols)).</em></p>
<p>C++_header_file: <a style="background: #000000;color: #00ff00" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/public_linked_list.h" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/public_linked_list.h</a></p>
<hr>
<pre>/**
* file: public_linked_list.h
* type: C++ (header file)
* author: karbytes
* date: 08_JULY_2023
* license: PUBLIC_DOMAIN
*/
/* preprocessing directives */
#ifndef LINKED_LIST_H // If public_linked_list.h has not already been linked to a source file (.cpp),
#define LINKED_LIST_H // then link this header file to the source file(s) which include this header file.
/* preprocessing directives */
#include <iostream> // library for defining objects which handle command line input and command line output
#include <fstream> // library for defining objects which handle file input and file output
#include <string> // library which defines a sequence of text characters (i.e. char type values) as a string type variable
/**
* A variable whose data type is NODE is a tuple consisting of two variables
* such that one of those variables stores some arbitrary piece of information (i.e. key)
* while the other variable stores the address of a NODE variable (i.e. next).
*
* Like a C++ class, a C++ struct is a user defined data type.
*
* Note that each of the members of a struct variable is public and neither private nor protected.
*
* Note that each of the members of a struct variable is a variable and not a function.
*/
struct NODE
{
std::string key; // key stores an arbitrary sequence of characters
NODE * next; // next stores the memory address of a NODE type variable.
};
/**
* A variable whose data type is LINKED_LIST is a software object whose data attributes
* consist of exactly one pointer-to-NODE type variable which is assumed to be the
* first node of a linear and unidirectional (i.e. singly-linked) linked list.
*
* When a LINKED_LIST type variable is declared, a dynamic NODE type variable is created
* and the memory address of that dynamic NODE type variable is stored in a pointer-to-NODE
* type variable named head.
*
* After a LINKED_LIST type variable is created and before that variable is deleted,
* NODE type elements can be inserted into the list which the LINKED_LIST type variable represents
* and NODE type elements can be removed from the list which the LINKED_LIST type variable represents.
*
* After a LINKED_LIST type variable is created and before that variable is deleted,
* that variable (i.e. object) can invoke a print function which prints a description
* of the caller LINKED_LIST object.
*
* When a LINKED_LIST variable is deleted, the pointer-to-NODE type variable named head
* (which was assigned memory during program runtime rather than during program compilation time)
* is deleted.
*/
class LINKED_LIST
{
public:
NODE * head; // head stores the memory address of the first NODE type element of a LINKED_LIST type data structure.
bool remove_node_with_key(std::string key); // helper method
LINKED_LIST(); // constructor
void insert_node_at_end_of_list(NODE * node); // setter method
void remove_nodes_with_key(std::string key); // setter method
int get_number_of_nodes_in_list(); // getter method
void print(std::ostream & output = std::cout); // descriptor method
friend std::ostream & operator << (std::ostream & output, LINKED_LIST & linked_list); // descriptor method
~LINKED_LIST(); // destructor
};
/* preprocessing directives */
#endif // Terminate the conditional preprocessing directives code block in this header file.
</pre>
<hr>
<p><strong>LINKED_LIST_CLASS_SOURCE_CODE</strong></p>
<hr>
<p>The following source code defines the functions of the LINKED_LIST class.</p>
<p>C++_source_file: <a style="background: #000000;color: #00ff00" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/public_linked_list.cpp" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/public_linked_list.cpp</a></p>
<hr>
<pre>/**
* file: public_linked_list.cpp
* type: C++ (source file)
* author: karbytes
* date: 08_JULY_2023
* license: PUBLIC_DOMAIN
*/
// Include the C++ header file which contains preprocessing directives, variable declarations, and function prototypes for the LINKED_LIST class.
#include "public_linked_list.h"
/**
* Starting at the NODE type element whose memory address is stored in head and ending at NULL,
* traverse the singly-linked list comprised of NODE type elements such that each of those elements are visited.
*
* If the current element's key is identical to the input key,
* set the next property of the previous node to
* the memory address of the node which is next in the list after the current element
* and return true.
*
* If at least one node in the list has a key which is identical to the input key,
* the function will return true. Otherwise, the function will return false.
*
* (The method of using two pointers, p and q, to traverse the list is colloquially described as "inchworming"
* because those two pointers metaphorically resemble opposite ends of an inchworm as that insect stretches its
* front end forward by approximately one inch before moving its back end to where its front end is located).
*/
bool LINKED_LIST::remove_node_with_key(std::string key)
{
NODE * p = head;
NODE * q = head;
while (q)
{
if ((q -> key == key) && (q != head))
{
std::cout << "\n\nThe NODE whose memory address is " << q << " is being removed from the LINKED_LIST...";
p -> next = q -> next;
return true;
}
p = q;
q = p -> next;
}
return false;
}
/**
* Instantiate an "empty" linked list (i.e. a linked list with only a head node and no "body nodes").
*
* Note that only "body nodes" may be inserted into or else removed from a linked list which a LINKED_LIST type object represents.
*/
LINKED_LIST::LINKED_LIST()
{
std::cout << "\n\nCreating the LINKED_LIST type object whose memory address is " << this << "...";
head = new NODE;
head -> key = "HEAD";
head -> next = NULL;
}
/**
* Make the input NODE type variable the last element of the linked list represented by the caller LINKED_LIST object.
*
* (Note that using a NODE which is currently an element of the caller LINKED_LIST as the input to this function
* will turn the linked list represented by the caller LINKED_LIST object into a closed loop rather than a finite linear sequence).
*
* (The method of using two pointers, p and q, to traverse the list is colloquially described as "inchworming"
* because those two pointers metaphorically resemble opposite ends of an inchworm as that insect stretches its
* front end forward by approximately one inch before moving its back end to where its front end is located).
*/
void LINKED_LIST::insert_node_at_end_of_list(NODE * node)
{
NODE * p = head;
NODE * q = head;
while (q)
{
p = q;
q = p -> next;
}
std::cout << "\n\nThe NODE whose memory address is " << p << " is being inserted into the LINKED_LIST as the last element of that list...";
p -> next = node;
node -> next = NULL;
}
/**
* Remove all nodes from the linked list represented by the caller LINKED_LIST object
* whose key values are identical to the input key value.
*/
void LINKED_LIST::remove_nodes_with_key(std::string key)
{
bool at_least_one_node_was_removed = false;
at_least_one_node_was_removed = remove_node_with_key(key);
while (at_least_one_node_was_removed) at_least_one_node_was_removed = remove_node_with_key(key);
}
/**
* Return the natural number count of NODE type elements inside the singly-linked list which
* the caller LINKED_LIST object represents.
*
* Starting with the head and ending with NULL,
* traverse sequentially down the list of NODE type elements and
* count each element in the exist.
*
* If the linked list is "empty" (i.e. the head is the only NODE in the caller LINKED_LIST),
* one will be returned.
*
* (The method of using two pointers, p and q, to traverse the list is colloquially described as "inchworming"
* because those two pointers metaphorically resemble opposite ends of an inchworm as that insect stretches its
* front end forward by approximately one inch before moving its back end to where its front end is located).
*/
int LINKED_LIST::get_number_of_nodes_in_list()
{
int node_count = 0;
NODE * p = head;
NODE * q = head;
while (q)
{
p = q;
q = p -> next;
node_count += 1;
}
return node_count;
}
/**
* The print method of the LINKED_LIST class prints a description of the caller LINKED_LIST object to the output stream.
*
* A description of each NODE type element of the linked list which the caller LINKED_LIST object represents will be printed to the output stream
* in the order those elements were inserted into the list by "inchworming" from NODE_0 to NODE_N (where N is the total number of nodes in the list).
*
* (The method of using two pointers, p and q, to traverse the list is colloquially described as "inchworming"
* because those two pointers metaphorically resemble opposite ends of an inchworm as that insect stretches its
* front end forward by approximately one inch before moving its back end to where its front end is located).
*
* Note that the default value of the function input parameter is the standard command line output stream (std::cout).
*
* The default parameter is defined in the LINKED_LIST class header file (i.e. public_linked_list.h) and not in the LINKED_LIST class source file (i.e. public_linked_list.cpp).
*/
void LINKED_LIST::print(std::ostream & output)
{
int node_count = 0;
NODE * p = head;
NODE * q = head;
output << "\n\n--------------------------------------------------------------------------------------------------";
output << "\nthis = " << this << ". // The keyword named this is a pointer which stores the memory address of the first memory cell of a LINKED_LIST sized chunk of contiguous memory cells which are allocated to the caller LINKED_LIST object.";
output << "\n&head = " << &head << ". // The reference operation returns the memory address of the first memory cell of a pointer-to-NODE sized chunk of contiguous memory cells which are allocated to the caller LINKED_LIST data attribute named head.";
output << "\nsizeof(NODE) = " << sizeof(NODE) << ". // The sizeof() operation returns the number of bytes of memory which a NODE type variable occupies. (Each memory cell has a data capacity of 1 byte).";
output << "\nsizeof(std::string) = " << sizeof(std::string) << ". // The sizeof() operation returns the number of bytes of memory which a string type variable occupies. (Each memory cell has a data capacity of 1 byte).";
output << "\nsizeof(NODE *) = " << sizeof(NODE *) << ". // The sizeof() operation returns the number of bytes of memory which a pointer-to-NODE type variable occupies. (Each memory cell has a data capacity of 1 byte).";
output << "\nsizeof(LINKED_LIST) = " << sizeof(LINKED_LIST) << ". // The sizeof() operation returns the number of bytes of memory which a LINKED_LIST type variable occupies. (Each memory cell has a data capacity of 1 byte).";
output << "\nhead = " << head << ". // head stores either the first memory cell of a contiguous chunk of memory cells which are allocated to a NODE type variable or else head stores NULL (and the value NULL is displayed as 0).";
output << "\nhead -> key = " << head -> key << ". // The arrow operator returns the string type property named key of the NODE type variable which head points to.";
output << "\nhead -> next = " << head -> next << ". // The arrow operator returns the pointer-to-NODE type property named next of the NODE type variable which head points to.";
output << "\nget_number_of_nodes_in_list() = " << get_number_of_nodes_in_list() << ".";
output << "\n// p is a pointer to a NODE type variable.";
output << "\nLINKED_LIST := {";
while (q)
{
p = q;
output << "\n\tNODE_" << node_count << " := {";
output << "\n\t\tp := " << p << ".";
output << "\n\t\tp -> key = " << p -> key << ".";
output << "\n\t\tp -> next = " << p -> next << ".";
output << "\n\t}.";
q = p -> next;
node_count += 1;
}
output << "\n}.";
output << "\n--------------------------------------------------------------------------------------------------";
}
/**
* The friend function is an alternative to the print method.
* The friend function overloads the ostream operator (<<).
*
* (Overloading an operator is assigning a different function to a native operator other than the function which that operator is used to represent by default).
*
* Note that the default value of the leftmost function input parameter is the standard command line output stream (std::cout).
* The default parameter is defined in the LINKED_LIST class header file (i.e. public_linked_list.h).
*
* The friend function is not a member of the LINKED_LIST class,
* but the friend function has access to the private and protected members
* of the LINKED_LIST class and not just to the public members of the LINKED_LIST class.
*
* The friend keyword only prefaces the function prototype of this function
* (and the prototype of this function is declared in the LINKED_LIST class header file (i.e. public_linked_list.h)).
*
* The friend keyword does not preface the definition of this function
* (and the definition of this function is specified in the LINKED_LIST class source file (i.e. public_linked_list.cpp)).
*
* // overloaded print function example one
* LINKED_LIST linked_list_0;
* std::cout << linked_list_0; // identical to linked_list_0.print();
*
* // overloaded print function example two
* std::ofstream file;
* LINKED_LIST linked_list_1;
* file << linked_list_1; // identical to linked_list_1.print(file);
*/
std::ostream & operator << (std::ostream & output, LINKED_LIST & linked_list)
{
linked_list.print(output);
return output;
}
/**
* The destructor method of the LINKED_LIST class de-allocates memory which was used to
* instantiate the LINKED_LIST object which is calling this function.
*
* The destructor method of the LINKED_LIST class is automatically called when
* the program scope in which the caller LINKED_LIST object was instantiated terminates.
*/
LINKED_LIST::~LINKED_LIST()
{
std::cout << "\n\nDeleting the LINKED_LIST type object whose memory address is " << this << "...";
delete head;
}
</pre>
<hr>
<p><strong>HASH_TABLE_CLASS_HEADER</strong></p>
<hr>
<p>The following header file contains the preprocessing directives and function prototypes of the HASH_TABLE class.</p>
<p>C++_header_file: <a style="background: #000000;color: #00ff00" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table.h" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table.h</a></p>
<hr>
<pre>/**
* file: hash_table.h
* type: C++ (header file)
* author: karbytes
* date: 08_JULY_2023
* license: PUBLIC_DOMAIN
*/
/* preprocessing directives */
#ifndef HASH_TABLE_H // If hash_table.h has not already been linked to a source file (.cpp),
#define HASH_TABLE_H // then link this header file to the source file(s) which include this header file.
/* preprocessing directives */
#include "public_linked_list.h" // Include the C++ header file which contains preprocessing directives, variable declarations, and function prototypes for the LINKED_LIST class.
#define MAXIMUM_N 100 // constant which represents maximum N value
/**
* A variable whose data type is HASH_TABLE is a software object whose data attributes
* consist of exactly one pointer-to-LINKED_LIST type variable named array
* and exactly one int type variable named N.
*
* N stores a nonnegative integer value no larger than MAXIMUM_N.
*
* array stores the memory address of the first memory cell of a LINKED_LIST sized chunk of contiguous memory cells
* (and array is a pointer to the first element of an array comprised of N LINKED_LIST type elements).
*
* When a HASH_TABLE type variable is declared, a dynamic LINKED_LIST type variable is created
* and the memory address of that dynamic LINKED_LIST type variable is stored in a pointer-to-LINKED_LIST
* type variable named array.
*
* After a HASH_TABLE type variable is created and before that variable is deleted,
* NODE type elements can be inserted into the hash table array and
* NODE type elements can be removed from the hash table array.
*
* When a NODE is inserted into the hash table array, a hash function takes that NODE's key as
* a hash function input and then the hash function outputs a corresponding nonnegative integer
* no larger than (N - 1) which represents the index of the hash table array where that
* NODE will be inserted (and that NODE will be appended to the end of the LINKED_LIST which is stored at
* that particular array index).
*
* After a HASH_TABLE type variable is created and before that variable is deleted,
* that variable (i.e. object) can invoke a print function which prints a description
* of the caller HASH_TABLE object.
*
* When a HASH_TABLE variable is deleted, the pointer-to-LINKED_LIST type variable named array
* (which was assigned memory during program runtime rather than during program compilation time)
* and every head property of every LINKED_LIST type element of that array
* is deleted.
*/
class HASH_TABLE
{
private:
// array stores the memory address of the first element of an array of N LINKED_LIST type elements.
LINKED_LIST * array;
// N stores a nonnegative integer value no larger than MAXIMUM_N.
int N;
// The hash function returns an array index which corresponds with the input key value.
int hash(std::string key);
public:
// The default constructor sets the length of the hash table array to 10 by default.
HASH_TABLE(int hash_table_length = 10);
// The setter method appends the input NODE to the end of the LINKED_LIST located at array[hash(node -> key)].
void insert_node(NODE * node);
// The setter method removes all NODE type instances from the hash table array whose key values match the input key value.
void remove_nodes_with_key(std::string key);
// The getter method returns a singly-linked list of all NODE type instances in the hash table array whose key values match the input key value.
LINKED_LIST get_nodes_with_key(std::string key);
// The getter method returns the number of LINKED_LIST type values stored in the hash table array (and the value returned is N).
int get_number_of_linked_lists_in_hash_table();
// The getter method returns the total number of NODE type values stored in the hash table array (and the value returned is an integer which is equal to or larger than N).
int get_number_of_nodes_in_hash_table();
// The descriptor method prints a description of the caller HASH_TABLE object to the output stream (and the command line terminal is the default output stream parameter).
void print(std::ostream & output = std::cout);
// The descriptor method overloads the ostream operator to make it identical to calling the HASH_TABLE print function.
friend std::ostream & operator << (std::ostream & output, HASH_TABLE & hash_table);
// The destructor de-allocates memory which was assigned to the caller HASH_TABLE object.
~HASH_TABLE();
};
/* preprocessing directives */
#endif // Terminate the conditional preprocessing directives code block in this header file.
</pre>
<hr>
<p><strong>HASH_TABLE_CLASS_SOURCE_CODE</strong></p>
<hr>
<p>The following source code defines the functions of the HASH_TABLE class.</p>
<p>C++_source_file: <a style="background: #000000;color: #00ff00" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table.cpp" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table.cpp</a></p>
<hr>
<pre>/**
* file: hash_table.cpp
* type: C++ (source file)
* author: karbytes
* date: 08_JULY_2023
* license: PUBLIC_DOMAIN
*/
// Include the C++ header file which contains preprocessing directives, variable declarations, and function prototypes for the HASH_TABLE class.
#include "hash_table.h"
/**
* The hash function returns an array index which corresponds with the input key value.
*/
int HASH_TABLE::hash(std::string key)
{
int value = 0, i = 0;
for (i = 0; i < key.length(); i += 1) value += int(key[i]);
return value % N;
}
/**
* The default constructor sets the length of the hash table array to 10 by default.
*
* The function returns a HASH_TABLE type object.
*/
HASH_TABLE::HASH_TABLE(int hash_table_length)
{
std::cout << "\n\nCreating the HASH_TABLE type object whose memory address is " << this << "...";
N = ((hash_table_length < 1) || (hash_table_length > MAXIMUM_N)) ? 10 : hash_table_length;
array = new LINKED_LIST[N];
}
/**
* The setter method appends the input NODE to the end of the LINKED_LIST located at array[hash(node -> key)].
*/
void HASH_TABLE::insert_node(NODE * node)
{
int index = hash(node -> key);
array[index].insert_node_at_end_of_list(node);
}
/**
* The setter method removes all NODE type instances from the hash table array whose key values match the input key value.
*/
void HASH_TABLE::remove_nodes_with_key(std::string key)
{
int index = hash(key);
return array[index].remove_nodes_with_key(key);
}
/**
* The getter method returns a singly-linked list of all NODE type instances in the hash table array whose key values match the input key value.
*
* Set the next pointer value of the final NODE element in the returned LINKED_LIST to NULL.
*/
LINKED_LIST HASH_TABLE::get_nodes_with_key(std::string key)
{
int index = hash(key);
LINKED_LIST search_results;
NODE * p = array[index].head;
NODE * q = array[index].head;
while (q)
{
if ((p -> key == key) && (p != array[index].head))
{
search_results.insert_node_at_end_of_list(p);
p -> next = NULL;
}
p = q;
q = p -> next;
}
return search_results;
}
/**
* The getter method returns the number of LINKED_LIST type values stored in the hash table array (and the value returned is N).
*/
int HASH_TABLE::get_number_of_linked_lists_in_hash_table()
{
return N;
}
/**
* The getter method returns the total number of NODE type values stored in the hash table array (and the value returned is an integer which is equal to or larger than N).
*/
int HASH_TABLE::get_number_of_nodes_in_hash_table()
{
int node_count = 0, i = 0;
for (i = 0; i < N; i += 1) node_count += array[i].get_number_of_nodes_in_list();
return node_count;
}
/**
* The descriptor method prints a description of the caller HASH_TABLE object to the output stream (and the command line terminal is the default output stream parameter).
*/
void HASH_TABLE::print(std::ostream & output)
{
int i = 0;
output << "\n\n--------------------------------------------------------------------------------------------------";
output << "\nthis = " << this << ". // The keyword named this is a pointer which stores the memory address of the first memory cell of a HASH_TABLE sized chunk of contiguous memory cells which are allocated to the caller HASH_TABLE object.";
output << "\n&array = " << &array << ". // The reference operation returns the memory address of the first memory cell of a pointer-to-LINKED_LIST sized chunk of contiguous memory cells which are allocated to the caller HASH_TABLE data attribute named array.";
output << "\n&N = " << &N << ". // The reference operation returns the memory address of the first memory cell of a pointer-to-int sized chunk of contiguous memory cells which are allocated to the caller HASH_TABLE data attribute named N.";
output << "\nsizeof(int) = " << sizeof(int) << ". // The sizeof() operation returns the number of bytes of memory which a int type variable occupies. (Each memory cell has a data capacity of 1 byte).";
output << "\nsizeof(int *) = " << sizeof(int *) << ". // The sizeof() operation returns the number of bytes of memory which a pointer-to-int type variable occupies. (Each memory cell has a data capacity of 1 byte).";
output << "\nsizeof(std::string) = " << sizeof(std::string) << ". // The sizeof() operation returns the number of bytes of memory which a string type variable occupies. (Each memory cell has a data capacity of 1 byte).";
output << "\nsizeof(std::string *) = " << sizeof(std::string *) << ". // The sizeof() operation returns the number of bytes of memory which a pointer-to-string type variable occupies. (Each memory cell has a data capacity of 1 byte).";
output << "\nsizeof(NODE) = " << sizeof(NODE) << ". // The sizeof() operation returns the number of bytes of memory which a NODE type variable occupies. (Each memory cell has a data capacity of 1 byte).";
output << "\nsizeof(NODE *) = " << sizeof(NODE *) << ". // The sizeof() operation returns the number of bytes of memory which a pointer-to-NODE type variable occupies. (Each memory cell has a data capacity of 1 byte).";
output << "\nsizeof(LINKED_LIST) = " << sizeof(LINKED_LIST) << ". // The sizeof() operation returns the number of bytes of memory which a LINKED_LIST type variable occupies. (Each memory cell has a data capacity of 1 byte).";
output << "\nsizeof(LINKED_LIST *) = " << sizeof(LINKED_LIST *) << ". // The sizeof() operation returns the number of bytes of memory which a pointer-to-LINKED_LIST type variable occupies. (Each memory cell has a data capacity of 1 byte).";
output << "\nsizeof(HASH_TABLE) = " << sizeof(HASH_TABLE) << ". // The sizeof() operation returns the number of bytes of memory which a HASH_TABLE type variable occupies. (Each memory cell has a data capacity of 1 byte).";
output << "\nsizeof(HASH_TABLE *) = " << sizeof(HASH_TABLE *) << ". // The sizeof() operation returns the number of bytes of memory which a pointer-to-HASH_TABLE type variable occupies. (Each memory cell has a data capacity of 1 byte).";
output << "\narray = " << array << ". // array stores either the first memory cell of a contiguous chunk of memory cells which are allocated to a LINKED_LIST type variable or else array stores NULL (and the value NULL is displayed as 0).";
output << "\nN = " << N << ". // N stores the total number of LINKED_LIST types elements which are represented by the array property of the caller HASH_TABLE object.";
output << "\nHASH_TABLE := {";
for (i = 0; i < N; i += 1)
{
output << "\n\n############################################################";
output << "\narray[" << i << "] := {";
output << array[i];
output << "\n}.";
output << "\n############################################################";
}
output << "\n}.";
output << "\n--------------------------------------------------------------------------------------------------";
}
/**
* The friend function is an alternative to the print method.
* The friend function overloads the ostream operator (<<).
*
* (Overloading an operator is assigning a different function to a native operator other than the function which that operator is used to represent by default).
*
* Note that the default value of the leftmost function input parameter is the standard command line output stream (std::cout).
* The default parameter is defined in the HASH_TABLE class header file (i.e. hash_table.h).
*
* The friend function is not a member of the HASH_TABLE class,
* but the friend function has access to the private and protected members
* of the HASH_TABLE class and not just to the public members of the HASH_TABLE class.
*
* The friend keyword only prefaces the function prototype of this function
* (and the prototype of this function is declared in the HASH_TABLE class header file (i.e. hash_table.h)).
*
* The friend keyword does not preface the definition of this function
* (and the definition of this function is specified in the HASH_TABLE class source file (i.e. hash_table.cpp)).
*
* // overloaded print function example one
* HASH_TABLE hash_table_0;
* std::cout << hash_table_0; // identical to hash_table_0.print();
*
* // overloaded print function example two
* std::ofstream file;
* HASH_TABLE hash_table_1;
* file << hash_table_1; // identical to hash_table_1.print(file);
*/
std::ostream & operator << (std::ostream & output, HASH_TABLE & hash_table)
{
hash_table.print(output);
return output;
}
/**
* The destructor method of the HASH_TABLE class de-allocates memory which was used to
* instantiate the HASH_TABLE object which is calling this function.
*
* The destructor method of the HASH_TABLE class is automatically called when
* the program scope in which the caller HABLE_TABLE object was instantiated terminates.
*/
HASH_TABLE::~HASH_TABLE()
{
std::cout << "\n\nDeleting the HASH_TABLE type object whose memory address is " << this << "...";
delete [] array;
}
</pre>
<hr>
<p><strong>PROGRAM_SOURCE_CODE</strong></p>
<hr>
<p>The following source code defines the client which implements the HASH_TABLE class. The client executes a series of unit tests which demonstrate how the HASH_TABLE class methods work.</p>
<p>C++_source_file: <a style="background: #000000;color: #00ff00" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table_driver.cpp" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table_driver.cpp</a></p>
<hr>
<pre>/**
* 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;
}
</pre>
<hr>
<p><strong>SAMPLE_PROGRAM_OUTPUT</strong></p>
<hr>
<p>The text in the preformatted text box below was generated by one use case of the C++ program featured in this <a style="background: #ff9000;color: #000000" href="https://en.wikipedia.org/wiki/Computer_programming" target="_blank" rel="noopener">computer programming</a> tutorial web page.</p>
<p>plain-text_file: <a style="background: #000000;color: #ff9000" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table_driver_output.txt" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/hash_table_driver_output.txt</a></p>
<hr>
<pre>--------------------------------
Start Of Program
--------------------------------
************************************************
Unit Test # 0: HASH_TABLE constructor, print method, and destructor.
************************************************
HASH_TABLE hash_table;
hash_table.print(output);
--------------------------------------------------------------------------------------------------
this = 0x7ffc9c06b880. // The keyword named this is a pointer which stores the memory address of the first memory cell of a HASH_TABLE sized chunk of contiguous memory cells which are allocated to the caller HASH_TABLE object.
&array = 0x7ffc9c06b880. // The reference operation returns the memory address of the first memory cell of a pointer-to-LINKED_LIST sized chunk of contiguous memory cells which are allocated to the caller HASH_TABLE data attribute named array.
&N = 0x7ffc9c06b888. // The reference operation returns the memory address of the first memory cell of a pointer-to-int sized chunk of contiguous memory cells which are allocated to the caller HASH_TABLE data attribute named N.
sizeof(int) = 4. // The sizeof() operation returns the number of bytes of memory which a int type variable occupies. (Each memory cell has a data capacity of 1 byte).
sizeof(int *) = 8. // The sizeof() operation returns the number of bytes of memory which a pointer-to-int type variable occupies. (Each memory cell has a data capacity of 1 byte).
sizeof(std::string) = 32. // The sizeof() operation returns the number of bytes of memory which a string type variable occupies. (Each memory cell has a data capacity of 1 byte).
sizeof(std::string *) = 8. // The sizeof() operation returns the number of bytes of memory which a pointer-to-string type variable occupies. (Each memory cell has a data capacity of 1 byte).
sizeof(NODE) = 40. // The sizeof() operation returns the number of bytes of memory which a NODE type variable occupies. (Each memory cell has a data capacity of 1 byte).
sizeof(NODE *) = 8. // The sizeof() operation returns the number of bytes of memory which a pointer-to-NODE type variable occupies. (Each memory cell has a data capacity of 1 byte).
sizeof(LINKED_LIST) = 8. // The sizeof() operation returns the number of bytes of memory which a LINKED_LIST type variable occupies. (Each memory cell has a data capacity of 1 byte).
sizeof(LINKED_LIST *) = 8. // The sizeof() operation returns the number of bytes of memory which a pointer-to-LINKED_LIST type variable occupies. (Each memory cell has a data capacity of 1 byte).
sizeof(HASH_TABLE) = 16. // The sizeof() operation returns the number of bytes of memory which a HASH_TABLE type variable occupies. (Each memory cell has a data capacity of 1 byte).
sizeof(HASH_TABLE *) = 8. // The sizeof() operation returns the number of bytes of memory which a pointer-to-HASH_TABLE type variable occupies. (Each memory cell has a data capacity of 1 byte).
array = 0x564e5da804b8. // array stores either the first memory cell of a contiguous chunk of memory cells which are allocated to a LINKED_LIST type variable or else array stores NULL (and the value NULL is displayed as 0).
N = 10. // N stores the total number of LINKED_LIST types elements which are represented by the array property of the caller HASH_TABLE object.
HASH_TABLE := {
############################################################