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.

Singly Link List - Insertion at the end of

Demonstration

how to add a node at the end of a Singly Link List

how to insert a node in Link List

how to insert a node at the end of a Link List

C++ Source code for Insertion of a node at the end of Singly Link List

#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 the end of Singly Link List

 


Insert a node at second last position in a singly link list

Insert a node at second last position in a singly link list

#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

 

 

Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.