Lesson 10 +30 XP

Circular Linked Lists (Singly & Doubly)

Circular Linked Lists (Singly & Doubly)

In an ordinary linked list the last node points to NULL - the list has a dead end. In a circular linked list the last node points back to the first, so the nodes form a ring. From any node you can reach every other node, and a single pointer to the last node gives O(1) access to both ends.

1. Circular Singly Linked List

A circular singly linked list (CLL) is a singly linked list in which the LINK of the last node contains the address of the first node instead of NULL.

DIAGRAM
        +-------------------------------------------+
        v                                           |
 START-->[10 | *]--->[20 | *]--->[30 | *]--->[40 | *]
                                              LAST
  • There is no NULL link anywhere (except when the list is empty: START = NULL).
  • The end of a traversal is detected by coming back to the starting node, not by NULL.
  • A single node points to itself: LINK[START] = START.

Keeping a pointer to the LAST node

Many implementations keep only LAST instead of START, because LINK[LAST] is the first node:

  • first node = LINK[LAST] - O(1)
  • last node = LAST - O(1)

So insertion at both the beginning and the end becomes O(1).

2. Traversal

A while (PTR ≠ NULL) loop would never stop. Instead we process the first node, then continue until PTR returns to START - a do-while loop.

Algorithm CLL_TRAVERSE(START)
if START = NULL then
    Write "List is empty" and Exit
PTR ← START
repeat
    PROCESS(INFO[PTR])
    PTR ← LINK[PTR]
until PTR = START             // back where we began
Exit

3. Insertion at the Beginning

Using START only: the last node must be found (to change its link), so this is O(n).

PSEUDO
Algorithm CLL_INSFIRST(START, ITEM)
NEW ← new node; INFO[NEW] ← ITEM
if START = NULL then
    LINK[NEW] ← NEW           // single node points to itself
    START ← NEW
    Exit
PTR ← START
while LINK[PTR] ≠ START do    // find the last node
    PTR ← LINK[PTR]
LINK[NEW] ← START             // new node → old first
LINK[PTR] ← NEW               // last node → new first
START ← NEW
Exit

Using a LAST pointer: no search at all - O(1).

PSEUDO
Algorithm CLL_INSFIRST_LAST(LAST, ITEM)
NEW ← new node; INFO[NEW] ← ITEM
if LAST = NULL then
    LINK[NEW] ← NEW
    LAST ← NEW
else
    LINK[NEW] ← LINK[LAST]    // new node → old first node
    LINK[LAST] ← NEW          // last node → new node
Exit

4. Insertion at the End

With a LAST pointer, inserting at the end is the same as inserting at the beginning, followed by moving LAST to the new node.

Algorithm CLL_INSEND_LAST(LAST, ITEM)
CLL_INSFIRST_LAST(LAST, ITEM) // new node is now right after LAST
LAST ← LINK[LAST]             // ... so make it the new last node
Exit
DIAGRAM
 Before:  first-->[10]-->[20]-->[30]--+       LAST = 30
            ^                         |
            +-------------------------+

 After:   first-->[10]-->[20]-->[30]-->[40]--+  LAST = 40
            ^                                |
            +--------------------------------+

Complexity: O(1).

5. Deletion of the First Node

PSEUDO
Algorithm CLL_DELFIRST(LAST)
if LAST = NULL then
    Write "UNDERFLOW" and Exit
FIRST ← LINK[LAST]
if FIRST = LAST then          // only one node
    LAST ← NULL
else
    LINK[LAST] ← LINK[FIRST]  // last node skips the old first
free(FIRST)
Exit

Complexity: O(1) with LAST.

6. Deletion of the Last Node

The new last node is the predecessor of LAST, which must be found by walking round the ring.

PSEUDO
Algorithm CLL_DELLAST(LAST)
if LAST = NULL then
    Write "UNDERFLOW" and Exit
if LINK[LAST] = LAST then     // only one node
    free(LAST); LAST ← NULL; Exit
PTR ← LINK[LAST]              // start at the first node
while LINK[PTR] ≠ LAST do     // stop at the second-last node
    PTR ← LINK[PTR]
LINK[PTR] ← LINK[LAST]        // second-last → first
free(LAST)
LAST ← PTR
Exit

Complexity: O(n).

7. Circular Doubly Linked List

A circular doubly linked list (CDLL) combines both ideas: every node has PREV and NEXT, the last node's NEXT points to the first, and the first node's PREV points to the last.

DIAGRAM
     +------------------------------------------+
     |                                          v
   [10] <=> [20] <=> [30] <=> [40]      NEXT[40] = 10
     ^                          |       PREV[10] = 40
     +--------------------------+
  • Both ends are reachable in O(1) from START: the last node is PREV[START].
  • There are no NULL checks for the ends, so the insert and delete code has fewer special cases.
  • Insert or delete at the front, at the back, or of a known node: all O(1).
PSEUDO
Algorithm CDLL_INSEND(START, ITEM)
NEW ← new node; INFO[NEW] ← ITEM
if START = NULL then
    NEXT[NEW] ← NEW; PREV[NEW] ← NEW
    START ← NEW; Exit
LAST ← PREV[START]            // O(1): no walk needed
NEXT[NEW] ← START
PREV[NEW] ← LAST
NEXT[LAST] ← NEW
PREV[START] ← NEW
Exit

8. Circular Header Linked List

A circular header list has a header node, and the last node links back to the header (not to the first data node). The list is empty when LINK[HEAD] = HEAD. Traversal starts at LINK[HEAD] and stops when it returns to HEAD, so the code needs no special case for an empty list. Polynomials are often stored this way (Unit II).

9. Comparison of Linked List Variants

VariantLinks per nodeLast node's linkInsert at frontInsert at endDelete known node
Singly (SLL)1NULLO(1)O(n), O(1) with tailO(n)
Circular singly (CLL) with LAST1First nodeO(1)O(1)O(n)
Doubly (DLL) with tail2NULLO(1)O(1)O(1)
Circular doubly (CDLL)2First nodeO(1)O(1)O(1)

10. Advantages, Disadvantages and Applications

AdvantagesDisadvantages
Any node can be the starting point; the whole list is reachable from itCareless traversal loops forever (no NULL to stop at)
O(1) insertion at both ends with one LAST pointerReversing and some deletions are slightly more complex
Natural fit for anything that repeats in a cycleEnds are harder to see while debugging

Applications

  • Round-robin CPU scheduling: ready processes sit in a ring; each gets a time slice in turn.
  • Multiplayer games: turns pass around the players in a cycle.
  • Music playlist on repeat: after the last song comes the first.
  • Circular buffers / queues in networking and keyboard input.
  • Josephus problem: people stand in a circle and every k-th one is removed until one remains - simulated directly with a CLL.
💡 Tip

Key points to remember - Last node links back to the first; traversal ends when PTR returns to START (do-while). - Keep a pointer to the LAST node: first = LINK[LAST], so both ends are O(1). - Insert at end = insert at front + move LAST forward. - CDLL: PREV[START] is the last node; all end operations are O(1) with no NULL checks.

🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. What is a circular linked list? Write algorithms to insert a node at the beginning and at the end of a circular linked list. (7.5 Marks)
  • Write: the definition and ring diagram, why a LAST pointer is used, CLL_INSFIRST_LAST and CLL_INSEND_LAST, and O(1) complexity.
  • Q2. Write an algorithm to delete the first node of a circular linked list. (5 Marks)
  • Q3. Explain circular doubly linked lists. What are their advantages? Give applications of circular lists. (5 Marks)
  • Write: the CDLL diagram, PREV[START] = last node, no NULL checks, round-robin scheduling and playlists.