Python program to create and display a Circular Linked List

python tutorials and learn python

Created with Sketch.

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.

Python program to create and display a Circular Linked List

ALGORITHM:

  1. 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.
  2. Define another class for creating the circular linked list, and it has two nodes: head and tail. It has two methods: add() and display() .
  3. 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:

  1. #Represents the node of list.  
  2. class Node:
  3.   def __init__(self,data):
  4.     self.data = data;
  5.     self.next = None;
  6. class CreateList:
  7.   #Declaring head and tail pointer as null.  
  8.   def __init__(self):
  9.     self.head = Node(None);
  10.     self.tail = Node(None);
  11.     self.head.next = self.tail;
  12.     self.tail.next = self.head;
  13.   #This function will add the new node at the end of the list.  
  14.   def add(self,data):
  15.     newNode = Node(data);
  16.     #Checks if the list is empty.  
  17.     if self.head.data is None:
  18.       #If list is empty, both head and tail would point to new node.  
  19.       self.head = newNode;
  20.       self.tail = newNode;
  21.       newNode.next = self.head;
  22.     else:
  23.       #tail will point to new node.  
  24.       self.tail.next = newNode;
  25.       #New node will become new tail.  
  26.       self.tail = newNode;
  27.       #Since, it is circular linked list tail will point to head.  
  28.       self.tail.next = self.head;
  29.   #Displays all the nodes in the list  
  30.   def display(self):
  31.     current = self.head;
  32.     if self.head is None:
  33.       print(“List is empty”);
  34.       return;
  35.     else:
  36.         print(“Nodes of the circular linked list: “);
  37.         #Prints each node by incrementing pointer.  
  38.         print(current.data),
  39.         while(current.next != self.head):
  40.             current = current.next;
  41.             print(current.data),
  42. class CircularLinkedList:
  43.   cl = CreateList();
  44.   #Adds data to the list  
  45.   cl.add(1);
  46.   cl.add(2);
  47.   cl.add(3);
  48.   cl.add(4);
  49.   #Displays all the nodes present in the list  
  50.   cl.display();

Output:

Nodes of the circular linked list: 
1 2 3 4

Leave a Reply

Your email address will not be published. Required fields are marked *