C Program to Implement a Circular Linked List with Insertion, Deletion, and Traversal



#include <stdio.h>
#include <stdlib.h>

// Structure for a node in the circular linked list
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));
    if (newNode == NULL) {
        printf("Memory allocation failed!\n");
        exit(1);
    }
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// Function to insert a node at the front
void insertAtFront(struct Node** tail, int data) {
    struct Node* newNode = createNode(data);
    if (*tail == NULL) {
        *tail = newNode;
        newNode->next = newNode; // Point to itself
    } else {
        newNode->next = (*tail)->next; // New node points to head
        (*tail)->next = newNode; // Tail's next points to new node
    }
    printf("Inserted %d at front\n", data);
}

// Function to insert a node at the end
void insertAtEnd(struct Node** tail, int data) {
    struct Node* newNode = createNode(data);
    if (*tail == NULL) {
        *tail = newNode;
        newNode->next = newNode; // Point to itself
    } else {
        newNode->next = (*tail)->next; // New node points to head
        (*tail)->next = newNode; // Current tail points to new node
        *tail = newNode; // Update tail to new node
    }
    printf("Inserted %d at end\n", data);
}

// Function to delete the first node
void deleteFirst(struct Node** tail) {
    if (*tail == NULL) {
        printf("List is empty\n");
        return;
    }
    struct Node* head = (*tail)->next;
    int data = head->data;
    if (head == *tail) { // Only one node
        free(head);
        *tail = NULL;
    } else {
        (*tail)->next = head->next; // Tail points to second node
        free(head);
    }
    printf("Deleted %d from front\n", data);
}

// Function to delete the last node
void deleteLast(struct Node** tail) {
    if (*tail == NULL) {
        printf("List is empty\n");
        return;
    }
    struct Node* current = (*tail)->next;
    int data = (*tail)->data;
    if (current == *tail) { // Only one node
        free(*tail);
        *tail = NULL;
    } else {
        // Traverse to the second-to-last node
        while (current->next != *tail) {
            current = current->next;
        }
        current->next = (*tail)->next; // Point to head
        free(*tail);
        *tail = current; // Update tail
    }
    printf("Deleted %d from end\n", data);
}

// Function to delete a node by value
void deleteByValue(struct Node** tail, int data) {
    if (*tail == NULL) {
        printf("List is empty\n");
        return;
    }
    struct Node* current = (*tail)->next;
    struct Node* prev = *tail;
    // If head node has the data
    if (current->data == data) {
        deleteFirst(tail);
        return;
    }
    // Search for the node
    do {
        if (current->data == data) {
            prev->next = current->next;
            if (current == *tail) {
                *tail = prev; // Update tail if last node is deleted
            }
            free(current);
            printf("Deleted %d\n", data);
            return;
        }
        prev = current;
        current = current->next;
    } while (current != (*tail)->next);
    printf("Node with data %d not found\n", data);
}

// Function to traverse the circular linked list
void traverse(struct Node* tail) {
    if (tail == NULL) {
        printf("List is empty\n");
        return;
    }
    printf("Circular Linked List: ");
    struct Node* current = tail->next; // Start from head
    do {
        printf("%d ", current->data);
        current = current->next;
    } while (current != tail->next);
    printf("\n");
}

// Function to get the length of the list
int getLength(struct Node* tail) {
    if (tail == NULL) {
        return 0;
    }
    int length = 0;
    struct Node* current = tail->next;
    do {
        length++;
        current = current->next;
    } while (current != tail->next);
    return length;
}

// Main function to test the circular linked list operations
int main() {
    struct Node* tail = NULL;

    // Insert nodes
    insertAtFront(&tail, 10); // 10
    insertAtEnd(&tail, 20); // 10 -> 20
    insertAtFront(&tail, 5); // 5 -> 10 -> 20
    insertAtEnd(&tail, 30); // 5 -> 10 -> 20 -> 30
    traverse(tail);
    printf("Length: %d\n", getLength(tail));

    // Delete nodes
    deleteFirst(&tail); // 10 -> 20 -> 30
    traverse(tail);
    deleteLast(&tail); // 10 -> 20
    traverse(tail);
    deleteByValue(&tail, 20); // 10
    traverse(tail);

    // Test edge cases
    deleteByValue(&tail, 100); // Not found
    deleteFirst(&tail); // Empty
    traverse(tail);
    printf("Length: %d\n", getLength(tail));

    // Insert again to test
    insertAtEnd(&tail, 40); // 40
    insertAtFront(&tail, 50); // 50 -> 40
    traverse(tail);

    return 0;
}

Output:


Comments