C Circular Linked Lists

Intermediate
6 min read
Updated: Nov 2026
1 example

A List That Loops Back

In a circular linked list the last node's next points to the first node, so there is no NULL end. Traversal uses do { ... } while (p != head) to visit each node once. Circular lists suit round-robin scheduling, buffers, and games where play wraps around.

Remember
last->next = head;            /* close the circle */
struct Node *p = head;
do {
    /* use p->data */
    p = p->next;
} while (p != head);

Cheat Sheet

Keep these facts handy while you work with Circular Linked Lists in C.

TopicDetail
No NULLLoop ends when you return to the start
Loopdo { } while (p != head)
UseRound robin, ring buffers
DangerInfinite loop if you test for NULL

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Stacks.

Example 1 — One lap and five steps around a ring

Three nodes linked into a circle.

C
#include <stdio.h>

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

int main(void) {
    struct Node a = {1, NULL}, b = {2, NULL}, c = {3, NULL};
    a.next = &b;
    b.next = &c;
    c.next = &a;

    struct Node *p = &a;
    printf("one lap:");
    do {
        printf(" %d", p->data);
        p = p->next;
    } while (p != &a);

    printf("\nfive steps:");
    p = &a;
    for (int i = 0; i < 5; i++) {
        printf(" %d", p->data);
        p = p->next;
    }
    printf("\n");
    return 0;
}

How It Works

  1. c.next = &a closes the circle.
  2. The do-while runs once before testing, then stops when it returns to a.
  3. Counting steps lets you wrap around forever without special cases.

Edge Cases & Pitfalls

Check these before relying on Circular Linked Lists in real programs.

inf

Looping until NULL

A circular list never reaches NULL, so while (p) loops forever.

free

Freeing a circle

Break the circle (set the last next to NULL) or count nodes before freeing.

Key Takeaways

  • No end: stop by returning to the start.
  • do-while: visit at least once.
  • Free carefully.

One line: In a circular linked list the last node's next points to the first node, so there is no NULL end.

Frequently Asked Questions

A linked list where the last node points back to the first node, forming a loop.
Compare the current pointer to the starting node; when they are equal again, you have completed one lap.
Continue with "C Stacks" next. Implement LIFO stacks in C with arrays and linked lists, and use them for expressions and parsing. The C Linked Lists hub lists every topic in order.

Next: Stacks

Implement LIFO stacks in C with arrays and linked lists, and use them for expressions and parsing.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful