This is a program trying to make a linked list.
#include <iostream>
using namespace std;
struct node {
char name[20];
int age;
int height;
node* next; // Pointer to the next node
};
node* startPTR = NULL; // Start Pointer (root)
// This pointer permanantly points to the start of the list
// Initially there are no nodes
void addNode_AT_END(); // prototype for the function that 'adds nodes at the end'
int main() {
do {
addNode_AT_END();
cout << "Add more ?";
char ch;
cin >> ch;
} while( ch == 'y');
}
void addNode_AT_END() {
node *temp1;
node *temp2;
temp1 = new node; // We declare space for a pointer item and assign a temporary pointer to it
//*temp1 is the node that it points to
cout << "Enter the name : ";
cin >> temp1->name;
cout << endl << "Enter the age : ";
cin >> temp1->age;
cout << endl << "Enter height : ";
cin >> temp1->height;
temp1->next = NULL; // indicates that this node when inserted in the list will be the last node
if( startPTR == NULL) {
startPTR = temp1; // In the empty list last node will be the first node
} else {
temp2 = startPTR;
while( temp2->next != NULL )
temp2 = temp2->next;
temp2->next = temp1;
}
}
From this yet to be completed program this is what i understand :

If the figure after the second call to the function addNode_AT_END is true, then what does temp2->next in the statement while( temp2->next != NULL ) contain ?
Your diagrams are incorrect. start = temp2 does means that start and temp2 pointers both point to the same node. Your diagram shows the next field of the temp2 pointer holds the address of start . Also after doing
start->next = temp1does not mean that if you get some new node value intemp1(in the next function call), thestart->nextwill still keep pointing to the new value just allocated intemp1. It will hold the old value that was in before you overwrote it with the new one.start->next = temp1simply copies the value intemp1ie. an address to the variable (pointer variable) the next component of the node pointed by start which isstart->next. After that there is no connection between start andtemp1.In linked list context “temp1 —-> temp2” means the
nextfield of the node whose address is stored intemp1, holds the address of the node with an address which was held or is held bytemp2. Now after you change the value of the pointer variabletemp2does not change thenextfield of the node held at address stored intemp1.temp1->nextstill contains the value which it stored before.The next links does not point to some variable name, that is,
start->next = tempwill not makestartnode’s next node always pointing to the whatever nodetemp1contains, but itstart->nextwill contain the address whichtemp1stored at the time of the assignment.note that by saying “start is pointing to temp1” means that the address
will break when
temp2->next = NULLwhich means thattemp2points to the last node of the list. At this pointtemp2->next = temp1links the newly allocated node after the node currently pointed bytemp2. Which is simply adds the new node at the end.UPDATE
First time:
Second time:
Third time:
The pointers are the means to travel/traverse through the list. The starting address of the list is held in a pointer
start. As the next field of each node points to the next node, if we get thestartnode, then by following the next fields sequentially we can visit each node.temp1andtemp2are pointers with which the traversals are done, they act as temporary pointers,temp1is used to hold the newly allocated node, andtemp2is used to travel through the list by following thenextlinks till the last, when the last link is found (detected by the NULL pointer in the next field), the NULL link of this last node is replaced by the newly allocated node held bytemp1. As now the node held bytemp1is linked/added to the end of the list,temp1is reused to hold another new node.