C Program for Insertion at the nth node of the Singly Linked List

Insertion at the nth node of the Singly Linked List

Did you know Linked list is one of the most important data structures, where the data is dynamic in nature.The implementation of a linked list in C is done using pointers.In the linked list if we have the first node then we can access any node of the linked list but there is no way of traversing the previous node and this is the disadvantage of singly linked list.To insert the new node at nth position in singly linked list is a little bit complicated. We need to first break the existing linked list and then adjust new node in between them.
For Example:-
Input : 63->15->8->4, data = 12, position = 2
Output : 63->12->15->8->4

C Program for Insertion at the nth node of the Singly Linked List
Insertion at the nth node of the Singly Linked List using C

Steps required to insert nth node of the Singly Linked List are:-

  • Traverse the Linked list until position1 nodes.
  • Once all the position-1 nodes are traversed, allocate memory and the given data to the new node.
  • Point the next pointer of the new node to the next of current node.
  • Point the next pointer of current node to the new node.

Code for Constructing Node of the Singly Linked List:-

struct Node
{
int data;
struct Node* next;
};

Algorithm for C Program for inserting at the nth node of the Singly Linked List:-

STEP 1: IF ptr = NULL
Write Overflow
Goto STEP 12
End Of IF

STEP 2: SET new_node =ptr

STEP 3: new_node → data

STEP 4: SET temp = head

STEP 5: SET I = 0

STEP 6: REPEAT STEP 5 AND 6 UNTIL I

STEP 7: temp = temp → next

STEP 8: IF temp = NULL

Write desired node not found
Goto STEP 12
End Of IF
End Of LOOP

STEP 9: ptr → next = temp → next

STEP 10: temp → next=ptr

STEP 11: SET ptr=new_node

STEP 12: EXIT

C Program for insertion at the nth node of the Singly Linked List:-

#include <stdio.h>
#include <stdlib.h> 

struct node *head=NULL;
struct node
        {
    	int data;
    	struct node *next;
        };

void ins(int data)
        {
    	struct node *temp = (struct node*)malloc(sizeof(struct node));  //it will insert a new node to the start of the linked list
    	temp->data=data;
    	temp->next=head;
    	head=temp;
        }

void ins_at_pos_n(int data,int position)
    {
    	struct node *ptr = (struct node*)malloc(sizeof(struct node));
    	ptr->data=data;		//Creating a new node
    	int i;
    	struct node *temp=head;
    	if(position==1)
        {
        	ptr->next=temp;
    		head=ptr;
    		return;
    	}

    	for(i=1;i<position-1;i++)  //moving to the (n-1)th position node in the linked list
    	{
        	temp=temp->next;
    	}

    	ptr->next=temp->next;  //Make the newly created node point to next node of ptr temp
    	temp->next=ptr;  	//Make ptr temp point to newly created node in the linked list
}

void display()
{
    	struct node *temp=head;
    	printf("\nList: ");
    	while(temp!=NULL)
    	{
		    printf("\n%d ",temp->data);
            temp=temp->next;		
	}
}

int main()
{
    int i, n, pos, data;
    printf("Enter the number of nodes: \n");
    scanf("%d",&n);
    printf("Enter the data for the nodes: \n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&data);
        ins(data);
    }
    printf("Enter the data you want to insert in between the nodes: \n");
    scanf("%d",&data);
    printf("Enter the position at which you want to insert the nodes: \n");
    scanf("%d",&pos);
    if(pos>n)
    {
        printf("Enter a valid position: ");
    
    }
    else 
    {
        ins_at_pos_n(data,pos);
    	
    }
    display();
    return 0;
}
Output:-
Enter the number of nodes: 
5
Enter the data for the nodes:
44
5
22
6
95
Enter the data you want to insert in between the nodes:
100
Enter the position at which you want to insert the nodes:
4
List:
44
5
22
100
6
95