C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Data Structures in C

Delete Node by Value in Linked List

C Program: Delete Node by Value in Linked List

C

#include <stdio.h>

#include <stdlib.h>

 

// Structure for a node

struct Node {

    int data;

    struct Node *next;

};

 

// Function to create linked list

struct Node* createList(int n) {

    struct Node *head = NULL, *temp, *newNode;

    int data, i;

 

    for (i = 1; i <= n; i++) {

        newNode = (struct Node*)malloc(sizeof(struct Node));

        if (newNode == NULL) {

            printf("Memory allocation failed!\n");

            exit(0);

        }

 

        printf("Enter data for node %d: ", i);

        scanf("%d", &data);

        newNode->data = data;

        newNode->next = NULL;

 

        if (head == NULL)

            head = newNode;

        else

            temp->next = newNode;

 

        temp = newNode;

    }

    return head;

}

 

// Function to display linked list

void displayList(struct Node *head) {

    struct Node *temp = head;

    if (head == NULL) {

        printf("\nList is empty.\n");

        return;

    }

    printf("\nLinked List: ");

    while (temp != NULL) {

        printf("%d -> ", temp->data);

        temp = temp->next;

    }

    printf("NULL\n");

}

 

// Function to delete node by value

struct Node* deleteByValue(struct Node *head, int value) {

    struct Node *temp = head, *prev = NULL;

 

    // Case 1: List is empty

    if (head == NULL) {

        printf("\nList is empty. Nothing to delete.\n");

        return NULL;

    }

 

    // Case 2: Value found at head

    if (temp != NULL && temp->data == value) {

        head = temp->next;

        free(temp);

        printf("\nNode with value %d deleted (from beginning).\n", value);

        return head;

    }

 

    // Case 3: Traverse to find value

    while (temp != NULL && temp->data != value) {

        prev = temp;

        temp = temp->next;

    }

 

    // Value not found

    if (temp == NULL) {

        printf("\nValue %d not found in the list.\n", value);

        return head;

    }

 

    // Delete node

    prev->next = temp->next;

    free(temp);

 

    printf("\nNode with value %d deleted successfully.\n", value);

    return head;

}

 

int main() {

    struct Node *head = NULL;

    int n, value;

 

    printf("Enter number of nodes: ");

    scanf("%d", &n);

 

    head = createList(n);

 

    printf("\nBefore deletion:");

    displayList(head);

 

    printf("\nEnter the value to delete: ");

    scanf("%d", &value);

 

    head = deleteByValue(head, value);

 

    printf("\nAfter deletion:");

    displayList(head);

 

    return 0;

}

Output

 
OUTPUT :

Enter number of nodes: 5
Enter data for node 1: 10
Enter data for node 2: 20
Enter data for node 3: 30
Enter data for node 4: 40
Enter data for node 5: 50

Before deletion:
Linked List: 10 -> 20 -> 30 -> 40 -> 50 -> NULL

Enter the value to delete: 30

Node with value 30 deleted successfully.

After deletion:
Linked List: 10 -> 20 -> 40 -> 50 -> NULL

Explanation

Step

Description

1

Create the linked list dynamically using createList().

2

In deleteByValue(), first check if the list is empty.

3

If the head node matches the value, delete it.

4

Otherwise, traverse to find the node with the given value.

5

If found, adjust the previous node’s next pointer and free memory.

6

If not found, display a message.