codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🧮

Data Structures & Algorithms

6 / 65 topics
3Arrays4Linked Lists5Doubly Linked Lists6Circular Linked Lists
Tutorials/Data Structures & Algorithms/Circular Linked Lists
🧮Data Structures & Algorithms

Circular Linked Lists

Updated 2026-04-20
3 min read

Introduction

In this section, we will delve into Circular Linked Lists, a variant of linked lists where the last node points back to the first node instead of pointing to null. This unique structure offers several advantages and is used in various applications such as implementing circular buffers, round-robin scheduling algorithms, and more.

What is a Circular Linked List?

A Circular Linked List is a data structure consisting of nodes where each node contains two parts: the data part and the reference (or link) to the next node. Unlike a singly linked list or a doubly linked list, in a circular linked list, the last node points back to the first node, forming a circle.

Types of Circular Linked Lists

  1. Circular Singly Linked List: Each node has a single pointer pointing to the next node in the sequence.
  2. Circular Doubly Linked List: Each node has two pointers: one pointing to the next node and another pointing to the previous node.

Structure of a Circular Linked List Node

A typical node in a circular linked list consists of:

  • Data: The value or information stored in the node.
  • Next Pointer: A reference to the next node in the sequence. In a circular singly linked list, this pointer points back to the head node when it reaches the end.

For a circular doubly linked list, each node would also have a Previous Pointer pointing to the previous node.

Advantages of Circular Linked Lists

  • Efficient Traversal: Since there are no null pointers at the end, traversing the list is straightforward and can continue indefinitely until explicitly stopped.
  • Circular Nature: Useful in scenarios where data needs to be accessed cyclically, such as task scheduling or round-robin algorithms.

Disadvantages of Circular Linked Lists

  • Complexity in Insertion/Deletion: Operations like insertion and deletion require additional checks to ensure the circular nature is maintained.
  • Memory Overhead: Each node requires extra memory for storing pointers, similar to other linked list types.

Implementation of a Circular Singly Linked List

Below is a basic implementation of a circular singly linked list in Python:

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class CircularLinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            self.head.next = self.head  # Point to itself in a circular manner
        else:
            temp = self.head
            while temp.next != self.head:
                temp = temp.next
            temp.next = new_node
            new_node.next = self.head

    def display(self):
        if not self.head:
            print("List is empty")
            return
        temp = self.head
        while True:
            print(temp.data, end=" -> ")
            temp = temp.next
            if temp == self.head:
                break
        print()

# Example usage
cll = CircularLinkedList()
cll.append(10)
cll.append(20)
cll.append(30)
cll.display()  # Output: 10 -> 20 -> 30 ->

Explanation

  • Node Class: Represents a node with data and next.
  • CircularLinkedList Class:
    • append(data): Adds a new node to the end of the list. If the list is empty, it initializes the head node and points it back to itself.
    • display(): Prints all nodes in the list until it loops back to the head.

Implementation of a Circular Doubly Linked List

Below is an implementation of a circular doubly linked list in Python:

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
        self.prev = None

class CircularDoublyLinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            self.head.next = self.head
            self.head.prev = self.head
        else:
            temp = self.head
            while temp.next != self.head:
                temp = temp.next
            temp.next = new_node
            new_node.prev = temp
            new_node.next = self.head
            self.head.prev = new_node

    def display_forward(self):
        if not self.head:
            print("List is empty")
            return
        temp = self.head
        while True:
            print(temp.data, end=" <-> ")
            temp = temp.next
            if temp == self.head:
                break
        print()

    def display_backward(self):
        if not self.head:
            print("List is empty")
            return
        temp = self.head.prev
        while True:
            print(temp.data, end=" <-> ")
            temp = temp.prev
            if temp == self.head.prev:
                break
        print()

# Example usage
cdll = CircularDoublyLinkedList()
cdll.append(10)
cdll.append(20)
cdll.append(30)
cdll.display_forward()  # Output: 10 <-> 20 <-> 30 <->
cdll.display_backward() # Output: 30 <-> 20 <-> 10 <->

Explanation

  • Node Class: Represents a node with data, next, and prev.
  • CircularDoublyLinkedList Class:
    • append(data): Adds a new node to the end of the list. If the list is empty, it initializes the head node and sets both next and prev pointers to itself.
    • display_forward(): Prints all nodes in forward direction until it loops back to the head.
    • display_backward(): Prints all nodes in backward direction starting from the last node.

Best Practices

  1. Handle Edge Cases: Always check if the list is empty before performing operations like traversal or deletion.
  2. Maintain Circular Nature: Ensure that after every insertion or deletion, the circular nature of the list is maintained.
  3. Memory Management: Be cautious about memory leaks and ensure that all nodes are properly deallocated when they are no longer needed.

Conclusion

Circular linked lists provide a unique way to handle data where cyclic traversal is required. They offer efficient traversal but come with additional complexity in terms of insertion, deletion, and maintaining the circular nature. Understanding and implementing circular linked lists can be beneficial for solving specific problems in computer science and software engineering.

By mastering circular linked lists, you enhance your ability to design and implement complex data structures that meet specific requirements efficiently.


PreviousDoubly Linked ListsNext Stacks

Recommended Gear

Doubly Linked ListsStacks