Insert a node at a specific position in a linked list in C

C Program to Insert a node in a Singly Linked List at a given position

On this page, we will look at different ways and methods to write a C Program for Insertion in a Singly Linked List at a given position in C Language.

Insert in B-Tree

Method Overview

Method for Linked List Insertion at specific position in C

  • If the user wants to insert a node at nth position
  • First check if the position is in the range [1, size] otherwise it is an invalid position.
  • Create the memory and assign data value to this new Node
  • Visit the (n-1)th node and change its next pointer to this new Node
  • Assign the next pointer of this new Node to (n+1)th node

Also, take care if pos == 1 as we will need to change the head also in this case.

Insert a node at a specific position

Program for Singly Linked List Insertion at a specific position in C

Run
// Program for Singly Linked List Insertion at a given position in C
#include<stdio.h>
#include<stdlib.h>

struct Node
{
  int data;
  struct Node *next;
};
// current size of Linked List
int size = 0;

void insert (struct Node **head, int data)
{
  struct Node *new_node = (struct Node *) malloc (sizeof (struct Node));

  new_node->data = data;
  new_node->next = *head;
  *head = new_node;
  size++;
}

// method to insert at a given position
void insertPosition (int pos, int data, struct Node **head)
{

  struct Node *new_node = (struct Node *) malloc (sizeof (struct Node));
  new_node->data = data;
  new_node->next = NULL;

  // Invalid positions
  if (pos < 1 || pos > size + 1)
    printf ("Invalid\n");

  // inserting first node
  else if (pos == 1)
    {
      new_node->next = *head;
      *head = new_node;
      size++;
    }

  else
    {
      struct Node *temp = *head;

      // traverse till the current (pos-1)th node
      while (--pos > 1)
	{
	  temp = temp->next;
	}
      new_node->next = temp->next;
      temp->next = new_node;
      size++;
    }
}

void display (struct Node *node)
{

  printf ("Linked List : ");

  // as linked list will end when Node is Null
  while (node != NULL)
    {
      printf ("%d ", node->data);
      node = node->next;
    }
  printf ("\n");
}

int main ()
{
  struct Node *head = NULL;

  insert (&head, 70);
  insert (&head, 60);
  insert (&head, 40);
  insert (&head, 30);
  insert (&head, 10);

  display (head);

  // Inserts data: 20 at 2nd position
  insertPosition (2, 20, &head);

  // Inserts data: 50 at 5th position
  insertPosition (5, 50, &head);

  // Inserts data: 80 at 8th position
  insertPosition (8, 80, &head);

  display (head);

  return 0;
}

Output

Linked List : 10 30 40 60 70 
Linked List : 10 20 30 40 50 60 70 80

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java