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:
// Class Node, similar to the linked list
class Node{
int value;
// Points to the next node.
Node next;
}
struct Node {
int data;
struct Node *next;
};
public class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public class Node
{
public int data;
public Node next;
public Node(int data)
{
this.data = data;
this.next = null;
}
}
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class Node {
public $data;
public $next;
function __construct($data) {
$this->data = $data;
$this->next = null;
}
}
# 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:
The above Circular singly linked list can be represented as:
// 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;
Node* one = createNode(3);
Node* two = createNode(5);
Node* three = createNode(9);
// Connect nodes
one->next = two;
two->next = three;
three->next = one;
// 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;
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;
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;
$one = new Node(3);
$two = new Node(5);
$three = new Node(9);
// Connect nodes
$one->next = $two;
$two->next = $three;
$three->next = $one;
# 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.
Contact Us