C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Data Structures in C

Deletion Operations in a Doubly Linked List

C Program: Deletion in Doubly Linked List (Beginning, End, and Position)

C

#include <stdio.h>

#include <stdlib.h>

 

// Structure definition

struct Node {

    int data;

    struct Node *prev;

    struct Node *next;

};

 

// Function to create a new node

struct Node* createNode(int data) {

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

    newNode->data = data;

    newNode->prev = NULL;

    newNode->next = NULL;

    return newNode;

}

 

// Function to insert at end (for setup)

struct Node* insertAtEnd(struct Node *head, int data) {

    struct Node *newNode = createNode(data);

    if (head == NULL)

        return newNode;

 

    struct Node *temp = head;

    while (temp->next != NULL)

        temp = temp->next;

 

    temp->next = newNode;

    newNode->prev = temp;

    return head;

}

 

// Function to display list

void displayList(struct Node *head) {

    struct Node *temp = head;

    printf("\nDoubly Linked List: ");

    while (temp != NULL) {

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

        temp = temp->next;

    }

    printf("NULL\n");

}

 

// Function to delete from beginning

struct Node* deleteFromBeginning(struct Node *head) {

    if (head == NULL) {

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

        return NULL;

    }

    struct Node *temp = head;

    head = head->next;

    if (head != NULL)

        head->prev = NULL;

 

    printf("\nDeleted node: %d\n", temp->data);

    free(temp);

    return head;

}

 

// Function to delete from end

struct Node* deleteFromEnd(struct Node *head) {

    if (head == NULL) {

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

        return NULL;

    }

 

    struct Node *temp = head;

    while (temp->next != NULL)

        temp = temp->next;

 

    printf("\nDeleted node: %d\n", temp->data);

 

    if (temp->prev != NULL)

        temp->prev->next = NULL;

    else

        head = NULL;

 

    free(temp);

    return head;

}

 

// Function to delete from specific position

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

    if (head == NULL) {

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

        return NULL;

    }

 

    struct Node *temp = head;

    int i;

    if (pos == 1)

        return deleteFromBeginning(head);

 

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

        temp = temp->next;

 

    if (temp == NULL) {

        printf("\nPosition out of range!\n");

        return head;

    }

 

    printf("\nDeleted node: %d\n", temp->data);

 

    if (temp->prev != NULL)

        temp->prev->next = temp->next;

 

    if (temp->next != NULL)

        temp->next->prev = temp->prev;

 

    free(temp);

    return head;

}

 

int main() {

    struct Node *head = NULL;

    int n, data, choice, pos, i;

 

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

    scanf("%d", &n);

 

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

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

        scanf("%d", &data);

        head = insertAtEnd(head, data);

    }

 

    displayList(head);

 

    while (1) {

        printf("\n--- MENU ---\n");

        printf("1. Delete from Beginning\n");

        printf("2. Delete from End\n");

        printf("3. Delete from Position\n");

        printf("4. Display List\n");

        printf("5. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

 

        switch (choice) {

            case 1: head = deleteFromBeginning(head); break;

            case 2: head = deleteFromEnd(head); break;

            case 3:

                printf("Enter position to delete: ");

                scanf("%d", &pos);

                head = deleteFromPosition(head, pos);

                break;

            case 4: displayList(head); break;

            case 5: exit(0);

            default: printf("Invalid choice!\n");

        }

    }

 

    return 0;

}

Output

 
OUTPUT :

Enter number of initial 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

Doubly Linked List: 10 <-> 20 <-> 30 <-> 40 <-> NULL

--- MENU ---
1. Delete from Beginning
2. Delete from End
3. Delete from Position
4. Display List
5. Exit
Enter your choice: 1

Deleted node: 10
Doubly Linked List: 20 <-> 30 <-> 40 <-> NULL

Explanation

Operation

Description

deleteFromBeginning()

Removes the first node and adjusts the head pointer.

deleteFromEnd()

Traverses to the last node, removes it, and updates the previous node’s next pointer.

deleteFromPosition()

Navigates to the given position, unlinks that node, and frees its memory.