Insertion in Singly Linked List in C++

Insertion in Circular Linked List

What is insertion in singly linked in CPP programming?

Insertion in singly linked in C++ in general words can be understood as insertion of a new node in a singly linked list that is already created. We can perform three different types of insertion operations on singly linked list, these different types of insertion operations are:-

  • Insertion at beginning of the circular linked list.
  • Inserting a new node at the specific position.
  • Insertion at end in circular linked list.

In this article, we will discuss what is the difference between these types of insertion and how to perform them over a singly linked list.

Types of insertion in singly linked list

1.) Insertion at beginning:

In this type of insertion in singly linked list a new node is added before the head of the list i.e. before the first node of the list.

2.) Insertion at specific position:

Here we insert a new node at specific position by traversing till the desired position and inserting new node there.

3.) Insertion at end:

We insert new node after the last node i.e. after the tail node in the insertion at end singly linked list.

Types of insertion in circular linked list

Insertion at beginning

Insertion at beginning

How to perform insertion at beginning in singly linked list?

To perform insertion at beginning in singly linked list we will use the following steps:-

  1. We will create a space at beginning, before head node.
  2. In this space created we will built a new node.
  3. After that we will but the data in the new node that we want to insert.
void insertatBegin(int num) 
{
    struct node *frntNode;
    frntNode = (struct node*)malloc(sizeof(struct node));
    if(frntNode == NULL)
    {
        cout<<" Memory can not be allocated"; } else { frntNode->num = num;         
        frntNode->nextptr = stnode; 
        stnode = frntNode; 
    }
}
Insertin at beginning in singly linked list in C++

Insertion at specific position

Insertion at specific position

How to perform insertion at specific position in singly linked list?

To perform insertion at specific position in singly linked list we will use the following steps:-

  1. We will traverse till the specific position in the list.
  2. Then We will create some blank memory for the node there.
  3. We will create a new node in that memory and put the data in the node that we want to enter.
void insertSpecific(int num, int pos)
{
    int i;
    struct node *frntNode, *tmp;
    frntNode = (struct node*)malloc(sizeof(struct node));
    if(frntNode == NULL)
    {
       cout<<"Memory can not be allocated."; } else { frntNode->num = num;  
        frntNode->nextptr = NULL;
        tmp = stnode;
        for(i=2; i<=pos-1; i++) { tmp = tmp->nextptr;
 
            if(tmp == NULL)
                break;
        }
        if(tmp != NULL)
        {
            frntNode->nextptr = tmp->nextptr;  
            tmp->nextptr = frntNode;   
        }
        else
        {
            cout<<"Data cannot be entered in that particular position\n";
        }
    }
}
Algorithm for insertion in between the nodes in singly linked list in c++

Insertion at end

Insertion at end

How to perform insertion at end in singly linked list?

To perform insertion at end in singly linked list we will use the following steps:-

After creating a linked list with some data in it we will :-

  1. Traverse till the end of the list.
  2. After that we will create a new node after the last node.
  3. Then we will put the data in the node that we want to insert.
void insertatEnd(int num)
{
struct node *frntNode, *tmp;
frntNode = (struct node*)malloc(sizeof(struct node));
if(frntNode == NULL)
{
cout<<"Memory can not be allocated.";
}
else
{
frntNode->num = num;
frntNode->nextptr = NULL;
tmp = stnode;
while(tmp->nextptr != NULL)
tmp = tmp->nextptr;
tmp->nextptr = frntNode;
}
Algorithm for Insertion at end in singly linked list in c++
Linked Lists in C++ meme