3. Deletion of a node
1. Delete first node
2. Delete last node
3. Delete node from given position
TEQIP
-III
4. Delete the first node
Check if the linked list is empty
If so return underflow
Search for the node
Find its successor and predecessor of the node
If the end of the list has been reached then return node dose not exist
Delete the node from the list
Free the space (deleted node)
TEQIP
-III
7. void delete_from_first()
{
struct node *ptr;
if(head == NULL)
{
printf("nList is emptyn");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("nNode deleted from the beginning of the linked list");
}
}
TEQIP
-III
8. Delete the last node
Check if the linked list is empty
If so return underflow
Traverse the list to reach at the predecessor of last node
Delete the node from the list
Free the space (deleted node)
TEQIP
-III
11. void delete_from_End(node head)
{
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("nlist is empty");
}
else if(head→ next == NULL)
{
head = NULL;
free(head);
printf("nOnly node of the list deleted ...n");
}
TEQIP
-III
12. else
{
ptr = head;
while(ptr → next != NULL)
{
ptr1 = ptr;
ptr = ptr → next;
}
ptr1 → next = NULL;
free(ptr);
printf("nDeleted Node from the last ...n");
}
}
TEQIP
-III
13. Delete the node from given position
1. Check if the linked list is empty
If so return underflow
2. Search for the node
3. Find its successor and predecessor of the node
4. If the end of the list has been reached then return node dose not exist
5. Delete the node from the list
6. Free the space (deleted node)
TEQIP
-III
16. void random_delete()
{
struct node *ptr,*ptr1;
int loc,i;
printf("n Enter the position of the node after which you want to perform deletion n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr→ next;
if(ptr == NULL)
{
printf("nGiven position does not exist");
return;
}
}
ptr1 → next = ptr → next;
free(ptr);
printf("nDeleted node %d ",loc+1);
}
TEQIP
-III