Deletion from Beginning in Doubly Linked List in C++

How to delete element from beginning in doubly linked list in C++?

In singly linked list, we can move only in  single direction because each node has the address of the next node only.But in doubly linked list  there are two pointers, one of these pointers points to the next node and the other points to the previous node so we can move in both directions. A doubly linked list allow deletion from at:-
  • Deletion from beginning.
  • Deletion from end.
  • Deleting any particular node.

In this article, we will learn how we can delete from beginning in doubly linked list in C++.

deletion from beginning in doubly linked list

Steps to delete from beginning in doubly linked list in C++

Following steps are involved in deletion of  first node from a doubly linked list  :-

1.) First node is the head node make next node as head.

2.) Change the previous of next node with null

3.) Delete the node

Defining doubly linked list in C++

struct Node 
{
  int Data;
  Struct Node* next;
  Struct Node* prev;
};
Deletion from beginning in doubly linked list in C++

Algorithm to delete from beginning in doubly linked list in C++

  •  IF HEAD = NULL
  •  EXIT
  •  ELSE SET PTR = HEAD
  •  SET HEAD = HEAD → NEXT
  •  SET HEAD → PREV = NULL
  •  FREE PTR
  •  EXIT

Program to delete from beginning in doubly linked list in C++

#include <iostream>
#include <stdlib.h>
using namespace std;
struct node {
    int num;
    struct node * preptr;
    struct node * nextptr;
}*start_node, *end_node;
 

void Listcreation(int n);
void DeleteFirstNode();
void displayList(int a);

int main()
{
    int n,num1,a,plc;
    start_node = NULL;
    end_node = NULL;
     
    cout<<" Enter the number of nodes: ";
    cin>>n;
    Listcreation(n); 
    a=1;
    displayList(a);
    DeleteFirstNode();
        a=2;
    displayList(a);
    return 0;
}
 
void Listcreation(int n)
{
    int i, num;
    struct node *fnNode;
 
    if(n >= 1)
    {
        start_node = (struct node *)malloc(sizeof(struct node));
        if(start_node != NULL)
        {
            cout<<" Enter data for node 1: "; //assigning data in the first node
            cin>>num;
            start_node->num = num;
            start_node->preptr = NULL;
            start_node->nextptr = NULL;
            end_node = start_node;
            for(i=2; i<=n; i++)
            {
                fnNode = (struct node *)malloc(sizeof(struct node));
                if(fnNode != NULL)
                {
                    cout<<" Enter data for node "<< i<< ": ";
                    cin>>num;
                    fnNode->num = num;
                    fnNode->preptr = end_node;    // new node is linking with the previous node
                    fnNode->nextptr = NULL;       // set next address of fnnode is NULL
                    end_node->nextptr = fnNode;   // previous node is linking with the new node
                    end_node = fnNode;            // assign new node as last node
                }
                else
                {
                    cout<<" Memory can not be allocated.";
                    break;
                }
            }
        }
        else
        {
            cout<<" Memory can not be allocated.";
        }
    }
}

void DeleteFirstNode()
{
    struct node * NodeToDel;
    if(start_node == NULL)
    {
        cout<<" Deletion is not possible. No data in the list.\n";
    }
    else
    {
        NodeToDel = start_node;
        start_node = start_node->nextptr;    // move the next address of starting node to second node
        start_node->preptr = NULL;          // set previous address of staring node is NULL
        free(NodeToDel);                   // delete the first node from memory
    }
}

void displayList(int m)
{
    struct node * tmp;
    int n = 1;
    if(start_node == NULL)
    {
        cout<<" No data found in the List yet.";
    }
    else
    {
        tmp = start_node;
        if (m==1)
        {
        cout<<"\n Data entered in the list are :\n";
        }
        else
        {
         cout<<"\n After deletion the new list are :\n";   
        }
        while(tmp != NULL)
        {
            cout<<" node "<< n<<": "<< tmp->num<<endl;
            n++;
            tmp = tmp->nextptr; 
        }
    }
}
 Output:
 Enter the number of nodes: 5
 Enter 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

 After deletion the new list are :
 node 1: 22
 node 2: 33
 node 3: 44
 node 4: 55