Deletion in Doubly Linked List in C++

Deletion in doubly linked list in C++

What is deletion in doubly linked list in CPP programming?

Deletion in a doubly linked list in C++ can be simplified as deleting a node from an already constructed linked list. On the doubly linked list, we can perform three different types of deletion operation, these different types of deletion operation are :-

  • Deleting the beginning node from the list.
  • Deletion from specific position in doubly linked list.
  • Deletion from end in doubly linked list.

Types of Deletion in Doubly Linked list in C++?

Deletion from Beginning

In the process of deletion from beginning we make the second node of the list as new head and free the previous head of the list.

Deletion from specific position

In the process of deletion from specific position in doubly linked list we delete any node that user want to delete from the given linked list.

Deletion from end

In the process of deletion from end in doubly linked list we point the next pointer of last second node to NULL and then we will free the last node.

How to perform deletion from beginning in doubly linked list?

To perform deletion from beginning in doubly linked list we will use the following steps:-

  1. First we will create a doubly linked list.
  2. Now we will free the pointer that is pointing the head of the list and will make second node as head of the list.
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; start_node->preptr = NULL; free(NodeToDel); } }
Type of deletion | From beginning

How to perform deletion from specific position in doubly linked list?

To perform deletion from specific position in doubly linked list we will use the following steps:-

  1. After creating a doubly linked list, traverse till the specific position.
  2. Assign the next pointer of the previous node to the next reference node.
  3. For the next target node the previous pointer is set to the address of the previous node.
void deleteSpecific(int pos)
{
    struct node *curNode;
    int i;
 
    curNode = start_node;
    for(i=1; i<pos && curNode!=NULL; i++)
    {
        curNode = curNode->nextptr;
    }
 
    if(pos == 1)
    {
       DeleteFirstnode();
    }
    else if(curNode == end_node)
    {
        DeleteLastnode();
    }
    else if(curNode != NULL)
    {
        curNode->preptr->nextptr = curNode->nextptr;
        curNode->nextptr->preptr = curNode->preptr;
 
        free(curNode); 
    }
    else
    {
        cout<<" The given position is invalid!\n";
    }
} 
Type of deletion | From specific position

How to perform deletion from end in doubly linked list?

To perform deletion from end in doubly linked list we will use the following steps:-

After creating a linked list with some data in it we will :-

  1. Traverse till the end of the list using a node pointer.
  2. Link the next pointer of last second node with NULL.
  3. And now we will delete the last node.
void deleteLastnode()
{
    struct node * NodeToDel;
 
    if(end_node == NULL)
    {
        cout<<" Delete is not possible. No data in the list.\n";
    }
    else
    {
        NodeToDel = end_node;
        end_node = end_node->preptr;    
        end_node->nextptr = NULL;      
        free(NodeToDel);              
    }
}
Type of deletion | From End

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