struct node {
char name[20];
int age;
int height;
node* next; // Pointer to the next node
};
node* startPTR = NULL; // global
void addNode_AT_END() {
node *temp1;
node *temp2;
temp1 = new node;
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;
if( startPTR == NULL) {
startPTR = temp1;
} else {
temp2 = startPTR;
while( temp2->next != NULL )
temp2 = temp2->next;
temp2->next = temp1;
}
}
In this snippet , when the function is called the third time , else part works. The address of the startPTR is assigned to the temp2. Now what does temp2->next contain when the condition is being checked in the while loop? (during third call) If you say it contains the address of the second node , please tell how does it know the address of the second node,because we had assigned the address of the second node to the first node during the second call to the function using the statement temp2->next = temp1 but because of it’s local scope we loose the address.
This is the way i am currently thinking.Please explain how the condition is checked during the third call to the function and how the linked list is being formed?
It’s only the pointer variables
temp1andtemp2that are of local scope. The actual linked-list nodes (of typestruct node) are allocated on the heap via the call tonew. Heap allocated data persists until it’s free’d via a call todelete.This means that the line
temp2->next = temp1is storing the address of the new node that’s appended to the tail of the list, and this information will be available on subsequent calls to the functionaddNode_AT_END.The code seems to have a variety of issues – you’re using global variables, unless there are subsequent calls to
deletesomewhere you have memory leaks, there’s already the std container classstd::listthat’s available…Hope this helps.
EDIT: Regarding your comment – when you make a call
new nodeyou are constructing anodeobject on the heap. This object will available for use until a subsequent call todeleteis made.When you make the call
temp2->next = temp1the following is true:temp2points to the address in memory where the last node in the list is stored (this node would have been created by the call tonewon the last iteration).temp1is assigned as thenextpointer for the last node in the list (the data “pointed to” bytemp2). This means that the address is stored on the heap, not within the localtemp2pointer varaible.When your function exits, yes the local pointer variables
temp1andtemp2go out of scope, but the heap allocated linked-list nodes are not destroyed – and this is where the addresses are stored.EDIT: Second comment – in the
elsebranch of the function, the pointertemp2is initialised to point to the head of the list with the linetemp2 = startPTR;The next lines (the
whileloop) traverse the linked-list from the head node until the pointertemp2points to the last node in the list (untiltemp2->next = NULL)At this stage the new node is appended to the list, as discussed above.