Insertion in doubly linked list after the specified node

Before making the required pointer adjustments, skip the required number of nodes to reach the mentioned node, to insert a node after that specified node, in the list. For this, we will use the below steps:

  • Use the below statement to allocate the memory for the new node.
    ptr = (struct node *)malloc(sizeof(struct node));
  • Use the below statements, to skip the required number of nodes to reach the specified node. Here, we are traversing the list by using the pointer temp.
    temp=head;  
        for(i=0;i<loc;i++)  
       {  
           temp = temp->next;  
           // the temp will be null if the list does not last long up to mentioned location
           if(temp == NULL)    
           {  
                 return;  
           }  
       }
  • At the end of the for a loop, the temp would point to the specified node. We will insert the new node after this node. But, before that, we need to make a few pointer adjustments. Use the below statement, to make the next pointer of ptr point to the next node of temp.
    ptr → next = temp → next;
  • Use the below statement, to make the prev of the new node ptr point to temp.
    ptr → prev = temp;
  • Use the below statement, to make the next pointer of temp point to the new node ptr.
    temp → next = ptr;
  • Use the below statement, to make the previous pointer of the next node of temp point to the new node.
    temp → next → prev = ptr;

Algorithm:

  • Step 1:IF PTR = NULL
    Write OVERFLOW
    Go to Step 15
    [END OF IF]
  • Step 2: SET NEW_NODE = PTR
  • Step 3: SET PTR = PTR -> NEXT
  • Step 4: SET NEW_NODE -> DATA = VAL
  • Step 5: SET TEMP = START
  • Step 6: SET I = 0
  • Step 7: REPEAT 8 to 10 until I
  • Step 8: SET TEMP = TEMP -> NEXT
  • STEP 9: IF TEMP = NULL
  • STEP 10:WRITE “LESS THAN DESIRED NO. OF ELEMENTS”
    GOTO STEP 15
    [END OF IF]
    [END OF LOOP]
  • Step 11: SET NEW_NODE -> NEXT = TEMP -> NEXT
  • Step 12: SET NEW_NODE -> PREV = TEMP
  • Step 13 : SET TEMP -> NEXT = NEW_NODE
  • Step 14: SET TEMP -> NEXT -> PREV = NEW_NODE
  • Step 15: EXIT

Example in C:

#include<stdio.h>  
#include<stdlib.h>  
void insertAtSpecified(int);  
void create(int);  
struct node  
{  
    int data;  
    struct node *next;  
    struct node *prev;  
};  
struct node *head;  
void main ()  
{  
    int choice,item,loc;  
    do   
    {  
        printf("\nEnter the element to insert:\n");  
        scanf("%d",&item);  
        if(head == NULL)  
        {  
            create(item);  
        }  
        else  
        {  
            insertAtSpecified(item);  
        }  
        printf("\nPress 1 to insert more elements.\n");  
        scanf("%d",&choice);  
    }while(choice == 1);  
}  
void create(int item)  
    {  
   struct node *ptr = (struct node *)malloc(sizeof(struct node));  
   if(ptr == NULL)  
   {  
       printf("\nOVERFLOW");  
   }  
   else  
   {  
 
 
   if(head==NULL)  
   {  
       ptr->next = NULL;  
       ptr->prev=NULL;  
       ptr->data=item;  
       head=ptr;  
   }  
   else   
   {  
       ptr->data=item;printf("\nPress 1 to insert more elements.\n");  
       ptr->prev=NULL;  
       ptr->next = head;  
       head->prev=ptr;  
       head=ptr;  
   }  
    printf("\nNode Inserted Successfully!!\n");  
}  
 
}  
void insertAtSpecified(int item)  
{  
 
   struct node *ptr = (struct node *)malloc(sizeof(struct node));  
   struct node *temp;   
   int i, loc;   
   if(ptr == NULL)  
   {  
       printf("\n OVERFLOW");  
   }  
   else  
   {  
       printf("\nEnter the location:\n");  
       scanf("%d",&loc);  
       temp=head;  
       for(i=0;i<loc;i++)  
       {  
           temp = temp->next;  
           if(temp == NULL)  
           {  
               printf("\nNode can't be inserted.\n");  
               return;  
           }  
       }  
       ptr->data = item;  
       ptr->next = temp->next;  
       ptr -> prev = temp;  
       temp->next = ptr;  
       temp->next->prev=ptr;  
       printf("Node Inserted Successfully!!\n");  
   }  
}

Output:

Enter the element to insert:
2
 
Node Inserted Successfully!!
 
Press 1 to insert more elements.
1
 
Enter the element to insert:
4
 
Enter the location:
0
Node Inserted Successfully!!
 
Press 1 to insert more elements.
1
 
Enter the element to insert:
6
 
Enter the location:
1
Node Inserted Successfully!!
 
Press 1 to insert more elements.
1
 
Enter the element to insert:
8
 
Enter the location:
4
 
Node can't be inserted.
 
Press 1 to insert more elements.
5