C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Dynamic Memory Allocation in C

Delete node dynamically in linked list

C Program: Delete node dynamically in linked list

Method 1: Delete node dynamically in linked list

C

#include <stdio.h>

#include <stdlib.h>

 

// Define structure for node

struct Node {

    int data;

    struct Node *next;

};

 

int main() {

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

    int choice = 1;

 

    while (choice) {

        // Dynamically allocate memory for new node

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

        if (newNode == NULL) {

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

            return 1;

        }

 

        printf("Enter data for node: ");

        scanf("%d", &newNode->data);

        newNode->next = NULL;

 

        // First node creation

        if (head == NULL)

            head = temp = newNode;

        else {

            temp->next = newNode;  // Link new node

            temp = newNode;        // Move temp to last node

        }

 

        printf("Do you want to add another node? (1-Yes / 0-No): ");

        scanf("%d", &choice);

    }

 

    // Display the linked list

    printf("\nLinked List Elements:\n");

    temp = head;

    while (temp != NULL) {

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

        temp = temp->next;

    }

    printf("NULL\n");

 

    // Free allocated memory

    temp = head;

    struct Node *nextNode;

    while (temp != NULL) {

        nextNode = temp->next;

        free(temp);

        temp = nextNode;

    }

 

    return 0;

}

Output

 
OUTPUT :

Enter data for node: 10
Do you want to add another node? (1-Yes / 0-No): 1
Enter data for node: 20
Do you want to add another node? (1-Yes / 0-No): 1
Enter data for node: 30
Do you want to add another node? (1-Yes / 0-No): 0

Linked List Elements:
10 -> 20 -> 30 -> NULL

Explanation

Step

Description

1

Define a struct Node containing data and next pointer.

2

Use malloc() to dynamically allocate memory for each new node.

3

Link each new node to the end of the list.

4

Continue until the user enters 0.

5

Display the complete linked list.

6

Free memory at the end to prevent leaks.

 

C Program: Linked List Deletion operations next (beginning, end, position)

Method 2: Linked List Deletion (Beginning, End, and Specific Position)

C

#include <stdio.h>

#include <stdlib.h>

 

// Structure definition for a node

struct Node {

    int data;

    struct Node *next;

};

 

// Function prototypes

void createList(struct Node **head);

void displayList(struct Node *head);

void deleteAtBeginning(struct Node **head);

void deleteAtEnd(struct Node **head);

void deleteAtPosition(struct Node **head, int position);

 

int main() {

    struct Node *head = NULL;

    int choice, position;

 

    // Step 1: Create initial list

    createList(&head);

 

    // Menu-driven deletion operations

    do {

        printf("\n--- Linked List Deletion Menu ---\n");

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

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

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

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

        printf("0. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

 

        switch (choice) {

            case 1:

                deleteAtBeginning(&head);

                break;

            case 2:

                deleteAtEnd(&head);

                break;

            case 3:

                printf("Enter position to delete: ");

                scanf("%d", &position);

                deleteAtPosition(&head, position);

                break;

            case 4:

                displayList(head);

                break;

            case 0:

                printf("Exiting program.\n");

                break;

            default:

                printf("Invalid choice!\n");

        }

 

    } while (choice != 0);

 

    return 0;

}

 

// Function to create a linked list

void createList(struct Node **head) {

    struct Node *newNode, *temp;

    int choice = 1;

 

    while (choice) {

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

        printf("Enter data for new node: ");

        scanf("%d", &newNode->data);

        newNode->next = NULL;

 

        if (*head == NULL)

            *head = temp = newNode;

        else {

            temp->next = newNode;

            temp = newNode;

        }

 

        printf("Add another node? (1-Yes / 0-No): ");

        scanf("%d", &choice);

    }

}

 

// Function to display linked list

void displayList(struct Node *head) {

    struct Node *temp = head;

    if (head == NULL) {

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

        return;

    }

    printf("\nCurrent Linked List:\n");

    while (temp != NULL) {

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

        temp = temp->next;

    }

    printf("NULL\n");

}

 

// Function to delete from beginning

void deleteAtBeginning(struct Node **head) {

    struct Node *temp;

    if (*head == NULL) {

        printf("List is empty, nothing to delete.\n");

        return;

    }

    temp = *head;

    *head = (*head)->next;

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

    free(temp);

}

 

// Function to delete from end

void deleteAtEnd(struct Node **head) {

    struct Node *temp, *prev;

 

    if (*head == NULL) {

        printf("List is empty, nothing to delete.\n");

        return;

    }

 

    if ((*head)->next == NULL) {

        printf("Deleted node with data: %d\n", (*head)->data);

        free(*head);

        *head = NULL;

        return;

    }

 

    temp = *head;

    while (temp->next != NULL) {

        prev = temp;

        temp = temp->next;

    }

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

    prev->next = NULL;

    free(temp);

}

 

// Function to delete node at specific position

void deleteAtPosition(struct Node **head, int position) {

    struct Node *temp = *head, *prev;

    int i = 1;

 

    if (*head == NULL) {

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

        return;

    }

 

    if (position == 1) {

        *head = temp->next;

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

        free(temp);

        return;

    }

 

    while (temp != NULL && i < position) {

        prev = temp;

        temp = temp->next;

        i++;

    }

 

    if (temp == NULL) {

        printf("Invalid position!\n");

        return;

    }

 

    prev->next = temp->next;

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

    free(temp);

}

Output

 
OUTPUT :

Enter data for new node: 10
Add another node? (1-Yes / 0-No): 1
Enter data for new node: 20
Add another node? (1-Yes / 0-No): 1
Enter data for new node: 30
Add another node? (1-Yes / 0-No): 0

--- Linked List Deletion Menu ---
1. Delete from Beginning
2. Delete from End
3. Delete from Specific Position
4. Display List
0. Exit
Enter your choice: 4
Current Linked List:
10 -> 20 -> 30 -> NULL

Enter your choice: 2
Deleted node with data: 30
Current Linked List:
10 -> 20 -> NULL


Explanation

Operation

Description

Delete from Beginning

Removes the first node and updates head.

Delete from End

Traverses to the last node and frees it.

Delete from Position

Deletes a node at any given index (1-based).

Menu-driven

Lets user choose which operation to perform interactively.