First, we need to traverse the list to reach the last node of the list. Deletion of the last node in a doubly-linked list requires pointer adjustments at that position. We will follow the below steps, to delete the last node of the list:
- If the condition head == NULL is true, no operation can be carried on, because it means that the list is already empty.
- If the condition head → next == NULL is true, we only need to assign the head of the list to NULL and free head to completely delete the list, because it means that there is only one node in the list.
- Otherwise, use the below statements, to traverse the list to reach the last node of the list.
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
} |
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
- At the end of the for loop, the ptr would point to the last node of the list. Thus, we will use the below statement, to make the next pointer of the previous node of ptr to NULL.
ptr → prev → next = NULL |
ptr → prev → next = NULL
- At last, use the below statement, to free the pointer which is also the node to be deleted.
Algorithm:
- Step 1:IF HEAD = NULL
Write UNDERFLOW
Go to Step 7
[END OF IF] - Step 2: SET TEMP = HEAD
- Step 3: REPEAT STEP 4 WHILE TEMP->NEXT != NULL
- Step 4:SET TEMP = TEMP->NEXT
[END OF LOOP] - Step 5: SET TEMP ->PREV-> NEXT = NULL
- Step 6: FREE TEMP
- Step 7: EXIT
Example in C:
#include<stdio.h>
#include<stdlib.h>
void create(int);
void deletitionAtEnd();
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 the end\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:
deletitionAtEnd();
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;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("\nNode Inserted Successfully!!\n");
}
}
void deletitionAtEnd()
{
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;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nNode Deleted Successfully!!\n");
}
} |
#include<stdio.h>
#include<stdlib.h>
void create(int);
void deletitionAtEnd();
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 the end\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:
deletitionAtEnd();
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;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("\nNode Inserted Successfully!!\n");
}
}
void deletitionAtEnd()
{
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;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nNode Deleted Successfully!!\n");
}
}
Output:
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
2
UNDERFLOW
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
1
Enter the element to insert:
2
Node Inserted Successfully!!
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
2
Node Deleted Successfully!!
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
1
Enter the element to insert:
1
Node Inserted Successfully!!
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
1
Enter the element to insert:
2
Node Inserted Successfully!!
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
2
Node Deleted Successfully!!
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
1
Enter the element to insert:
3
Node Inserted Successfully!!
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
2
Node Deleted Successfully!!
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
2
Node Deleted Successfully!!
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
2
UNDERFLOW
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
3 |
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
2 UNDERFLOW
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
1 Enter the element to insert:
2 Node Inserted Successfully!!
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
2 Node Deleted Successfully!!
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
1 Enter the element to insert:
1 Node Inserted Successfully!!
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
1 Enter the element to insert:
2 Node Inserted Successfully!!
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
2 Node Deleted Successfully!!
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
1 Enter the element to insert:
3 Node Inserted Successfully!!
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
2 Node Deleted Successfully!!
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
2 Node Deleted Successfully!!
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
2 UNDERFLOW
1.Insert Node
2.Delete node from the end
3.Exit
4.Enter your choice:
3