Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
Module 2 · syllabusAsked 1× in KTU papersLesson live

Circular linked lists

The last node points back to the first: no NULL at the end, and O(1) insertion at both ends with a single last pointer.

Syllabus says: “Circular Linked List”

On this page · 12 sections
ormakkurippu ✎
Never-forget card

Keep a pointer to the LAST node — then last→next is the FIRST. Front and end insert are the same 2 lines; the end version just moves last.

Picture it

Musical chairs in a circle: after the last chair comes the first one again, so there is no end to fall off.

Write this in the exam

In a circular singly linked list the last node's link points to the first node; keeping a pointer to the last node gives O(1) access to both the first and the last node.

What you will be able to do

  • Describe a circular linked list.
  • Insert a node at the beginning and at the end (Nov 2025 Q11b).
  • Traverse without an infinite loop.

Picture

first102030
The red arrow: last node (30) links back to the first (10).

Uses

  • Round-robin CPU scheduling — keep cycling through processes.
  • Music playlists on repeat.
  • Multiplayer turn order in games.

Now watch it run

Every frame comes from really running the algorithm on your input — change it and break it! Tap ▶ Play, or step one frame at a time with ◀ ▶.

Loading visualizer…

Algorithms (using a pointer to the last node)

algorithm
Algorithm InsertFront(x)
1. new ← getNode(x)
2. if last = NULL then new→link ← new;  last ← new      // single node points to itself
3. else new→link ← last→link;  last→link ← new

Algorithm InsertEnd(x)
1. new ← getNode(x)
2. if last = NULL then new→link ← new;  last ← new
3. else new→link ← last→link;  last→link ← new;  last ← new

Algorithm Display()
1. if last = NULL then print "Empty"; return
2. p ← last→link                  // first node
3. do  print p→data;  p ← p→link  while p ≠ last→link
DefinitionIf you keep a head pointer instead

Inserting at either end then needs a walk to the last node (O(n)) to update its link — that is why the ‘last’ pointer is preferred.

C implementation

circular_linked_list.ctested ✓ 160/160 cases
C
/* Circular singly linked list — KTU PCCST303, Module 2
 * We keep a pointer to the LAST node: last->next is the first node, so both
 * insert-at-front and insert-at-end are O(1) (KTU Nov 2025 Q11b).
 * Commands: IF x | IE x | D x | S x | T (count + largest)
 * After every command: optional result line, then "LIST: ..." (first → last).          */
#include <stdio.h>
#include <stdlib.h>

struct node {
    int data;
    struct node *next;
};
struct node *last = NULL;

struct node *getNode(int x) {
    struct node *n = malloc(sizeof *n);
    if (n == NULL) { printf("OUT OF MEMORY\n"); exit(1); }
    n->data = x;
    n->next = NULL;
    return n;
}

void insertFront(int x) {
    struct node *n = getNode(x);
    if (last == NULL) { n->next = n; last = n; return; }   /* one node points to itself */
    n->next = last->next;
    last->next = n;
}

void insertEnd(int x) {
    insertFront(x);          /* insert at the front ...                   */
    last = last->next;       /* ... and move last onto the new node: O(1) */
}

void deleteKey(int x) {
    struct node *prev, *p;
    if (last == NULL) { printf("UNDERFLOW\n"); return; }
    prev = last;
    p = last->next;
    while (p->data != x && p != last) { prev = p; p = p->next; }
    if (p->data != x) { printf("%d not found\n", x); return; }
    if (p == prev) last = NULL;                 /* it was the only node */
    else {
        prev->next = p->next;
        if (p == last) last = prev;
    }
    free(p);
}

void search(int x) {
    struct node *p;
    int pos = 1;
    if (last == NULL) { printf("not found (empty)\n"); return; }
    p = last->next;
    do {
        if (p->data == x) { printf("found at position %d\n", pos); return; }
        p = p->next;
        pos++;
    } while (p != last->next);
    printf("%d not found\n", x);
}

void traverse(void) {
    struct node *p;
    int count = 0, max;
    if (last == NULL) { printf("empty list\n"); return; }
    p = last->next;
    max = p->data;
    do {                      /* stop when we come back to the first node */
        count++;
        if (p->data > max) max = p->data;
        p = p->next;
    } while (p != last->next);
    printf("count = %d, largest = %d\n", count, max);
}

void display(void) {
    struct node *p;
    printf("LIST:");
    if (last != NULL) {
        p = last->next;
        do { printf(" %d", p->data); p = p->next; } while (p != last->next);
    }
    printf("\n");
}

int main(void) {
    char cmd[8];
    int a;
    while (scanf("%7s", cmd) == 1) {
        if (cmd[0] == 'I' && cmd[1] == 'F' && scanf("%d", &a) == 1) insertFront(a);
        else if (cmd[0] == 'I' && cmd[1] == 'E' && scanf("%d", &a) == 1) insertEnd(a);
        else if (cmd[0] == 'D' && scanf("%d", &a) == 1) deleteKey(a);
        else if (cmd[0] == 'S' && scanf("%d", &a) == 1) search(a);
        else if (cmd[0] == 'T') traverse();
        else { printf("BAD COMMAND\n"); break; }
        display();
    }
    while (last != NULL) {
        struct node *first = last->next;
        if (first == last) { free(first); last = NULL; }
        else { last->next = first->next; free(first); }
    }
    return 0;
}
Sample run · insert at front and end (Nov 2025 Q11b) · real output from gcc

Input (stdin)

IE 10
IE 20
IF 5
IE 30
T
D 30

Output (stdout)

LIST: 10
LIST: 10 20
LIST: 5 10 20
LIST: 5 10 20 30
count = 4, largest = 30
LIST: 5 10 20 30
LIST: 5 10 20

Open in the Code Lab →

Common mistakes

  • Using while (p ≠ NULL) → infinite loop.
  • Forgetting that a single node must point to itself.
  • Not moving last on insert at end.

Asked in KTU exams (1)

Quick check — can you get them all?

Question 1

Q1.With a pointer `last`, where is the first node?

Question 2

Q2.Insert at END = insert at FRONT plus…

Question 3

Q3.A traversal loop must stop when…

Practice

Every visualizer has a Practice mode: predict the next state before the animation reveals it. You can also work through the topic's question set on the practice page.

Exam-ready summary

  • last→link = first.
  • Front: new→link = last→link; last→link = new.
  • End: same, then last = new.
  • Traverse with do-while until back at first.

References

  • Horowitz, Sahni & Anderson-Freed, Fundamentals of Data Structures in C, 2/e — §4.4.4 (Circular list representation)

Explanations on this page are original. Textbook references point to where the standard treatment can be read; no text is reproduced from them.

Prefer revising from notes? Module-wise notes and question papers on Azhimathi Mayam (opens in a new tab)