MCS-208 · Unit 3

Lists

Section 6 of 8

Block 1 · Unit 3Lists14 min · Section 6 of 8

3.6Circular Linked Lists

Covers IGNOU MCS-208 §3.6

In a circular linked list there is no end: the last node points back to the first. This suits data that is naturally processed in rounds, such as players taking turns or processes sharing a CPU. The circle also removes the NULL that tells a loop where to stop, so traversal needs a different stopping rule.

The structure

It is convenient to keep a pointer to the last node, last. Then the first node is last->next, so insertion at either end is O(1)O(1) with a single pointer.

A circular list reached through its last node
      +----+----+     +----+----+     +----+----+
 +--> | 10 |  --+---> | 20 |  --+---> | 30 |  --+--+
 |    +----+----+     +----+----+     +----+----+  |
 |                                       ^ last    |
 +-------------------------------------------------+

Traversal

A loop that waits for NULL would never stop. Instead, start at the first node and stop when you come back to it. A do … while loop visits the first node before testing:

Traversal of a circular list
if (last != NULL) {
    Node *p = last->next;            /* first node */
    do {
        visit(p->data);
        p = p->next;
    } while (p != last->next);       /* back at the start: stop */
}

Insertion and deletion

  • Insert at the front: n->next = last->next; last->next = n; (last does not change).
  • Insert at the end: the same two lines, then last = n.
  • Empty list: the new node points to itself: n->next = n; last = n;.
  • Delete a node: find its predecessor prev (going round at most once), set prev->next = p->next, and if p was the last node set last = prev; if p was the only node, the list becomes empty (last = NULL).

Uses of circular lists

  • Round-robin scheduling: the operating system gives each process a time slice in turn, going round the circle of ready processes.
  • Turn-based games and repeating playlists, where after the last item comes the first again.
  • Circular buffers for streaming data (often in an array, as in Unit 5’s circular queue).

Key takeaways

  • The last node points to the first; the empty list is last == NULL (or head == NULL).
  • A pointer to the last node gives O(1)O(1) access to both ends.
  • Traverse with do … while (p != first); never wait for NULL.
  • When the first node changes, also update last->next.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

In a non-empty circular list accessed through last, which expression gives the first node?

Show answer

Answer: B. last->next

The last node’s link closes the circle by pointing to the first node. This is why one pointer to the last node gives access to both ends.

Question 2 · multiple choice

Why is a do … while loop used to traverse a circular list from its first node?

Show answer

Answer: C. Because the stopping test “back at the first node” is true at the start, so the test must come after the first visit

A while (p != first) loop starting at first would stop immediately and visit nothing. There is no NULL in a circular list.

Question 3 · multiple choice

What does this program print?

C · q3-circle.c
#include <stdio.h>

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

int main(void)
{
    Node a = {1, NULL}, b = {2, NULL}, c = {3, NULL};
    a.next = &b; b.next = &c; c.next = &a;      /* circle */
    Node *p = &b;
    int steps = 0;
    do {
        printf("%d ", p->data);
        p = p->next;
        steps++;
    } while (p != &b);
    printf("(%d steps)\n", steps);
    return 0;
}
Show answer

Answer: C. 2 3 1 (3 steps)

A circular list can be traversed from any node; starting at 2, it visits 2, 3, 1 and stops when it is back at 2. Every node is visited exactly once.

Question 4 · multiple choice

A new node n is inserted at the front of a non-empty circular list accessed through last. Which statements are needed?

Show answer

Answer: D. n->next = last->next; last->next = n;

n must point to the old first node, then the last node points to n. The third option changes last->next first, so n ends up pointing to itself.

Question 5 · multiple choice

What happens if a circular list’s search loop is written as while (p != NULL && p->data != key) and the key is missing?

Show answer

Answer: C. It loops forever, because no link in a circular list is NULL.

p keeps going round the circle. The loop must stop when it comes back to the starting node.

Question 6 · short answer

Can a doubly linked list be made circular? Explain, and give two differences between a doubly linked list and a circular linked list (Check Your Progress, Q2 and Q3).

Show worked answer

Yes. In a circular doubly linked list the right link of the last node points to the first node and the left link of the first node points to the last node, so there is no NULL and the list can be walked round in both directions. Differences between an ordinary doubly linked list and a (singly) circular linked list: (1) a doubly linked node has two links (left and right), a circular singly linked node has one; (2) a doubly linked list has NULL at both ends, a circular list has no NULL because the last node points to the first; (3) a doubly linked list can be traversed backwards, a singly circular list only forwards (but it can start anywhere and go round); (4) traversal stops at NULL in a doubly linked list, but when it returns to the start node in a circular list.

↑ ↓ move↵ openTab switch type