Linked List implementation for deletion, search and reverse Operations in C

              As earlier post contains the linked list implementation and insertion operation in C. here, I include the operations on linked list which contains deletion, search and reverse.

Code:

#include <stdio.h>

#include <stdlib.h>

// Node structure is created

struct L_Node {

    int n_data;

    struct L_Node* next;

};

//Let us create a new node

struct L_Node* createNode(int n_data) {

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

    newNode->n_data = n_data;

    newNode->next = NULL;

    return newNode;

}

// creation of Insertion at end

void insertEnd(struct L_Node** head, int n_data) {

    struct L_Node* newNode = createNode(n_data);

    if (*head == NULL) {

        *head = newNode;

        return;

    }

    struct L_Node* temp = *head;

    while (temp->next != NULL) {

        temp = temp->next;

    }

    temp->next = newNode;

}

// search a value

void search(struct L_Node* head, int key) {

    struct L_Node* temp = head;

    int pos = 1;

    while (temp != NULL) {

        if (temp->n_data == key) {

            printf("Element %d found at position %d\n", key, pos);

            return;

        }

        temp = temp->next;

        pos++;

    }

    printf("Element %d not found\n", key);

}

// Delete the value

void deleteNode(struct L_Node** head, int key) {

    struct L_Node* temp = *head;

    struct L_Node* prev = NULL;

    // If head node itself holds the key

    if (temp != NULL && temp->n_data == key) {

        *head = temp->next;

        free(temp);

        return;

    }

    // Search for the key

    while (temp != NULL && temp->n_data != key) {

        prev = temp;

        temp = temp->next;

    }

    // If key not found

    if (temp == NULL) return;

    // Unlink the node

    prev->next = temp->next;

    free(temp);

}

// Reverse the linked list

void reverse(struct L_Node** head) {

    struct L_Node* prev = NULL;

    struct L_Node* current = *head;

    struct L_Node* next = NULL;

    while (current != NULL) {

        next = current->next;   // store next

        current->next = prev;   // reverse link

        prev = current;         // move prev forward

        current = next;         // move current forward

    }

    *head = prev;

}

// Print the linked list

void printList(struct L_Node* head) {

    struct L_Node* temp = head;

    while (temp != NULL) {

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

        temp = temp->next;

    }

    printf("NULL\n");

}

// main() function

int main() {

    struct L_Node* head = NULL;

    insertEnd(&head, 110);

    insertEnd(&head, 202);

    insertEnd(&head, 30);

    insertEnd(&head, 45);

    insertEnd(&head, 67);

    printf("Original List: ");

    printList(head);

    search(head, 45);

    search(head, 140);

    deleteNode(&head, 202);

    printf("After Deletion: ");

    printList(head);

    reverse(&head);

    printf("After Reversal: ");

    printList(head);

    return 0;

}

Output:

Original List: 110 -> 202 -> 30 -> 45 -> 67 -> NULL

Element 45 found at position 4

Element 140 not found

After Deletion: 110 -> 30 -> 45 -> 67 -> NULL

After Reversal: 67 -> 45 -> 30 -> 110 -> NULL

That’s all. The C implementation on linked list operations is done. Hope, this code is useful to you. Keep Coding!!!

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

Symmetric Encryption in java

Java NIO examples to illustrate channels and buffers.