Bird
0
0
DSA Cprogramming

Create a Circular Singly Linked List in DSA C

Choose your learning style9 modes available
Mental Model
A circular singly linked list is like a chain where the last link connects back to the first, making a loop.
Analogy: Imagine a round table where each person holds hands with the next, and the last person holds hands with the first, so everyone forms a circle.
head -> [1] -> [2] -> [3] ->
          ↑               ↓
          ← ← ← ← ← ← ← ← ←
Dry Run Walkthrough
Input: Create a circular singly linked list with nodes 1, 2, 3 in order
Goal: Build a linked list where the last node points back to the first node, forming a circle
Step 1: Create node with value 1 and set head to it
head -> [1] -> null
Why: Start the list with the first node
Step 2: Create node with value 2 and link it after node 1
head -> [1] -> [2] -> null
Why: Add second node to the list
Step 3: Create node with value 3 and link it after node 2
head -> [1] -> [2] -> [3] -> null
Why: Add third node to the list
Step 4: Make node 3 point back to head node 1
head -> [1] -> [2] -> [3] -> head (1)
Why: Close the loop to form a circular list
Result:
head -> [1] -> [2] -> [3] -> head (1)
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

// Node structure for circular singly linked list
typedef struct Node {
    int data;
    struct Node* next;
} Node;

// Function to create a circular singly linked list with given array of values
Node* createCircularList(int arr[], int n) {
    if (n == 0) return NULL; // empty list

    Node* head = (Node*)malloc(sizeof(Node));
    head->data = arr[0];
    Node* curr = head;

    // Create and link nodes for remaining elements
    for (int i = 1; i < n; i++) {
        Node* newNode = (Node*)malloc(sizeof(Node));
        newNode->data = arr[i];
        curr->next = newNode; // link current node to new node
        curr = newNode;       // move current pointer forward
    }

    curr->next = head; // last node points back to head to form circle
    return head;
}

// Function to print circular singly linked list
void printCircularList(Node* head) {
    if (head == NULL) {
        printf("List is empty\n");
        return;
    }

    Node* curr = head;
    do {
        printf("%d -> ", curr->data);
        curr = curr->next;
    } while (curr != head);
    printf("(back to head)\n");
}

int main() {
    int values[] = {1, 2, 3};
    int n = sizeof(values) / sizeof(values[0]);

    Node* circularList = createCircularList(values, n);
    printCircularList(circularList);

    return 0;
}
if (n == 0) return NULL; // empty list
handle empty input array by returning NULL
head->data = arr[0];
initialize head node with first element
curr->next = newNode; // link current node to new node
link previous node to new node to extend list
curr = newNode; // move current pointer forward
advance current pointer to newly added node
curr->next = head; // last node points back to head to form circle
close the list by linking last node back to head
do { ... } while (curr != head);
traverse and print nodes until we loop back to head
OutputSuccess
1 -> 2 -> 3 -> (back to head)
Complexity Analysis
Time: O(n) because we create and link each of the n nodes once
Space: O(n) because we allocate memory for n nodes
vs Alternative: Compared to a linear singly linked list, circular list requires one extra pointer assignment to close the loop, but traversal can be infinite if not careful
Edge Cases
empty input array
function returns NULL and prints 'List is empty'
DSA C
if (n == 0) return NULL; // empty list
single element array
creates one node that points to itself forming a single-node circle
DSA C
curr->next = head; // last node points back to head to form circle
When to Use This Pattern
When you need a list where you can cycle through elements repeatedly without stopping, use a circular singly linked list to avoid null ends.
Common Mistakes
Mistake: Forgetting to link the last node back to the head, leaving the list linear
Fix: Add the line 'curr->next = head;' after creating all nodes to close the circle
Mistake: Using a while loop that checks for NULL to print nodes, causing infinite loop in circular list
Fix: Use a do-while loop that stops when current node returns to head
Summary
Creates a linked list where the last node points back to the first, forming a circle.
Use when you want to cycle through elements endlessly without a null end.
The key is linking the last node back to the head to close the loop.