ALGORITHM (Deletion of an element from the linked list)
Step 1 Begin
Step 2 if the list is empty, then element cannot be deleted
Step 3 else, if the element to be deleted is the first node, then make the start (head) point to the second element.
Step 4 else, Remove the element from list by calling a find function & returning the found address of the element.
Step 5 End
DELETE_LIST FUNCTION
/* prototype of delete_function */
list *delete_list(list *);
list *find(list *, int);
/*definition of delete_list */
list *delete_list(list *start)
{
int key; list * f, * temp;
printf("\n Insert the element to be purged \n");
scanf("%d", &key);
if(start->data == key)
{
temp=start->next;
free(start); start=temp;
}
else
{
f = find(start,key);
if(f==NULL)
printf("\n key is not found");
else
{
temp = f->next->next;
free(f->next);
f->next=temp;
}
}
return(start);
}
void main()
{
list *head;
void create(list *);
int count(list *);
void traverse(list *);
head=(list *)malloc(sizeof(list));
create(head);
printf(" \n traverse created list \n");
traverse(head);
printf("\n number of elements within the list %d \n", count(head));
head=insert(head);
printf(" \n traverse list after adding desiredelement \n");
traverse(head);
head=delete_list(head);
printf(" \n traverse list after delete_list \n");
traverse(head);
}
Program: Deletion of any element from the linked list by searching for element i.e. to be deleted