Reading


Linked List

if we have an array based stack of 5 integers

we will have a index for the top of the stack
and an array of 5 integers

using the index operator [] we can get the address of, print the value or change the value for any of the 5 integers

they are all stored in memory one after the other

data[0]: address: 0x55f51f2fdb70 value: 5
data[1]: address: 0x55f51f2fdb74 value: 10
data[2]: address: 0x55f51f2fdb78 value: 15
data[3]: address: 0x55f51f2fdb7c value: 20
data[4]: address: 0x55f51f2fdb80 value: 25

if you look at the addresses they are for one constecutive piece of memory , they are all 4 bytes apart 4 bytes for each integer



now if we look a the nodes and their addresses from out linked list you will see that they are not in order

node:0x55729b641820 data:25 next: 0x55729b641800
node:0x55729b641800 data:20 next: 0x55729b6417e0
node:0x55729b6417e0 data:15 next: 0x55729b641790
node:0x55729b641790 data:10 next: 0x55729b640ed0
node:0x55729b640ed0 data:5 next: 0



this is because the data is not stored in an array
each time a new value is added  a new node is created with new and the value is stored in the node
the node also conatains a pointer that we use to link to the next node in the sequence.


Look at this visualization
use the insert front function and you can see the methodology for creating your linked list

Linked List Demonstration