When it is required to create a circular linked list and display it, a 'Node' class needs to be created. In this class, there are two attributes, the data that is present in the node, and the access to the next node of the linked list. In a circular linked list, the head and the rear are adjacent to each other. They are connected to form a circle, and don't have 'NULL' value in the last node.
Another 'linked_list' class needs to be created that would have an initialization function, and the head of the node would be initialized to 'None'.
Below is a demonstration for the same −
class Node: def __init__(self, my_data): self.data = my_data self.next = None class linked_list: def __init__(self): self.head = None def add_data(self,my_data): new_node = Node(my_data) new_node.next = self.head self.head = new_node def print_it(self): temp = self.head while(temp): print(temp.data) temp = temp.next my_list = linked_list() my_list.add_data(47) my_list.add_data(89) my_list.add_data(34) my_list.add_data(11) print("The circular linked list is : ") my_list.print_it()
The circular linked list is : 11 34 89 47