Insert Node from End in Linked List
Advertisements
Insert Node from Back in Linked List in C++
Insert data from back is very similar to the insert from front in the linked list. Here the extra job is to find the last node of the linked list.
Syntax
node *temp1; // create a temporary node temp1=(node*)malloc(sizeof(node)); // allocate space for node temp1 = head; // transfer the address of 'head' to 'temp1' while(temp1->next!=NULL) // go to the last node temp1 = temp1->next;//tranfer the address of 'temp1->next' to 'temp1'
Now, Create a temporary node node *temp and allocate space for it. Then place info to temp->data, so the first field of the node node *temp is filled. node *temp will be the last node of the linked list. For this reason,temp->next will be NULL. To create a connection between linked list and the new node, the last node of the existing linked list node *temp1`s second field temp1->next is pointed to node *temp.
Syntax
node *temp; // create a temporary node temp = (node*)malloc(sizeof(node)); // allocate space for node temp->data = info; // store data(first field) temp->next = NULL; // second field will be null(last node) temp1->next = temp; // 'temp' node will be the last node
Google Advertisment