Deletion Operation in Linked List in C Language - Tech Fantastik

Sunday, September 13, 2020

Deletion Operation in Linked List in C Language

    Hello Friends , Welcome back to Tech Fantastik . Today we will going to learn how to  delete Node of linked list in C language . I will going to Provide you the simple code of deletion of node . First of all let's know various case to delete the nodes in given Linked List.

case 1 : Delete First Element Of Linked LIst

case 2 : Delete the Element of Given Data

case 3 : Delete Element Of Given Index in Linked LIst

case 4 : Delete Last Element Of Linked List

 

Deletion Operation  in Linked List in  C Language
Deletion Operation  in Linked List in  C Language

#include <stdio.h>

#include <stdlib.h>

struct Node {
    int data;
    struct Node * nxt;
};

// Linked List Traversal Function

void Trav(struct Node * ptr) {
    while (ptr != NULL) {
        printf("Element is : %d \n", ptr -> data);
        ptr = ptr -> nxt;
    }
}
// Function To Delete First Element Of Linked LIst
struct Node * deleteFirst(struct Node * head) {
    struct Node * ptr = head;
    head = head -> nxt;
    free(ptr);
    return head;
}
// Function To Delete Last Element Of Linked LIst
struct Node * deleteLast(struct Node * head) {
    struct Node * ptr = head -> nxt;
    struct Node * p = head;

    while (ptr -> nxt != NULL) {
        ptr = ptr -> nxt;
        p = p -> nxt;
    }
    p -> nxt = NULL;
    free(ptr);
    return head;
}
// Function To Delete Element Of Given Index in Linked LIst
struct Node * deleteAtIndex(struct Node * head, int index) {
    struct Node * ptr = head;
    struct Node * p = head -> nxt;

    for (int i = 0; i < index - 1; i++) {
        ptr = ptr -> nxt;
        p = p -> nxt;
    }
    ptr -> nxt = p -> nxt;
    free(p);
    return head;
}

// Function To Delete the with Given Data
struct Node * deleteAtValue(struct Node * head, int Value) {
    struct Node * ptr = head -> nxt;
    struct Node * p = head;

    while (ptr -> data != Value && ptr -> nxt != NULL) {
        ptr = ptr -> nxt;
        p = p -> nxt;
    }
    if (ptr -> data == Value) {
        p -> nxt = ptr -> nxt;
        free(ptr);
        return head;
    }
}

int main() {
    struct Node * e1, * e2, * e3, * e4, * e5, * e6;
    e1 = (struct Node * ) malloc(sizeof(struct Node));
    e2 = (struct Node * ) malloc(sizeof(struct Node));
    e3 = (struct Node * ) malloc(sizeof(struct Node));
    e4 = (struct Node * ) malloc(sizeof(struct Node));
    e5 = (struct Node * ) malloc(sizeof(struct Node));
    e6 = (struct Node * ) malloc(sizeof(struct Node));

    e1 -> data = 5;
    e1 -> nxt = e2;
    e2 -> data = 4;
    e2 -> nxt = e3;
    e3 -> data = 8;
    e3 -> nxt = e4;
    e4 -> data = 10;
    e4 -> nxt = e5;
    e5 -> data = 200;
    e5 -> nxt = e6;
    e6 -> data = 300;
    e6 -> nxt = NULL;

    printf("\n Linked List Traversal \n\n");
    Trav(e1);
    e1 = deleteFirst(e1);
    printf("\n Linked List Traversal After Deleting First Node \n\n");
    Trav(e1);

    e1 = deleteAtIndex(e1, 2);
    printf("\n Linked List Traversal After Deleting Node at a given Index \n\n");
    Trav(e1);
    e1 = deleteLast(e1);
    printf("\n Linked List Traversal after deleting Last Element \n\n");
    Trav(e1);

    e1 = deleteAtValue(e1, 200);
    printf("\n Linked List Traversal after deleting Element At Given Value \n\n");
    Trav(e1);

    return 0;
}

 

I hope you understood the code of deletion of node in given linked List . If you find this helpful then share this code among your friends.

Share with your friends

Add your opinion
Disqus comments
Notification
This is just an example, you can fill it later with your own note.
Done