Insertion at End in Doubly Linked list in C++

How to insert element at end in doubly linked list in C++?

Doubly Linked List is one of the linear data structure that we can use in place of array if we want to store a large amount of data. This is so because in doubly linked list the insertion and deletion operation are less time taking and more efficient than in an array. Moreover in a doubly linked list we can traverse in both the directions that is towards head or towards tails, hence which makes the use of doubly linked list more likely than an array, when we are working on large amount of data.

Insertion at end in doubly linked list in C++

Definition of doubly linked list in C++

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

Steps to insert element at end in doubly linked list in C++

1.) Allocate node.

2.)Put the data.

3.) Make next of this new node as Null

4.)This new node will become head if the linked list is empty.

5.)Else traverse till the last node.

6.)Change the next of last node from null to previous of new node.

Insertion at end in dobuly linked list in C++

Algorithm for insertion at end in doubly linked list in C++

  • IF PTR = NULL
  • SET NEW_NODE = PTR
  • SET PTR = PTR -> NEXT
  • SET NEW_NODE -> DATA = VAL
  • SET NEW_NODE -> NEXT = NULL
  • SET TEMP = START
  • Repeat next while TEMP -> NEXT != NULL
  • SET TEMP = TEMP -> NEXT
  • SET TEMP -> NEXT = NEW_NODE
  • SET NEW_NODE -> PREV = TEMP
  • EXIT

Program for insertion at end in doubly linked list in C++

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

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

void DlListcreation(int n);
void DlLinsertNodeAtEnd(int num);
void displayDlList();

int main()
{
    int n,num1;
    stnode = NULL;
    ennode = NULL;
	
    cout<<" Input the number of nodes : ";
    cin>>n;
    DlListcreation(n); 
    displayDlList();
    cout<<" Input data for the last node : ";
    cin>>num1;
    DlLinsertNodeAtEnd(num1);
    displayDlList();
    return 0;
}
 
void DlListcreation(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;
                    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 DlLinsertNodeAtEnd(int num)
{
    struct node * newnode;
 
    if(ennode == NULL)
    {
        cout<<" No data found in the list!\n";
    }
    else
    {
        newnode = (struct node *)malloc(sizeof(struct node));
        newnode->num = num;
        newnode->nextptr = NULL;        // set next address field of new node  is NULL
        newnode->preptr = ennode;       // previous address of new node is linking with ending node
        ennode->nextptr = newnode;      // next address of ending node is linking with new node
        ennode = newnode;               // set the new node as ending node  
    }
}

void displayDlList ()
{
  struct node *tmp;
  tmp = stnode;
  int n = 1;
  while (tmp != NULL)
    {
      cout << " node " << n << ": " << tmp->num << "\n";
      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 :
 node1: 11
 node2: 22
 node3: 33
 node4: 44
 node5: 55
 Input data for the last node : 66

 After insertion the new list are :
 node1: 11
 node2: 22
 node3: 33
 node4: 44
 node5: 55
 node6: 66

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

Doubly Linked List

  • Introduction to Doubly Linked list in Data Structure
    Click Here
  • Doubly Linked List in –
    C | C++ | Java
  • Insertion in doubly linked list –
    C | C++ | Java
  • Insertion at beginning in doubly linked list –
    C | C++ | Java
  • Insertion at end in doubly linked list –
    C | C++ | Java
  • Insertion at nth node in doubly linked list –
    C | C++ | Java
  • Deletion in doubly linked list  –
    C | C++ | Java
  • Deletion from beginning in doubly linked list :
  • Deletion from nth in doubly linked list :
    C | C++ | Java
  • Deletion from end in doubly linked list :
    C | C++ | Java
  • Insertion and Deletion in a  doubly linked list :
    C | C++ | Java
  • Insertion in the middle in a  doubly linked list :
    C | C++ | Java

Doubly Linked List