Representation of circular linked list

Circular linked lists are similar to single Linked Lists with the exception of connecting the last node to the first node.

Node representation of a Circular Linked List:

C++
// Class Node, similar to the linked list
class Node{
    int value;

// Points to the next node.
    Node next;
}
C
struct Node {
    int data;
    struct Node *next;
};
Java
public class Node {
    int data;
    Node next;
    
    public Node(int data) {
        this.data = data;
        this.next = null;
    }
}
C#
public class Node
{
    public int data;
    public Node next;
    
    public Node(int data)
    {
        this.data = data;
        this.next = null;
    }
}
Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}
PHP
class Node {
  public $data;
  public $next;

  function __construct($data) {
    $this->data = $data;
    $this->next = null;
  }
}
Python3
# Class Node, similar to the linked list
class Node:
    def __init__(self,data):
        self.data = data
        self.next = None

Example of Circular singly linked list:

Example of  circular linked list

The above  Circular singly linked list can be represented as:

C++
// Initialize the Nodes.
Node one = new Node(3);
Node two = new Node(5);
Node three = new Node(9);

// Connect nodes
one.next = two;
two.next = three;
three.next = one;
C
Node* one = createNode(3);
Node* two = createNode(5);
Node* three = createNode(9);

// Connect nodes
one->next = two;
two->next = three;
three->next = one;
Java
// Define the Node class
class Node {
    int value;
    Node next;

    public Node(int value) {
        this.value = value;
    }
}

// Initialize the Nodes.
Node one = new Node(3);
Node two = new Node(5);
Node three = new Node(9);

// Connect nodes
one.next = two;
two.next = three;
three.next = one;
C#
Node one = new Node(3);
Node two = new Node(5);
Node three = new Node(9);

// Connect nodes
one.next = two;
two.next = three;
three.next = one;
Javascript
let one = new Node(3);
let two = new Node(5);
let three = new Node(9);

// Connect nodes
one.next = two;
two.next = three;
three.next = one;
PHP
$one = new Node(3);
$two = new Node(5);
$three = new Node(9);

// Connect nodes
$one->next = $two;
$two->next = $three;
$three->next = $one;
Python3
# Initialize the Nodes.
one = Node(3)
two = Node(5)
three = Node(9)

# Connect nodes
one.next = two
two.next = three
three.next = one

Explanation: In the above program one, two, and three are the node with values 3, 5, and 9 respectively which are connected in a circular manner as:

  • For Node One: The Next pointer stores the address of Node two.
  • For Node Two: The Next stores the address of Node three
  • For Node Three: The Next points to node one.

Introduction to Circular Linked List

Similar Reads

What is Circular linked list?

The circular linked list is a linked list where all nodes are connected to form a circle. In a circular linked list, the first node and the last node are connected to each other which forms a circle. There is no NULL at the end....

Representation of circular linked list:

Circular linked lists are similar to single Linked Lists with the exception of connecting the last node to the first node....

Operations on the circular linked list:

We can do some operations on the circular linked list similar to the singly linked list which are:...

Advantages of Circular Linked Lists:

Any node can be a starting point. We can traverse the whole list by starting from any point. We just need to stop when the first visited node is visited again. Useful for implementation of a queue. Unlike this implementation, we don’t need to maintain two pointers for front and rear if we use a circular linked list. We can maintain a pointer to the last inserted node and the front can always be obtained as next of last....

Disadvantages of circular linked list:

Compared to singly linked lists, circular lists are more complex.Reversing a circular list is more complicated than singly or doubly reversing a circular list.It is possible for the code to go into an infinite loop if it is not handled carefully.It is harder to find the end of the list and control the loop.Although circular linked lists can be efficient in certain applications, their performance can be slower than other data structures in certain cases, such as when the list needs to be sorted or searched.Circular linked lists don’t provide direct access to individual nodes...

Applications of circular linked lists:

Multiplayer games use this to give each player a chance to play.A circular linked list can be used to organize multiple running applications on an operating system. These applications are iterated over by the OS.Circular linked lists can be used in resource allocation problems.Circular linked lists are commonly used to implement circular buffers,Circular linked lists can be used in simulation and gaming....

Why circular linked list?

A node always points to another node, so NULL assignment is not necessary.Any node can be set as the starting point.Nodes are traversed quickly from the first to the last....

Contact Us