Circular Single linked list with header node

Here's an example implementation of a circular single linked list in C:

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

struct Node {
    int data;
    struct Node* next;
};

void insertNode(struct Node** headRef, int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    if (*headRef == NULL) {
        *headRef = newNode;
        newNode->next = *headRef;
    } else {
        struct Node* current = *headRef;
        while (current->next != *headRef) {
            current = current->next;
        }
        current->next = newNode;
        newNode->next = *headRef;
    }
}

void deleteNode(struct Node** headRef, int data) {
    if (*headRef == NULL) {
        return;
    }
    struct Node* current = *headRef;
    struct Node* previous = NULL;
    do {
        if (current->data == data) {
            if (previous == NULL) {
                *headRef = current->next;
            } else {
                previous->next = current->next;
            }
            if (current == *headRef) {
                *headRef = previous;
            }
            free(current);
            return;
        }
        previous = current;
        current = current->next;
    } while (current != *headRef);
}

void printList(struct Node* head) {
    struct Node* current = head;
    printf("List contents: ");
    if (head != NULL) {
        do {
            printf("%d ", current->data);
            current = current->next;
        } while (current != head);
    }
    printf("\n");
}

int main() {
    struct Node* head = NULL;
    insertNode(&head, 5);
    insertNode(&head, 7);
    insertNode(&head, 3);
    printList(head);
    deleteNode(&head, 7);
    printList(head);
    return 0;
}
```

In this code, we define a `Node` structure that represents a single node in the list, with a `data` field and a `next` pointer that points to the next node in the list.

The `insertNode` function inserts a new node with the specified data value at the end of the circular list. If the list is currently empty, we set the `headRef` pointer to point to the new node, and set the `next` pointer of the new node to point to itself. Otherwise, we traverse the list to find the last node, and set its `next` pointer to point to the new node. We then set the `next` pointer of the new node to point to the head of the list.

The `deleteNode` function deletes the first node with the specified data value from the list. We traverse the list until we find a node with the specified data value, keeping track of the previous node as we go. If we find the node to be deleted, we update the `next` pointer of the previous node to point to the node after the one we are deleting. If the node being deleted is the head of the list, we update the `headRef` pointer to point to the previous node. We then free the memory used by the deleted node.

The `printList` function prints the data values of all nodes in the list.

In the `main` function, we create a circular list, insert three nodes into the list, print the contents of the list, delete a node from the list, and print the contents of the updated list.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext