Deletion in doubly linked list after specified node

We will follow the below steps, to delete the node after the specified data:

  • Use the below statement, to copy the head pointer into a temporary pointer temp.
    temp = head
  • Use the below statements, to traverse the list until we find the desired data value.
    while(temp -> data != val)  
    temp = temp -> next;
  • Use the below statements, to check if this is the last node of the list. We can’t perform the deletion if this is the last node of the list.
    if(temp -> next == NULL)  
        {  
          return;   
        }
  • First, check if the node to be deleted is the last node of the list. And, if it is so, use the below statement, to make the next pointer of this node point to null. It can thus be the new last node of the list.
    if(temp -> next -> next == NULL)  
    {  
      temp ->next = NULL;  
    }
  • In any other case, we will use the below statements, to make the pointer ptr point to the node to be deleted, to make the next of temp point to the next of ptr, to make the previous of next node of ptr point to temp and ultimately, to free the ptr.
    ptr = temp -> next;  
    temp -> next = ptr -> next;  
    ptr -> next -> prev = temp;  
    free(ptr);

Algorithm:

  • Step 1:IF HEAD = NULL
    Write UNDERFLOW
    Go to Step 9
    [END OF IF]
  • Step 2: SET TEMP = HEAD
  • Step 3: Repeat Step 4 while TEMP -> DATA != ITEM
  • Step 4:SET TEMP = TEMP -> NEXT
    [END OF LOOP]
  • Step 5: SET PTR = TEMP -> NEXT
  • Step 6: SET TEMP -> NEXT = PTR -> NEXT
  • Step 7: SET PTR -> NEXT -> PREV = TEMP
  • Step 8: FREE PTR
  • Step 9: EXIT

Example in C:

#include<stdio.h>  
#include<stdlib.h>  
struct node  
{  
    struct node *prev;  
    struct node *next;  
    int data;  
};  
struct node *head;  
void insertionAtBeginning();  
void deletitionAfterSpecifiedNode();  
void display();  
void main()  
{  
int choice =0;  
    while(choice != 9)  
    {  
        printf("\nSelect an option from the below list::\n");  
        printf("\n1.Insert Node\n2.Delete the node after the given data\n3.Show\n4.Exit\n");  
        printf("\nEnter your choice:\n");  
        scanf("\n%d",&choice);  
        switch(choice)  
        {  
            case 1:  
            insertionAtBeginning();  
            break;  
            case 2:  
            deletitionAfterSpecifiedNode();  
            break;  
            case 3:  
            display();  
            break;  
            case 4:  
            exit(0);  
            break;  
            default:  
            printf("Please enter a valid choice.\n");  
        }  
    }  
}  
void insertionAtBeginning()  
{  
   struct node *ptr;   
   int item;  
   ptr = (struct node *)malloc(sizeof(struct node));  
   if(ptr == NULL)  
   {  
       printf("\nOVERFLOW");  
   }  
   else  
   {  
    printf("\nEnter the element to insert:\n");  
    scanf("%d",&item);  
 
   if(head==NULL)  
   {  
       ptr->next = NULL;  
       ptr->prev=NULL;  
       ptr->data=item;  
       head=ptr;  
   }  
   else   
   {  
       ptr->data=item;  
       ptr->prev=NULL;  
       ptr->next = head;  
       head->prev=ptr;  
       head=ptr;  
   }  
   printf("\nNode inserted successfully!!\n");  
}  
 
}  
 
void deletitionAfterSpecifiedNode()  
{  
    struct node *ptr, *temp;  
    int val;  
    printf("\nEnter the element after which the node is to be deleted:\n");  
    scanf("%d", &val);  
    ptr = head;  
    while(ptr -> data != val)  
    ptr = ptr -> next;  
    if(ptr -> next == NULL)  
    {  
        printf("\nCan't delete\n");  
    }  
    else if(ptr -> next -> next == NULL)  
    {  
        ptr ->next = NULL;  
    }  
    else  
    {   
        temp = ptr -> next;  
        ptr -> next = temp -> next;  
        temp -> next -> prev = ptr;  
        free(temp);  
        printf("\nNode deleted successfully!!\n");  
    }     
}  
void display()  
{  
    struct node *ptr;  
    printf("\nList:\n");  
    ptr = head;  
    while(ptr != NULL)  
    {  
        printf("%d\n",ptr->data);  
        ptr=ptr->next;  
    }  
}

Output:

Select an option from the below list::
 
1.Insert Node
2.Delete the node after the given data
3.Show
4.Exit
 
Enter your choice:
1
 
Enter the element to insert:
1
 
Node inserted successfully!!
 
Select an option from the below list::
 
1.Insert Node
2.Delete the node after the given data
3.Show
4.Exit
 
Enter your choice:
1
 
Enter the element to insert:
2
 
Node inserted successfully!!
 
Select an option from the below list::
 
1.Insert Node
2.Delete the node after the given data
3.Show
4.Exit
 
Enter your choice:
1
 
Enter the element to insert:
3
 
Node inserted successfully!!
 
Select an option from the below list::
 
1.Insert Node
2.Delete the node after the given data
3.Show
4.Exit
 
Enter your choice:
1
 
Enter the element to insert:
4
 
Node inserted successfully!!
 
Select an option from the below list::
 
1.Insert Node
2.Delete the node after the given data
3.Show
4.Exit
 
Enter your choice:
1
 
Enter the element to insert:
5
 
Node inserted successfully!!
 
Select an option from the below list::
 
1.Insert Node
2.Delete the node after the given data
3.Show
4.Exit
 
Enter your choice:
3
 
List:
5
4
3
2
1
 
Select an option from the below list::
 
1.Insert Node
2.Delete the node after the given data
3.Show
4.Exit
 
Enter your choice:
2
 
Enter the element after which the node is to be deleted:
3
 
Node deleted successfully!!
 
Select an option from the below list::
 
1.Insert Node
2.Delete the node after the given data
3.Show
4.Exit
 
Enter your choice:
3
 
List:
5
4
3
1
 
Select an option from the below list::
 
1.Insert Node
2.Delete the node after the given data
3.Show
4.Exit
 
Enter your choice:
2
 
Enter the element after which the node is to be deleted:
1
 
Can't delete
 
Select an option from the below list::
 
1.Insert Node
2.Delete the node after the given data
3.Show
4.Exit
 
Enter your choice:
3
 
List:
5
4
3
1
 
Select an option from the below list::
 
1.Insert Node
2.Delete the node after the given data
3.Show
4.Exit
 
Enter your choice:
4
Please Share