C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Data Structures in C

Circular linked list - Insertion, Deletion, Traversal Operations

Here’s a complete, fully functional C program that implements all major operations on a Circular Linked List, including:

  • Creation
  • Insertion (beginning, end, specific position)
  • Deletion (beginning, end, specific position)
  • Traversal & Display

Everything is integrated into one menu-driven program for easy execution and understanding — perfect for practicals

 

C Program: Insertion, Deletion, Traversal Operations in Circular Linked List

C

#include <stdio.h>

#include <stdlib.h>

 

// Define structure for a node

struct Node {

    int data;

    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->next = NULL;

    return newNode;

}

 

// Function to display circular linked list

void display(struct Node *head) {

    struct Node *temp = head;

    if (head == NULL) {

        printf("\nList 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 insert node at beginning

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

    struct Node *newNode = createNode(data);

    struct Node *temp = head;

 

    if (head == NULL) {

        newNode->next = newNode; // circular link

        head = newNode;

        return head;

    }

 

    // Find last node

    while (temp->next != head)

        temp = temp->next;

 

    temp->next = newNode;

    newNode->next = head;

    head = newNode;

 

    printf("\nInserted %d at beginning.\n", data);

    return head;

}

 

// Function to insert node at end

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

    struct Node *newNode = createNode(data);

    struct Node *temp = head;

 

    if (head == NULL) {

        newNode->next = newNode;

        head = newNode;

        return head;

    }

 

    while (temp->next != head)

        temp = temp->next;

 

    temp->next = newNode;

    newNode->next = head;

 

    printf("\nInserted %d at end.\n", data);

    return head;

}

 

// Function to insert node at specific position

struct Node* insertAtPosition(struct Node *head, int data, int pos) {

    struct Node *newNode = createNode(data);

    struct Node *temp = head;

    int i;

 

    if (pos == 1)

        return insertAtBeginning(head, data);

 

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

        temp = temp->next;

 

    if (temp->next == head && i < pos - 1) {

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

        free(newNode);

        return head;

    }

 

    newNode->next = temp->next;

    temp->next = newNode;

 

    printf("\nInserted %d at position %d.\n", data, pos);

    return head;

}

 

// Function to delete node from beginning

struct Node* deleteFromBeginning(struct Node *head) {

    struct Node *temp = head, *last;

 

    if (head == NULL) {

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

        return NULL;

    }

 

    // Only one node

    if (head->next == head) {

        free(head);

        return NULL;

    }

 

    last = head;

    while (last->next != head)

        last = last->next;

 

    head = head->next;

    last->next = head;

    free(temp);

 

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

    return head;

}

 

// Function to delete node from end

struct Node* deleteFromEnd(struct Node *head) {

    struct Node *temp = head, *prev;

 

    if (head == NULL) {

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

        return NULL;

    }

 

    // Only one node

    if (head->next == head) {

        free(head);

        return NULL;

    }

 

    while (temp->next != head) {

        prev = temp;

        temp = temp->next;

    }

 

    prev->next = head;

    free(temp);

 

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

    return head;

}

 

// Function to delete node from specific position

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

    struct Node *temp = head, *prev;

    int i;

 

    if (head == NULL) {

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

        return NULL;

    }

 

    if (pos == 1)

        return deleteFromBeginning(head);

 

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

        prev = temp;

        temp = temp->next;

    }

 

    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;

}

 

// Function to create list

struct Node* createList(int n) {

    struct Node *head = NULL;

    int data, i;

 

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

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

        scanf("%d", &data);

        head = insertAtEnd(head, data);

    }

    return head;

}

 

// Main function

int main() {

    struct Node *head = NULL;

    int choice, data, pos, n;

 

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

    scanf("%d", &n);

    head = createList(n);

 

    do {

        printf("\n========= CIRCULAR LINKED LIST MENU =========\n");

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

        printf("2. Insert at Beginning\n");

        printf("3. Insert at End\n");

        printf("4. Insert at Position\n");

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

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

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

        printf("8. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

 

        switch (choice) {

            case 1:

                display(head);

                break;

 

            case 2:

                printf("Enter data: ");

                scanf("%d", &data);

                head = insertAtBeginning(head, data);

                break;

 

            case 3:

                printf("Enter data: ");

                scanf("%d", &data);

                head = insertAtEnd(head, data);

                break;

 

            case 4:

                printf("Enter data: ");

                scanf("%d", &data);

                printf("Enter position: ");

                scanf("%d", &pos);

                head = insertAtPosition(head, data, pos);

                break;

 

            case 5:

                head = deleteFromBeginning(head);

                break;

 

            case 6:

                head = deleteFromEnd(head);

                break;

 

            case 7:

                printf("Enter position: ");

                scanf("%d", &pos);

                head = deleteFromPosition(head, pos);

                break;

 

            case 8:

                printf("\nExiting program.\n");

                break;

 

            default:

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

        }

    } while (choice != 8);

 

    return 0;

}

Output

 
Enter number of initial nodes: 3
Enter data for node 1: 10
Enter data for node 2: 20
Enter data for node 3: 30

========= CIRCULAR LINKED LIST MENU =========
1. Display List
2. Insert at Beginning
3. Insert at End
4. Insert at Position
5. Delete from Beginning
6. Delete from End
7. Delete from Position
8. Exit
Enter your choice: 1

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

Enter your choice: 2
Enter data: 5
Inserted 5 at beginning.

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

Enter your choice: 6
Deleted node from end.
Circular Linked List: 5 -> 10 -> 20 -> (back to head)

Explanation

Key Functions

Function

Purpose

createNode()

Allocates and initializes a new node

display()

Traverses and prints the circular list

insertAtBeginning()

Adds node before the head

insertAtEnd()

Adds node after the last node

insertAtPosition()

Adds node at a specific location

deleteFromBeginning()

Removes first node

deleteFromEnd()

Removes last node

deleteFromPosition()

Removes node from a specific position