C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Data Structures in C

Circular linked list - Deletion Operations

CIRCULAR LINKED LIST — DELETION OPERATIONS

We’ll cover:

  1. Delete from beginning
  2. Delete from end
  3. Delete from a specific position

 

Structure Definition

#include <stdio.h>

#include <stdlib.h>

 

struct Node {

    int data;

    struct Node *next;

};

Each node contains:

  • data → stores value
  • next → points to the next node (and the last node links back to head)

Helper Function: Display List

void display(struct Node *head) {

    struct Node *temp = head;

    if (head == NULL) {

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

        return;

    }

 

    printf("Circular Linked List: ");

    do {

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

        temp = temp->next;

    } while (temp != head);

    printf("(back to head)\n");

}

 

Delete Node from Beginning

struct Node* deleteFromBeginning(struct Node *head) {

    struct Node *temp = head, *last;

 

    if (head == NULL) {

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

        return NULL;

    }

 

    // If only one node

    if (head->next == head) {

        free(head);

        return NULL;

    }

 

    // Traverse to last node

    last = head;

    while (last->next != head)

        last = last->next;

 

    // Adjust links

    last->next = head->next;

    head = head->next;

 

    // Delete old head

    free(temp);

 

    printf("Deleted node from beginning.\n");

    return head;

}

Explanation:

  • If only one node: free it and return NULL.
  • Otherwise:
    • Find last node.
    • Point last->next to head->next.
    • Move head to the next node.
    • Free old head.

Before:

10 → 20 → 30 → back to 10

After deleting 10:

20 → 30 → back to 20

Delete Node from End

struct Node* deleteFromEnd(struct Node *head) {

    struct Node *temp = head, *prev;

 

    if (head == NULL) {

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

        return NULL;

    }

 

    // If only one node

    if (head->next == head) {

        free(head);

        return NULL;

    }

 

    // Traverse to last node

    while (temp->next != head) {

        prev = temp;

        temp = temp->next;

    }

 

    prev->next = head;

    free(temp);

 

    printf("Deleted node from end.\n");

    return head;

}

Explanation:

  • Traverse to the last node keeping track of the previous node.
  • Update prev->next = head.
  • Free the last node.

Before:

10 → 20 → 30 → back to 10

After deleting 30:

10 → 20 → back to 10

Delete Node from Specific Position

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

    struct Node *temp = head, *prev;

    int i;

 

    if (head == NULL) {

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

        return NULL;

    }

 

    // Delete first node

    if (pos == 1)

        return deleteFromBeginning(head);

 

    // Traverse to position

    for (i = 1; i < pos && temp->next != head; i++) {

        prev = temp;

        temp = temp->next;

    }

 

    // If invalid position

    if (temp == head) {

        printf("Invalid position!\n");

        return head;

    }

 

    prev->next = temp->next;

    free(temp);

 

    printf("Deleted node at position %d.\n", pos);

    return head;

}

Explanation:

  • If deleting the first node → reuse deleteFromBeginning().
  • Otherwise, traverse to position pos.
  • Adjust pointers to skip the deleted node.
  • Free memory.

Before:

10 → 20 → 30 → 40 → back to 10

Delete node at position 3
After:

10 → 20 → 40 → back to 10

 

C Program: Deletion Operations in Circular Linked List

C

#include <stdio.h>

#include <stdlib.h>

 

// Define structure

struct Node {

    int data;

    struct Node *next;

};

 

// Function to display circular linked list

void display(struct Node *head) {

    struct Node *temp = head;

    if (head == NULL) {

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

        return;

    }

 

    printf("\nCircular Linked List: ");

    do {

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

        temp = temp->next;

    } while (temp != head);

    printf("(back to head)\n");

}

 

// Function to create circular linked list

struct Node* createList(int n) {

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

    int data, i;

 

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

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

        if (newNode == NULL) {

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

            return head;

        }

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

        scanf("%d", &data);

        newNode->data = data;

        newNode->next = NULL;

 

        if (head == NULL) {

            head = newNode;

            head->next = head;

            temp = head;

        } else {

            temp->next = newNode;

            newNode->next = head;

            temp = newNode;

        }

    }

    return head;

}

 

// Delete node from beginning

struct Node* deleteFromBeginning(struct Node *head) {

    struct Node *temp = head, *last;

 

    if (head == NULL) {

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

        return NULL;

    }

 

    // Only one node

    if (head->next == head) {

        free(head);

        return NULL;

    }

 

    // Traverse to last node

    last = head;

    while (last->next != head)

        last = last->next;

 

    // Adjust links

    last->next = head->next;

    head = head->next;

 

    free(temp);

    printf("\nDeleted node from beginning.\n");

    return head;

}

 

// Delete node from end

struct Node* deleteFromEnd(struct Node *head) {

    struct Node *temp = head, *prev;

 

    if (head == NULL) {

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

        return NULL;

    }

 

    // Only one node

    if (head->next == head) {

        free(head);

        return NULL;

    }

 

    // Traverse to last node

    while (temp->next != head) {

        prev = temp;

        temp = temp->next;

    }

 

    prev->next = head;

    free(temp);

    printf("\nDeleted node from end.\n");

    return head;

}

 

// Delete node from specific position

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

    struct Node *temp = head, *prev;

    int i;

 

    if (head == NULL) {

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

        return NULL;

    }

 

    // Delete first node

    if (pos == 1)

        return deleteFromBeginning(head);

 

    for (i = 1; i < pos && temp->next != head; i++) {

        prev = temp;

        temp = temp->next;

    }

 

    // Invalid position

    if (temp == head) {

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

        return head;

    }

 

    prev->next = temp->next;

    free(temp);

    printf("\nDeleted node at position %d.\n", pos);

    return head;

}

 

// Main function

int main() {

    struct Node *head = NULL;

    int n, choice, pos;

 

    printf("Enter number of nodes: ");

    scanf("%d", &n);

    head = createList(n);

 

    printf("\nInitial List:\n");

    display(head);

 

    do {

        printf("\n----- 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("5. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

 

        switch (choice) {

            case 1:

                head = deleteFromBeginning(head);

                display(head);

                break;

            case 2:

                head = deleteFromEnd(head);

                display(head);

                break;

            case 3:

                printf("Enter position to delete: ");

                scanf("%d", &pos);

                head = deleteFromPosition(head, pos);

                display(head);

                break;

            case 4:

                display(head);

                break;

            case 5:

                printf("Exiting program.\n");

                break;

            default:

                printf("Invalid choice! Try again.\n");

        }

    } while (choice != 5);

 

    return 0;

}

Output

 
Enter 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

Initial List:
Circular Linked List: 10 -> 20 -> 30 -> 40 -> (back to head)

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

Deleted node from beginning.
Circular Linked List: 20 -> 30 -> 40 -> (back to head)

Enter your choice: 3
Enter position to delete: 2
Deleted node at position 2.
Circular Linked List: 20 -> 40 -> (back to head)

Program Explanation

  1. Creation
  • The createList() function dynamically allocates memory using malloc() and links all nodes circularly.
  1. Deletion Functions
  • deleteFromBeginning() → removes the first node and updates head.
  • deleteFromEnd() → finds last node, updates link to head, and frees memory.
  • deleteFromPosition() → deletes node at a specific index.
  1. Display Function
  • Traverses circularly from head and prints all nodes until it loops back.
  1. Menu
  • Lets the user perform deletions multiple times interactively.