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 pathP15_LINKED_LIST.html
938 lines (890 loc) · 59 KB
/
P15_LINKED_LIST.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
<hr />
<p><strong>LINKED_LIST</strong></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" width="960" height="720" src="https://karlinaobject.wordpress.com/wp-content/uploads/2023/07/linked_list_image.png" alt="" class="wp-image-21623" srcset="https://karlinaobject.wordpress.com/wp-content/uploads/2023/07/linked_list_image.png 960w, https://karlinaobject.wordpress.com/wp-content/uploads/2023/07/linked_list_image.png?w=150&h=113 150w, https://karlinaobject.wordpress.com/wp-content/uploads/2023/07/linked_list_image.png?w=300&h=225 300w, https://karlinaobject.wordpress.com/wp-content/uploads/2023/07/linked_list_image.png?w=768&h=576 768w" sizes="(max-width: 960px) 100vw, 960px" /></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>LINKED_LIST</strong> type objects. Each LINKED_LIST type object represents a singly-linked list whose elements are NODE type variables. A <strong>NODE</strong> type variable is a user defined data type for instantiating data structures comprised of two variables: a string type variable named <strong>key</strong> and a pointer-to-NODE type variable named <strong>next</strong>. A LINKED_LIST object can execute various functions including the ability to insert NODE type elements to the end of the linked list and the ability to remove all elements from the list whose key values match an input string type value. (Note that, unlike a C++ <a style="background: #000000;color: #00ff00" href="https://karlinaobject.wordpress.com/pointers_and_arrays/" target="_blank" rel="noopener">array</a>, the elements of a C++ <a style="background: #ff9000;color: #000000" href="https://en.wikipedia.org/wiki/Linked_list" target="_blank" rel="noopener">linked list</a> are not necessarily homogeneous (i.e. of the same data type). Also, unlike a C++ array, a C++ linked list can change size after it is instantiated. Lastly, unlike a C++ array, the elements of a C++ linked list are not necessarily contiguous in memory).</span></p>
<p><em>To view hidden text inside each of the preformatted text boxes below, scroll horizontally.</em></p>
<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/linked_list.h" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/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/linked_list.cpp" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/linked_list.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/linked_list_driver.cpp" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/linked_list_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/linked_list_driver_output.txt" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/linked_list_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/linked_list.h" target="_blank" rel="noopener">linked_list.h</a>, <a style="background: #000000;color: #00ff00" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/linked_list.cpp" target="_blank" rel="noopener">linked_list.cpp</a>, and <a style="background: #000000;color: #00ff00" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/linked_list_driver.cpp" target="_blank" rel="noopener">linked_list_driver.cpp</a> into their own new text editor documents and save those documents using their corresponding file names:</p>
<pre>linked_list.h</pre>
<pre>linked_list.cpp</pre>
<pre>linked_list_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++ linked_list_driver.cpp 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/linked_list_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/linked_list.h" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/linked_list.h</a></p>
<hr />
<pre>/**
* file: linked_list.h
* type: C++ (header file)
* author: karbytes
* date: 07_JULY_2023
* license: PUBLIC_DOMAIN
*/
/* preprocessing directives */
#ifndef LINKED_LIST_H // If 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
{
private:
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
public:
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/linked_list.cpp" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/linked_list.cpp</a></p>
<hr />
<pre>/**
* file: linked_list.cpp
* type: C++ (source file)
* author: karbytes
* date: 07_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 "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. linked_list.h) and not in the LINKED_LIST class source file (i.e. 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. 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. 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. 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>PROGRAM_SOURCE_CODE</strong></p>
<hr />
<p>The following source code defines the client which implements the LINKED_LIST class. The client executes a series of unit tests which demonstrate how the LINKED_LIST 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/linked_list_driver.cpp" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/linked_list_driver.cpp</a></p>
<hr />
<pre>/**
* file: linked_list_driver.cpp
* type: C++ (source file)
* date: 07_JULY_2023
* author: karbytes
* license: PUBLIC_DOMAIN
*/
#include "linked_list.h" // Include the C++ header file which contains preprocessing directives, variable declarations, and function prototypes for the LINKED_LIST 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);
/**
* Unit Test # 0: LINKED_LIST constructor, print method, and destructor.
*/
void unit_test_0(std::ostream & output)
{
output << "\n\n************************************************";
output << "\nUnit Test # 0: LINKED_LIST constructor, print method, and destructor.";
output << "\n************************************************";
output << "\nLINKED_LIST linked_list;";
output << "\nlinked_list.print(output);";
LINKED_LIST linked_list;
linked_list.print(output);
}
/**
* Unit Test # 1: LINKED_LIST constructor, insert method, print method, and destructor.
*/
void unit_test_1(std::ostream & output)
{
output << "\n\n************************************************";
output << "\nUnit Test # 1: LINKED_LIST constructor, insert method, print method, and destructor.";
output << "\n************************************************";
output << "\nLINKED_LIST linked_list;";
output << "\nNODE node;";
output << "\nnode.key = \"unit_test_1\";";
output << "\nnode.next = NULL;";
output << "\nlinked_list.insert_node_at_end_of_list(&node);";
output << "\nlinked_list.print(output);";
LINKED_LIST linked_list;
NODE node;
node.key = "unit_test_1";
node.next = NULL;
linked_list.insert_node_at_end_of_list(&node);
linked_list.print(output);
}
/**
* Unit Test # 2: LINKED_LIST constructor, insert method, print method, and destructor.
*/
void unit_test_2(std::ostream & output)
{
output << "\n\n************************************************";
output << "\nUnit Test # 2: LINKED_LIST constructor, insert method, print method, and destructor.";
output << "\n************************************************";
output << "\nLINKED_LIST linked_list;";
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 << "\nlinked_list.insert_node_at_end_of_list(&node_A);";
output << "\nlinked_list.insert_node_at_end_of_list(&node_B);";
output << "\nlinked_list.insert_node_at_end_of_list(&node_C);";
output << "\noutput << linked_list; // functionally identical to linked_list.print(output)";
LINKED_LIST linked_list;
NODE node_A = { key : "node_A", next : NULL };
NODE node_B = { key : "node_B", next : NULL };
NODE node_C = { key : "node_C", next : NULL };
linked_list.insert_node_at_end_of_list(&node_A);
linked_list.insert_node_at_end_of_list(&node_B);
linked_list.insert_node_at_end_of_list(&node_C);
output << linked_list;
}
/**
* Unit Test # 3: LINKED_LIST constructor, insert method, remove method, print method, and destructor.
*/
void unit_test_3(std::ostream & output)
{
output << "\n\n************************************************";
output << "\nUnit Test # 3: LINKED_LIST constructor, insert method, remove method, print method, and destructor.";
output << "\n************************************************";
output << "\nLINKED_LIST linked_list;";
output << "\nNODE node_X = { key : \"node_X\", next : NULL };";
output << "\nNODE node_Y = { key : \"node_Y\", next : NULL };";
output << "\nNODE node_Z = { key : \"node_Z\", next : NULL };";
output << "\nlinked_list.insert_node_at_end_of_list(&node_X);";
output << "\nlinked_list.insert_node_at_end_of_list(&node_Y);";
output << "\nlinked_list.insert_node_at_end_of_list(&node_Z);";
output << "\nlinked_list.print(output);";
output << "\nlinked_list.remove_nodes_with_key(\"node_Y\");";
output << "\nlinked_list.print(output);";
LINKED_LIST linked_list;
NODE node_X = { key : "node_X", next : NULL };
NODE node_Y = { key : "node_Y", next : NULL };
NODE node_Z = { key : "node_Z", next : NULL };
linked_list.insert_node_at_end_of_list(&node_X);
linked_list.insert_node_at_end_of_list(&node_Y);
linked_list.insert_node_at_end_of_list(&node_Z);
linked_list.print(output);
linked_list.remove_nodes_with_key("node_Y");
linked_list.print(output);
}
/**
* Unit Test # 4: LINKED_LIST constructor, insert method, remove method, print method, and destructor.
*/
void unit_test_4(std::ostream & output)
{
output << "\n\n************************************************";
output << "\nUnit Test # 4: LINKED_LIST constructor, insert method, remove method, print method, and destructor.";
output << "\n************************************************";
output << "\nLINKED_LIST linked_list;";
output << "\nNODE n0 = { key : \"red\", next : NULL };";
output << "\nNODE n1 = { key : \"blue\", next : NULL };";
output << "\nNODE n2 = { key : \"green\", next : NULL };";
output << "\nNODE n3 = { key : \"red\", next : NULL };";
output << "\nNODE n4 = { key : \"green\", next : NULL };";
output << "\nNODE n5 = { key : \"red\", next : NULL };";
output << "\nNODE n6 = { key : \"red\", next : NULL };";
output << "\nNODE n7 = { key : \"red\", next : NULL };";
output << "\nlinked_list.insert_node_at_end_of_list(&n0);";
output << "\nlinked_list.insert_node_at_end_of_list(&n1);";
output << "\nlinked_list.insert_node_at_end_of_list(&n2);";
output << "\nlinked_list.insert_node_at_end_of_list(&n3);";
output << "\nlinked_list.insert_node_at_end_of_list(&n4);";
output << "\nlinked_list.insert_node_at_end_of_list(&n5);";
output << "\nlinked_list.insert_node_at_end_of_list(&n6);";
output << "\nlinked_list.insert_node_at_end_of_list(&n7);";
output << "\nlinked_list.print(output);";
output << "\nlinked_list.remove_nodes_with_key(\"red\");";
output << "\nlinked_list.print(output);";
output << "\nlinked_list.remove_nodes_with_key(\"green\");";
output << "\nlinked_list.print(output);";
output << "\nlinked_list.remove_nodes_with_key(\"blue\");";
output << "\nlinked_list.print(output);";
LINKED_LIST linked_list;
NODE n0 = { key : "red", next : NULL };
NODE n1 = { key : "blue", next : NULL };
NODE n2 = { key : "green", next : NULL };
NODE n3 = { key : "red", next : NULL };
NODE n4 = { key : "green", next : NULL };
NODE n5 = { key : "red", next : NULL };
NODE n6 = { key : "red", next : NULL };
NODE n7 = { key : "red", next : NULL };
linked_list.insert_node_at_end_of_list(&n0);
linked_list.insert_node_at_end_of_list(&n1);
linked_list.insert_node_at_end_of_list(&n2);
linked_list.insert_node_at_end_of_list(&n3);
linked_list.insert_node_at_end_of_list(&n4);
linked_list.insert_node_at_end_of_list(&n5);
linked_list.insert_node_at_end_of_list(&n6);
linked_list.insert_node_at_end_of_list(&n7);
linked_list.print(output);
linked_list.remove_nodes_with_key("red");
linked_list.print(output);
linked_list.remove_nodes_with_key("green");
linked_list.print(output);
linked_list.remove_nodes_with_key("blue");
linked_list.print(output);
}
/* program entry point */
int main()
{
// Declare a file output stream object.
std::ofstream file;
/**
* If linked_list_driver_output.txt does not already exist in the same directory as linked_list_driver.cpp,
* create a new file named linked_list_driver_output.txt.
*
* Open the plain-text file named linked_list_driver_output.txt
* and set that file to be overwritten with program data.
*/
file.open("linked_list_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);
// 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/linked_list_driver_output.txt" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/linked_list_driver_output.txt</a></p>
<hr />
<pre>--------------------------------
Start Of Program
--------------------------------
************************************************
Unit Test # 0: LINKED_LIST constructor, print method, and destructor.
************************************************
LINKED_LIST linked_list;
linked_list.print(output);
--------------------------------------------------------------------------------------------------
this = 0x7fff55749340. // 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.
&head = 0x7fff55749340. // 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.
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(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(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).
head = 0x5647196a14b0. // 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).
head -> key = HEAD. // The arrow operator returns the string type property named key of the NODE type variable which head points to.
head -> next = 0. // The arrow operator returns the pointer-to-NODE type property named next of the NODE type variable which head points to.
get_number_of_nodes_in_list() = 1.
// p is a pointer to a NODE type variable.
LINKED_LIST := {
NODE_0 := {
p := 0x5647196a14b0.
p -> key = HEAD.
p -> next = 0.
}.
}.
--------------------------------------------------------------------------------------------------
************************************************
Unit Test # 1: LINKED_LIST constructor, insert method, print method, and destructor.
************************************************
LINKED_LIST linked_list;
NODE node;
node.key = "unit_test_1";
node.next = NULL;
linked_list.insert_node_at_end_of_list(&node);
linked_list.print(output);
--------------------------------------------------------------------------------------------------
this = 0x7fff55749318. // 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.
&head = 0x7fff55749318. // 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.
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(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(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).
head = 0x5647196a14b0. // 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).
head -> key = HEAD. // The arrow operator returns the string type property named key of the NODE type variable which head points to.
head -> next = 0x7fff55749320. // The arrow operator returns the pointer-to-NODE type property named next of the NODE type variable which head points to.
get_number_of_nodes_in_list() = 2.
// p is a pointer to a NODE type variable.
LINKED_LIST := {
NODE_0 := {
p := 0x5647196a14b0.
p -> key = HEAD.
p -> next = 0x7fff55749320.
}.
NODE_1 := {
p := 0x7fff55749320.
p -> key = unit_test_1.
p -> next = 0.
}.
}.
--------------------------------------------------------------------------------------------------
************************************************
Unit Test # 2: LINKED_LIST constructor, insert method, print method, and destructor.
************************************************
LINKED_LIST linked_list;
NODE node_A = { key : "node_A", next : NULL };
NODE node_B = { key : "node_B", next : NULL };
NODE node_C = { key : "node_C", next : NULL };
linked_list.insert_node_at_end_of_list(&node_A);
linked_list.insert_node_at_end_of_list(&node_B);
linked_list.insert_node_at_end_of_list(&node_C);
output < key = HEAD. // The arrow operator returns the string type property named key of the NODE type variable which head points to.
head -> next = 0x7fff557492c0. // The arrow operator returns the pointer-to-NODE type property named next of the NODE type variable which head points to.
get_number_of_nodes_in_list() = 4.
// p is a pointer to a NODE type variable.
LINKED_LIST := {
NODE_0 := {
p := 0x5647196a14b0.
p -> key = HEAD.
p -> next = 0x7fff557492c0.
}.
NODE_1 := {
p := 0x7fff557492c0.
p -> key = node_A.
p -> next = 0x7fff557492f0.
}.
NODE_2 := {
p := 0x7fff557492f0.
p -> key = node_B.
p -> next = 0x7fff55749320.
}.
NODE_3 := {
p := 0x7fff55749320.
p -> key = node_C.
p -> next = 0.
}.
}.
--------------------------------------------------------------------------------------------------
************************************************
Unit Test # 3: LINKED_LIST constructor, insert method, remove method, print method, and destructor.
************************************************
LINKED_LIST linked_list;
NODE node_X = { key : "node_X", next : NULL };
NODE node_Y = { key : "node_Y", next : NULL };
NODE node_Z = { key : "node_Z", next : NULL };
linked_list.insert_node_at_end_of_list(&node_X);
linked_list.insert_node_at_end_of_list(&node_Y);
linked_list.insert_node_at_end_of_list(&node_Z);
linked_list.print(output);
linked_list.remove_nodes_with_key("node_Y");
linked_list.print(output);
--------------------------------------------------------------------------------------------------
this = 0x7fff55749298. // 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.
&head = 0x7fff55749298. // 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.
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(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(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).
head = 0x5647196a14b0. // 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).
head -> key = HEAD. // The arrow operator returns the string type property named key of the NODE type variable which head points to.
head -> next = 0x7fff557492c0. // The arrow operator returns the pointer-to-NODE type property named next of the NODE type variable which head points to.
get_number_of_nodes_in_list() = 4.
// p is a pointer to a NODE type variable.
LINKED_LIST := {
NODE_0 := {
p := 0x5647196a14b0.
p -> key = HEAD.
p -> next = 0x7fff557492c0.
}.
NODE_1 := {
p := 0x7fff557492c0.
p -> key = node_X.
p -> next = 0x7fff557492f0.
}.
NODE_2 := {
p := 0x7fff557492f0.
p -> key = node_Y.
p -> next = 0x7fff55749320.
}.
NODE_3 := {
p := 0x7fff55749320.
p -> key = node_Z.
p -> next = 0.
}.
}.
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
this = 0x7fff55749298. // 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.
&head = 0x7fff55749298. // 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.
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(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(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).
head = 0x5647196a14b0. // 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).
head -> key = HEAD. // The arrow operator returns the string type property named key of the NODE type variable which head points to.
head -> next = 0x7fff557492c0. // The arrow operator returns the pointer-to-NODE type property named next of the NODE type variable which head points to.
get_number_of_nodes_in_list() = 3.
// p is a pointer to a NODE type variable.
LINKED_LIST := {
NODE_0 := {
p := 0x5647196a14b0.
p -> key = HEAD.
p -> next = 0x7fff557492c0.
}.
NODE_1 := {
p := 0x7fff557492c0.
p -> key = node_X.
p -> next = 0x7fff55749320.
}.
NODE_2 := {
p := 0x7fff55749320.
p -> key = node_Z.
p -> next = 0.
}.
}.
--------------------------------------------------------------------------------------------------
************************************************
Unit Test # 4: LINKED_LIST constructor, insert method, remove method, print method, and destructor.
************************************************
LINKED_LIST linked_list;
NODE n0 = { key : "red", next : NULL };
NODE n1 = { key : "blue", next : NULL };
NODE n2 = { key : "green", next : NULL };
NODE n3 = { key : "red", next : NULL };
NODE n4 = { key : "green", next : NULL };
NODE n5 = { key : "red", next : NULL };
NODE n6 = { key : "red", next : NULL };
NODE n7 = { key : "red", next : NULL };
linked_list.insert_node_at_end_of_list(&n0);
linked_list.insert_node_at_end_of_list(&n1);
linked_list.insert_node_at_end_of_list(&n2);
linked_list.insert_node_at_end_of_list(&n3);
linked_list.insert_node_at_end_of_list(&n4);
linked_list.insert_node_at_end_of_list(&n5);
linked_list.insert_node_at_end_of_list(&n6);
linked_list.insert_node_at_end_of_list(&n7);
linked_list.print(output);
linked_list.remove_nodes_with_key("red");
linked_list.print(output);
linked_list.remove_nodes_with_key("green");
linked_list.print(output);
linked_list.remove_nodes_with_key("blue");
linked_list.print(output);
--------------------------------------------------------------------------------------------------
this = 0x7fff557491a8. // 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.
&head = 0x7fff557491a8. // 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.
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(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(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).
head = 0x5647196a14b0. // 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).
head -> key = HEAD. // The arrow operator returns the string type property named key of the NODE type variable which head points to.
head -> next = 0x7fff557491d0. // The arrow operator returns the pointer-to-NODE type property named next of the NODE type variable which head points to.
get_number_of_nodes_in_list() = 9.
// p is a pointer to a NODE type variable.
LINKED_LIST := {
NODE_0 := {
p := 0x5647196a14b0.
p -> key = HEAD.
p -> next = 0x7fff557491d0.
}.
NODE_1 := {
p := 0x7fff557491d0.
p -> key = red.
p -> next = 0x7fff55749200.
}.
NODE_2 := {
p := 0x7fff55749200.
p -> key = blue.
p -> next = 0x7fff55749230.
}.
NODE_3 := {
p := 0x7fff55749230.
p -> key = green.
p -> next = 0x7fff55749260.
}.
NODE_4 := {
p := 0x7fff55749260.
p -> key = red.
p -> next = 0x7fff55749290.
}.
NODE_5 := {
p := 0x7fff55749290.
p -> key = green.
p -> next = 0x7fff557492c0.
}.
NODE_6 := {
p := 0x7fff557492c0.
p -> key = red.
p -> next = 0x7fff557492f0.
}.
NODE_7 := {
p := 0x7fff557492f0.
p -> key = red.
p -> next = 0x7fff55749320.
}.
NODE_8 := {
p := 0x7fff55749320.
p -> key = red.
p -> next = 0.
}.
}.
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
this = 0x7fff557491a8. // 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.
&head = 0x7fff557491a8. // 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.
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(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(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).
head = 0x5647196a14b0. // 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).
head -> key = HEAD. // The arrow operator returns the string type property named key of the NODE type variable which head points to.
head -> next = 0x7fff55749200. // The arrow operator returns the pointer-to-NODE type property named next of the NODE type variable which head points to.
get_number_of_nodes_in_list() = 4.
// p is a pointer to a NODE type variable.
LINKED_LIST := {
NODE_0 := {
p := 0x5647196a14b0.
p -> key = HEAD.
p -> next = 0x7fff55749200.
}.
NODE_1 := {
p := 0x7fff55749200.
p -> key = blue.
p -> next = 0x7fff55749230.
}.
NODE_2 := {
p := 0x7fff55749230.
p -> key = green.
p -> next = 0x7fff55749290.
}.
NODE_3 := {
p := 0x7fff55749290.
p -> key = green.
p -> next = 0.
}.
}.
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
this = 0x7fff557491a8. // 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.
&head = 0x7fff557491a8. // 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.
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(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(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).
head = 0x5647196a14b0. // 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).
head -> key = HEAD. // The arrow operator returns the string type property named key of the NODE type variable which head points to.
head -> next = 0x7fff55749200. // The arrow operator returns the pointer-to-NODE type property named next of the NODE type variable which head points to.
get_number_of_nodes_in_list() = 2.
// p is a pointer to a NODE type variable.
LINKED_LIST := {
NODE_0 := {
p := 0x5647196a14b0.
p -> key = HEAD.
p -> next = 0x7fff55749200.
}.
NODE_1 := {
p := 0x7fff55749200.
p -> key = blue.
p -> next = 0.
}.
}.
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
this = 0x7fff557491a8. // 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.
&head = 0x7fff557491a8. // 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.
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(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(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).
head = 0x5647196a14b0. // 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).
head -> key = HEAD. // The arrow operator returns the string type property named key of the NODE type variable which head points to.
head -> next = 0. // The arrow operator returns the pointer-to-NODE type property named next of the NODE type variable which head points to.
get_number_of_nodes_in_list() = 1.
// p is a pointer to a NODE type variable.
LINKED_LIST := {
NODE_0 := {
p := 0x5647196a14b0.
p -> key = HEAD.
p -> next = 0.
}.
}.
--------------------------------------------------------------------------------------------------
--------------------------------
End Of Program
--------------------------------</pre>
<hr />
<p>This web page was last updated on 06_NOVEMBER_2024. The content displayed on this web page is licensed as <a style="background: #000000;color: #ff9000" href="https://karlinaobject.wordpress.com/public_domain/" target="_blank" rel="noopener">PUBLIC_DOMAIN</a> intellectual property.</p>
<hr />