C Program to Delete a Node from Linked List

Linked List is a dynamic data structure which is used to organise stored data linearly, we can perform certain operations on these data structures such as

Before going through this article we recommend you to clear your concepts about Linked List, refer the following article

Understanding the Concepts of Linked List-The Easy Way

C program to perform implementation of Linked List

We can perform three types of deletion on Linked List

  • Front node deletion
  • Intermediate node deletion
  • End node deletion

We will discuss each of them one by one and make C functions for each of the methods.

Front Node Deletion

In this type of deletion, we move the head pointer to the second node and make the address part of the previous head node as NULL. See the below picture to understand.

C program to delete a node from Linked List

Now let’s write the C code for it, see the below code snippet

void front_delete(){ struct node *y; if(empty()){ printf("Linked List is empty"); } else{ y = head->add; head->add = NULL; head = y; } } 

In the above code, we will first check if the list is empty or not if it is not empty we will store the address of the head node in pointer y and then we will make the address part of the head node as NULL(to disconnect it from the list) and then we will assign the address stored in y as the head.

Last Node Delete

It is almost same as that of Front node deletion, see the below code

void last_delete(){ struct node *y; if(empty()){ printf("Linked List is empty"); } else{ y = head; while((y->add)->add!=NULL){ y = y->add; } y->add = NULL; } } 

In the above code, we are traversing up to the second last node of the List and store the address of the second last node into pointer y and to disconnect the node we will simply make the address part of the second last node as NULL.

Deleting Intermediate Node

In this type of deletion, we perform to operations at a time

  • Searching
  • Deletion

We will perform the search operation because the node will be entered by the user, so we have to match the entered value with values present in the linked list and when a match is found we will perform the deleting operation, so let’s write the code for searching first.

int search(int x){ struct node *y; y=head; while(y!=NULL){ if(y->val==x){ return 1; } y = y->add; } } 

In the above code , the function will traverse the whole list and try to match the entered value with each node’s value part and when a match is found it will return 1 or true.

Now we will use the above search function to check whether the entered value is present in the list or not, see the below code snippet

void del(int x){ struct node *y; y = head; if(search(x)){ while((y->add)->val!=x){ y = y->add; } y->add = (y->add)->add; } else{ printf("No element found"); } } 

If a match is found then we will traverse up to the immediate predecessor of the node and will assign the address part of the next node as the address part of the predecessor node(to disconnect the mid node).

The whole code now will look like as shown below

#include<stdio.h> #include<stdlib.h> struct node{ int val; struct node *add;  }; int temp; struct node *y,*head; int empty(){ if(head==NULL){ return 1; } else{ return 0; }  } int insert(int x){ struct node *temp; if(empty()){ temp = (struct node *)malloc(sizeof(struct node)); temp->val = x; temp->add = NULL; head = temp; } else{ y = temp; temp = (struct node *)malloc(sizeof(struct node)); y->add = temp; temp->val = x; temp->add = NULL; } } void display(){ struct node *j; j = head; while(j!=NULL){ printf("\%d ",j->val); j = j->add; } } void front_delete(){ struct node *y; if(empty()){ printf("Linked List is empty"); } else{ y = head->add; head->add = NULL; head = y; } } void last_delete(){ struct node *y; if(empty()){ printf("Linked List is empty"); } else{ y = head; while((y->add)->add!=NULL){ y = y->add; } y->add = NULL; } } int search(int x){ struct node *y; y=head; while(y!=NULL){ if(y->val==x){ return 1; } y = y->add; } } void del(int x){ struct node *y; y = head; if(search(x)){ while((y->add)->val!=x){ y = y->add; } y->add = (y->add)->add; } else{ printf("No element found"); } } int main(){ insert(10); insert(20); insert(40); insert(25); insert(15); del(25); front_delete();       last_delete(); display(); } 

Output

C program to delete a node from Linked list

Thanks for reading

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook . You can also comment your questions below.

Also, don’t forget to subscribe to our Newsletter.

If you like this article, then please share it and help us grow.


Preorder and Postorder Traversal of binary tree in Python
02 September 2018

Binary Tree in Python
02 September 2018

Image Sharpening by High Pass Filter using Python and OpenCV
17 August 2018

Explaining Register variables in C with examples
17 August 2018

C program to generate all combinations of N-Bit Binary String
10 July 2018

Data Autosave System using PHP, MySQL and AJAX
06 July 2018

Понравилась статья? Поделиться с друзьями:
Добавить комментарий