Deletion in doubly linked list at beginning

Being the simplest operation, we just need to copy the head pointer to pointer ptr and shift the head pointer to its next, for deletion in the doubly linked list at the beginning.

ptr = head;  
head = head → next;

Now, use the below statements, to make the prev of this new head node point to NULL.

head → prev = NULL

At last, use the free function, to free the pointer ptr.

free(ptr)

Algorithm:

  • STEP 1:
    IF HEAD = NULL
    WRITE UNDERFLOW
    GOTO STEP 6
  • STEP 2: SET PTR = HEAD
  • STEP 3: SET HEAD = HEAD → NEXT
  • STEP 4: SET HEAD → PREV = NULL
  • STEP 5: FREE PTR
  • STEP 6: EXIT

Example in C:

#include  
#include  
void create(int);  
void deletitionAtBeginning();  
struct node  
{  
    int data;  
    struct node *next;  
    struct node *prev;  
};  
struct node *head;  
void main ()  
{  
    int choice,item;  
    do   
    {  
        printf("1.Insert Node\n2.Delete node from beginning\n3.Exit\n4.Enter your choice:\n");  
        scanf("%d",&choice);  
        switch(choice)  
        {  
            case 1:  
            printf("\nEnter the element to insert:\n");  
            scanf("%d",&item);  
            create(item);  
            break;   
            case 2:  
            deletitionAtBeginning();  
            break;   
            case 3:  
            exit(0);  
            break;    
            default:  
            printf("\nPlease enter a valid choice.\n");  
        }  
 
    }while(choice != 3);  
}  
void create(int item)  
{  
 
   struct node *ptr = (struct node *)malloc(sizeof(struct node));  
   if(ptr == NULL)  
   {  
       printf("\nOVERFLOW\n");  
   }  
   else  
   {  
 
 
   if(head==NULL)  
   {  
       ptr->next = NULL;  
       ptr->prev=NULL;  
       ptr->data=item;  
       head=ptr;  
   }  
   else   
   {  
       ptr->data=item;printf("\nPress 0 to insert more elements:\n");  
       ptr->prev=NULL;  
       ptr->next = head;  
       head->prev=ptr;  
       head=ptr;  
   }  
    printf("\nNode Inserted Successfully!!\n");  
}  
 
}  
void deletitionAtBeginning()  
{  
    struct node *ptr;  
    if(head == NULL)  
    {  
        printf("\nUNDERFLOW\n");  
    }  
    else if(head->next == NULL)  
    {  
        head = NULL;   
        free(head);  
        printf("\nNode Deleted Successfully!!\n");  
    }  
    else  
    {  
        ptr = head;  
        head = head -> next;  
        head -> prev = NULL;  
        free(ptr);  
        printf("\nNode Deleted Successfully!!\n");  
    }  
}

Output:

1.Insert Node
2.Delete node from beginning
3.Exit
4.Enter your choice:
1
 
Enter the element to insert:
2
 
Node Inserted Successfully!!
1.Insert Node
2.Delete node from beginning
3.Exit
4.Enter your choice:
2
 
Node Deleted Successfully!!
1.Insert Node
2.Delete node from beginning
3.Exit
4.Enter your choice:
2
 
UNDERFLOW
1.Insert Node
2.Delete node from beginning
3.Exit
4.Enter your choice:
1
 
Enter the element to insert:
1
 
Node Inserted Successfully!!
1.Insert Node
2.Delete node from beginning
3.Exit
4.Enter your choice:
1
 
Enter the element to insert:
2
 
Press 0 to insert more elements:
 
Node Inserted Successfully!!
1.Insert Node
2.Delete node from beginning
3.Exit
4.Enter your choice:
2
 
Node Deleted Successfully!!
1.Insert Node
2.Delete node from beginning
3.Exit
4.Enter your choice:
2
 
Node Deleted Successfully!!
1.Insert Node
2.Delete node from beginning
3.Exit
4.Enter your choice:
2
 
UNDERFLOW
1.Insert Node
2.Delete node from beginning
3.Exit
4.Enter your choice:
3