Circular Linked List in C

Written: August 22, 2026

In a circular list the last node points back to the head, so traversal must stop after returning to start.

This walkthrough keeps the logic small and readable so you can type it, run it, and then change one input at a time to see what happens.

What you will build

You will create nodes, link the tail to head, and print values once around the circle.

  • Define node with data + next
  • Insert at end updating tail->next = head
  • Traverse with do/while until back at head

Working C example

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

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

struct node *head = NULL;

void insert_end(int x) {
    struct node *n = malloc(sizeof *n);
    struct node *t;
    n->data = x;
    if (!head) {
        head = n;
        n->next = head;
        return;
    }
    t = head;
    while (t->next != head) t = t->next;
    t->next = n;
    n->next = head;
}

void display(void) {
    struct node *t;
    if (!head) return;
    t = head;
    do {
        printf("%d ", t->data);
        t = t->next;
    } while (t != head);
    printf("\n");
}

int main(void) {
    insert_end(10);
    insert_end(20);
    insert_end(30);
    display();
    return 0;
}

Advertisement

How the logic works

Read the program top to bottom: includes and main first, then the statements that change variables, then the print that proves the result.

If your output looks wrong, print the variables before and after the critical lines. That single habit catches most beginner bugs faster than rewriting the whole file.

Never use a plain while(t) loop on a circular list — it never hits NULL.

Advertisement

Common mistakes

Forgetting a semicolon, using the wrong format specifier in printf/scanf, and mixing up assignment (=) with comparison (==) are the usual culprits.

Compile with warnings enabled (`gcc -Wall`) so the compiler points at risky casts and unused variables before you chase them by hand.

Try this next

Change the sample inputs, add a second test case, and briefly note what stayed the same. Teaching yourself with tiny experiments sticks better than copying a longer program you never run.

When you can explain every line without looking, you are ready for the next exercise in the series.

Keep learning

If this walkthrough on circular linked list in c helped, open the code again and change one input or assumption. Small experiments beat rereading the same example.

Want more step-by-step tutorials like this? Browse blog.xqa.io — and tell us which topic you want next.

Advertisement

Previous Article

Find a Character in a Given String in C

Next Article

C Program to Read Structure Elements from the Keyboard

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨