C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Data Structures in C

Delete Node from a Specific Position in Linked List

C Program: Delete Node from a Specific Position in Linked List

C

#include <stdio.h>

#include <stdlib.h>

 

// Structure for a node

struct Node {

    int data;

    struct Node *next;

};

 

// Function to create a 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;

            temp = head;

        } else {

            temp->next = newNode;

            temp = temp->next;

        }

    }

 

    return head;

}

 

// Function to delete a node from a given position

struct Node* deleteFromPosition(struct Node *head, int position) {

    struct Node *temp = head, *prev;

    int i;

 

    // Empty list check

    if (head == NULL) {

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

        return NULL;

    }

 

    // Delete from beginning

    if (position == 1) {

        head = head->next;

        free(temp);

        printf("\nNode deleted from position 1.\n");

        return head;

    }

 

    // Traverse to the node before the one to delete

    for (i = 1; temp != NULL && i < position; i++) {

        prev = temp;

        temp = temp->next;

    }

 

    // Invalid position

    if (temp == NULL) {

        printf("\nInvalid position!\n");

        return head;

    }

 

    prev->next = temp->next;

    free(temp);

 

    printf("\nNode deleted from position %d.\n", position);

 

    return head;

}

 

// Function to display the linked list

void displayList(struct Node *head) {

    struct Node *temp = head;

 

    if (head == NULL) {

        printf("\nThe linked list is empty.\n");

        return;

    }

 

    printf("\nLinked List Elements: ");

    while (temp != NULL) {

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

        temp = temp->next;

    }

    printf("NULL\n");

}

 

int main() {

    struct Node *head = NULL;

    int n, position;

 

    printf("Enter the number of nodes: ");

    scanf("%d", &n);

 

    head = createList(n);

 

    printf("\nBefore deletion:");

    displayList(head);

 

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

    scanf("%d", &position);

 

    head = deleteFromPosition(head, position);

 

    printf("\nAfter deletion:");

    displayList(head);

 

    return 0;

}

Output

 
OUTPUT :

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

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

Enter the position to delete: 3

Node deleted from position 3.

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


Explanation

  • createList() — Dynamically creates the linked list.
  • deleteFromPosition()
    • Deletes a node from a given position:
      • If position = 1 → deletes the head.
      • Otherwise, traverses to the node just before the target node.
    • Checks for invalid positions or empty list.
  • displayList() — Prints all elements of the list.