Insertion in between the nodes in doubly linked list in C++

How to insert element in between the nodes in doubly linked list in C++?

Doubly Linked List is a improved version of singly linked list which has two pointer variables and a single data variable, the two pointer variables help us in storing the address of the next node as well as the previous node, which thus helps us in traversing in both the directions in a doubly linked list. Here, in this article we will be learning how to code a C++ Program for inserting an element in the end of the Doubly Linked List.

Insertion in between the nodes in doubly linked list

Definition of doubly linked list in C++

struct Node 
{
  int Data;
  Struct Node* next;
  Struct Node* prev;
};

Steps to insert element in between the nodes in doubly linked list in C++

1.) Allocate node.

2.) Put the data.

3.) Make next of new node as next of previous node

4.) Make the next of previous node as new node

5.) Make previous node as previous of new node

6.) Change previous of new node as next node

Insertion in between the nodes in doubly linked list in C++

Algorithm for insertion in between the nodes in doubly linked list in C++

  •  IF PTR = NULL
  • SET NEW_NODE = PTR
  •  SET PTR = PTR -> NEXT
  • SET NEW_NODE -> DATA = VAL
  •  SET TEMP = START
  •  SET I = 0
  • REPEAT 8 to 10 until I
  •  SET TEMP = TEMP -> NEXT
  •  IF TEMP = NULL
  •  WRITE “LESS THAN DESIRED NO. OF ELEMENTS”
  • SET NEW_NODE -> NEXT = TEMP -> NEXT
  • SET NEW_NODE -> PREV = TEMP
  • SET TEMP -> NEXT = NEW_NODE
  • SET TEMP -> NEXT -> PREV = NEW_NODE
  • EXIT

Program for insertion in between the nodes in doubly linked list in C++

#include <iostream>
#include <stdlib.h>
using namespace std;

struct node {
    int num;
    struct node * preptr;
    struct node * nextptr;
}*stnode, *ennode;
 

void Listcreation(int n);
void InsertNodeAtMiddle(int num, int pos);
void displayList(int a);

int main()
{
    int n,num1,a,insPlc;
    stnode = NULL;
    ennode = NULL;
	
	   	
    cout<<" Input the number of nodes : ";
    cin>>n;
    Listcreation(n); 
    a=1;
    displayList(a);
    cout<<" Input the position ( 2 to "<< (n-1) <<") to insert a new node : ";
    cin>>insPlc;

    
if(insPlc<=1 || insPlc>=n)
    {
     cout<<"\n Invalid position\n ";
    }
	      if(insPlc>1 && insPlc<n)
      {
    cout<<" Input data for the position "<< insPlc<< " : ";
    cin>>num1;          
    InsertNodeAtMiddle(num1,insPlc);  
        a=2;
    displayList(a);
      }    
    return 0;
}
 
void Listcreation(int n)
{
    int i, num;
    struct node *fnNode;
 
    if(n >= 1)
    {
        stnode = (struct node *)malloc(sizeof(struct node));
 
        if(stnode != NULL)
        {
            cout<<" Input data for node 1: "; // assigning data in the first node
            cin>>num;
 
            stnode->num = num;
            stnode->preptr = NULL;
            stnode->nextptr = NULL;
            ennode = stnode;
            for(i=2; i<=n; i++)
            {
                fnNode = (struct node *)malloc(sizeof(struct node));
                if(fnNode != NULL)
                {
                    cout<<" Input data for node "<<i<<": ";
                    cin>>num;
                    fnNode->num = num;
                    fnNode->preptr = ennode;    // new node is linking with the previous node
                    fnNode->nextptr = NULL;     // set next address of fnnode is NULL
                    ennode->nextptr = fnNode;   // previous node is linking with the new node
                    ennode = fnNode;            // assign new node as last node
                }
                else
                {
                    cout<<" Memory can not be allocated.";
                    break;
                }
            }
        }
        else
        {
            cout<<" Memory can not be allocated.";
        }
    }
}

void InsertNodeAtMiddle(int num, int pos)
{
    int i;
    struct node * newnode, *tmp;
    if(ennode == NULL)
    {
        cout<<" No data found in the list!\n";
    }
    else
    {
        tmp = stnode;
        i=1;
        while(i<pos-1 && tmp!=NULL)
        {
            tmp = tmp->nextptr;
            i++;
        }
        if(tmp!=NULL)
        {
            newnode = (struct node *)malloc(sizeof(struct node));
            newnode->num = num;
            //next address of new node is linking with the next address of temp node            
            newnode->nextptr = tmp->nextptr; 
            // previous address of new node is linking with the tmp node            
            newnode->preptr = tmp;              
            if(tmp->nextptr != NULL)
            {
                tmp->nextptr->preptr = newnode; // n+1th node is linking with new node
            }
            tmp->nextptr = newnode; // n-1th node is linking with new node 
        }
        else
        {
            cout<<" The position you entered, is invalid.\n";
        }
    }
} 

void displayList(int m)
{
    struct node * tmp;
    int n = 1;
    if(stnode == NULL)
    {
        cout<<" No data found in the List yet.";
    }
    else
    {
        tmp = stnode;
        if (m==1)
        {
        cout<<"\n Data entered in the list are :\n";
        }
        else
        {
         cout<<"\n After insertion the new list are :\n";   
        }
        while(tmp != NULL)
        {
            cout<<" node "<<n<<": "<<tmp->num<<endl;
            n++;
            tmp = tmp->nextptr; // current pointer moves to the next node
        }
    }
}
Output:
 Input the number of nodes : 5
 Input data for node 1: 11
 Input data for node 2: 22
 Input data for node 3: 33
 Input data for node 4: 44
 Input data for node 5: 55

 Data entered in the list are :
 node 1: 11
 node 2: 22
 node 3: 33
 node 4: 44
 node 5: 55
 Input the position ( 2 to 4) to insert a new node : 3
 Input data for the position 3 : 66

 After insertion the new list are :
 node 1: 11
 node 2: 22
 node 3: 66
 node 4: 33
 node 5: 44
 node 6: 55