Python program to create and display a Circular Linked List
In this program, we will create a circular linked list and print all the nodes present in the list.
Circular Linked List:
The circular linked list is a kind of linked list. First thing first, the node is an element of the list, and it has two parts that are, data and next. Data represents the data stored in the node, and next is the pointer that will point to the next node. Head will point to the first element of the list, and tail will point to the last element in the list. In the simple linked list, all the nodes will point to their next element and tail will point to null.
The circular linked list is the collection of nodes in which tail node also point back to head node. The diagram shown below depicts a circular linked list. Node A represents head and node D represents tail. So, in this list, A is pointing to B, B is pointing to C and C is pointing to D but what makes it circular is that node D is pointing back to node A.
ALGORITHM:
- Define a Node class which represents a node in the list. It has two properties data and next which will point to the next node.
- Define another class for creating the circular linked list, and it has two nodes: head and tail. It has two methods: add() and display() .
- add() will add the node to the list:
- It first checks whether the head is null, then it will insert the node as the head.
- Both head and tail will point to the newly added node.
- If the head is not null, the new node will be the new tail, and the new tail will point to the head as it is a circular linked list.
a. display() will show all the nodes present in the list.
- Define a new node ‘current’ that will point to the head.
- Print current.data till current will points to head
- Current will point to the next node in the list in each iteration.
PROGRAM:
- #Represents the node of list.
- class Node:
- def __init__(self,data):
- self.data = data;
- self.next = None;
- class CreateList:
- #Declaring head and tail pointer as null.
- def __init__(self):
- self.head = Node(None);
- self.tail = Node(None);
- self.head.next = self.tail;
- self.tail.next = self.head;
- #This function will add the new node at the end of the list.
- def add(self,data):
- newNode = Node(data);
- #Checks if the list is empty.
- if self.head.data is None:
- #If list is empty, both head and tail would point to new node.
- self.head = newNode;
- self.tail = newNode;
- newNode.next = self.head;
- else:
- #tail will point to new node.
- self.tail.next = newNode;
- #New node will become new tail.
- self.tail = newNode;
- #Since, it is circular linked list tail will point to head.
- self.tail.next = self.head;
- #Displays all the nodes in the list
- def display(self):
- current = self.head;
- if self.head is None:
- print(“List is empty”);
- return;
- else:
- print(“Nodes of the circular linked list: “);
- #Prints each node by incrementing pointer.
- print(current.data),
- while(current.next != self.head):
- current = current.next;
- print(current.data),
- class CircularLinkedList:
- cl = CreateList();
- #Adds data to the list
- cl.add(1);
- cl.add(2);
- cl.add(3);
- cl.add(4);
- #Displays all the nodes present in the list
- cl.display();
Output:
Nodes of the circular linked list: 1 2 3 4