Helo friends!!
I m having problems in understanding that how every node is added at last.Could anyone explain me this in much better manner.
Well here it is....
we use a local "reference pointer" which always points to the last
pointer in the list instead of to the last node. All additions to the list are made by following the reference pointer. The reference pointer starts off pointing to the head pointer.
Later, it points to the .next field inside the last node in the list.:?:
struct node* BuildWithLocalRef()
{
struct node* head = NULL;
struct node** lastPtrRef= &head; // Start out pointing to the head pointer
int i;
for (i=1; i<6; i++) {
Push(lastPtrRef, i); // Add node at the last pointer in the list
lastPtrRef= &((*lastPtrRef)->next); // Advance to point to the
// new last pointer
}
// head == {1, 2, 3, 4, 5};
return(head);
}