Circular Single Linked List

A circular singly linked list is a linked list in which the last node points back to the first node in the list, forming a loop. This means that there is no end to the list, and we can traverse the list indefinitely by following the `next` pointers from one node to the next, starting at any node in the list.

Here's an example of a circular singly linked list node structure in C:

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

In a circular singly linked list, the `next` pointer of the last node points back to the first node, instead of being set to NULL. To create a circular singly linked list, we can simply set the `next` pointer of the last node to point to the head of the list. Here's an example of how to create a circular singly linked list with three nodes in C:

```
struct Node* createList() {
    struct Node* head = NULL;
    struct Node* second = NULL;
    struct Node* third = NULL;

    head = (struct Node*)malloc(sizeof(struct Node));
    second = (struct Node*)malloc(sizeof(struct Node));
    third = (struct Node*)malloc(sizeof(struct Node));
    
    head->data = 1;
    head->next = second;

    second->data = 2;
    second->next = third;

    third->data = 3;
    third->next = head; // point back to the head of the list

    return head;
}
```

In this code, we create three nodes with data values of 1, 2, and 3, respectively. We set the `next` pointer of each node to point to the next node in the list, and we set the `next` pointer of the last node (i.e., the third node) to point back to the head of the list. This creates a circular singly linked list with three nodes. Note that we return a pointer to the head of the list, just like in a regular singly linked list. We can traverse the circular singly linked list in the same way as a regular singly linked list, starting at any node in the list and following the `next` pointers until we reach the starting node again.



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