Insert a node at the end of Singly Link List
Let us see the tutorial of “Insertion of a node at the end (rightmost position) of the Singly Link List.
Demonstration
C++ Source code for Insertion of a node at the end of Singly Link List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
#include<iostream> #include <stdlib.h> using namespace std; struct node { int number; //Data of the node struct node *nextptr; //Address of the node } *starting; void createNodeList(int n); //function to create the list void InsertRighttEnd(int number); //function to insert node at the end void displayList(); //function to display the list int main() { int n,number; cout<<"\n\n\n Linked List : Insert a new node at the end of a Singly Linked List :\n"; cout<<" Please input the the number of nodes : "; cin>>n; createNodeList(n); cout<<"\n Data entered by the user in the list is: \n"; displayList(); cout<<"\n Please input the data to insert at the end of the list : "; cin>>number; InsertRighttEnd(number); cout<<"\n Data, after inserted in the list are : \n"; displayList(); return 0; } void createNodeList(int n) { struct node *final, *tmp; int number, i; starting = (struct node *)malloc(sizeof(struct node)); if(starting == NULL) //check whether the starting is NULL and if so no memory allocation { cout<<" Memory can not be allocated."; } else { // reads data for the node through keyboard cout<<" Please input the data for node 1 : "; cin>>number; starting-> number = number; starting-> nextptr = NULL; //Links the address field to NULL tmp = starting; //Creates n nodes and adds to linked list for(i=2; i<=n; i++) { final = (struct node *)malloc(sizeof(struct node)); if(final == NULL) //check whether the final is NULL and if so no memory allocation { cout<<" Memory can not be allocated."; break; } else { cout<<" Please input the data for node 2 : "; cin>>number; final->number = number; // links the number field of final with number final->nextptr = NULL; // links the address field of final with NULL tmp->nextptr = final; // links previous node i.e. tmp to the final tmp = tmp->nextptr; } } } } void InsertRighttEnd(int number) { struct node *final, *tmp; final = (struct node*)malloc(sizeof(struct node)); if(final == NULL) { cout<<" Memory can not be allocated."; } else { final->number = number; //Links the data part final->nextptr = NULL; tmp = starting; while(tmp->nextptr != NULL) { tmp = tmp->nextptr; } tmp->nextptr = final; //Links the address part } } void displayList() { struct node *tmp; if(starting == NULL) { cout<<" No data found in the empty list."; } else { tmp = starting; while(tmp != NULL) { cout<<tmp->number<<","; // prints the data of current node tmp = tmp->nextptr; // advances the position of current node } } } |
Output
Insert a node at second last position in a singly link list
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
#include<iostream> #include <stdlib.h> using namespace std; struct node { int number; //Data of the node struct node *nextptr; //Address of the node } *starting; void createNodeList(int n); //function to create a singly link list void InsertSecondlastEnd(int number); //function to insert node at the second last position at the end void displayList(); //function to display a singly link list int main() { int n,number; cout<<"\n\n\n Linked List : Insert a new node at the second last position at the end of a Singly Linked List :\n"; cout<<" Please input the the number of nodes : "; cin>>n; createNodeList(n); cout<<"\n Data entered by the user in the list is: \n"; displayList(); cout<<"\n Please input the data to insert at the second last position of the link list : "; cin>>number; InsertSecondlastEnd(number); cout<<"\n Data, after inserted in the link list is: \n"; displayList(); return 0; } void createNodeList(int n) { struct node *final, *tmp; int number, i; starting = (struct node *)malloc(sizeof(struct node)); if(starting == NULL) //check whether the starting is NULL and if so no memory allocation { cout<<" Memory can not be allocated."; } else { // reads data for the node through keyboard cout<<" Please input the data for node 1 : "; cin>>number; starting-> number = number; starting-> nextptr = NULL; //Links the address field to NULL tmp = starting; //Creates n nodes and adds to linked list for(i=2; i<=n; i++) { final = (struct node *)malloc(sizeof(struct node)); if(final == NULL) //check whether the final is NULL and if so no memory allocation { cout<<" Memory can not be allocated."; break; } else { cout<<" Please input the data for node "<<i<<" : "; cin>>number; final->number = number; // links the number field of final with number final->nextptr = NULL; // links the address field of final with NULL tmp->nextptr = final; // links previous node i.e. tmp to the final tmp = tmp->nextptr; } } } } void InsertSecondlastEnd(int number) { struct node *final, *tmp, *secondlast; final = (struct node*)malloc(sizeof(struct node)); if(final == NULL) { cout<<" Memory can not be allocated."; } else { final->number = number; //Links the data part final->nextptr = NULL; tmp = starting; while(tmp->nextptr != NULL) {secondlast=tmp; tmp = tmp->nextptr; } secondlast->nextptr = final; //Links the address part final->nextptr=tmp; } } void displayList() { struct node *tmp; if(starting == NULL) { cout<<" No data found in the empty list."; } else { tmp = starting; while(tmp != NULL) { cout<<tmp->number<<","; // prints the data of current node tmp = tmp->nextptr; // advances the position of current node } } } |
Output
Linked List : Insert a new node at the end of a Singly Linked List :
Please input the the number of nodes : 6
Please input the data for node 1 : 44
Please input the data for node 2 : 55
Please input the data for node 3 : 66
Please input the data for node 4 : 77
Please input the data for node 5 : 88
Please input the data for node 6 : 99
Data entered by the user in the list is:
44, 55, 66, 77, 88, 99
Please input the data to insert at the second last position of the link list :
11
Data, after inserted in the link list is:
44, 55, 66, 77, 88, 11, 99