How to Delete a Node from Singly Linked List?

Here we’ll see how to write C program to delete a node from singly linked list. In this example, the node to be deleted will be identified by the value of the node.

Logic to Delete a Node from Singly Linked List

  • If the first node (where the head points to) matches with the value to be deleted.
    • Point head where the first node points to.
    • delete the first node.
  • Start traverse list starting from head (head is assigned to tmp in our example program below).
    • If the next node exists (not NULL) and the value of the next node matches with the value to be deleted.
      • Point the current node (pointed by tmp) where the next node points to.
      • Delete the next node.
    • Otherwise, move to the next node.

C Program to Delete a Node from Singly Linked List

#include <stdio.h>
#include <stdlib.h>

struct node{
 int val;
 struct node *next;
};

void print_list(struct node *head)
{
 printf("H->");

 while(head)
 {
  printf("%d->", head->val);
  head = head->next;
 }

 printf("|||\n");
}

void insert_front(struct node **head, int value)
{
 struct node * new_node = NULL;

 new_node = (struct node *)malloc(sizeof(struct node));

 if (new_node == NULL)
 {
  printf("Failed to insert element. Out of memory");
 }

 new_node->val = value;
 new_node->next = *head;

 *head = new_node;
}

void delete_node(struct node **head, int val)
{
 struct node *tmp = NULL;
 struct node *tmp1 = NULL;

 if (head == NULL || *head == NULL) return;

 if((*head)->val == val)
 {
  /*Delete the head node*/
  tmp = *head;
  *head = (*head)->next;
  free(tmp);
  return;
 }

 tmp = *head;

 while(tmp->next && tmp->next->val != val) tmp = tmp->next;

 if(tmp->next)
 {
  tmp1 = tmp->next;
  tmp->next = tmp1->next;

  free(tmp1);
 }
 else
 {
  printf("%d not found in the list.\n", val);
 }
}

void main()
{
 int count = 0, i, val;
 struct node * head = NULL;

 printf("Enter number of elements: ");
 scanf("%d", &count);

 for (i = 0; i < count; i++)
 {
  printf("Enter %dth element: ", i);
  scanf("%d", &val);
  insert_front(&head, val);
 }

 printf("Initial List: ");
 print_list(head);

 printf("Enter a value to delete from the list: ");
 scanf("%d", &val);

 delete_node(&head, val);

 printf("List after deletion: ");
 print_list(head);
}

Output:

Enter number of elements: 5
Enter 0th element: 43
Enter 1th element: 5
Enter 2th element: 10
Enter 3th element: 23
Enter 4th element: 78
Initial List: H->78->23->10->5->43->|||
Enter a value to delete from the list: 10
List after deletion: H->78->23->5->43->|||

In this example program delete_node() function implemented the logic described above. print_list() is used to print the list and inset_front() is used to create the list.

Read also:

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

Leave a Reply

Your email address will not be published. Required fields are marked *

0
3
1
5
4
0