As we know that a Simple linked list is a sequential collection of Nodes, and each node has a data value and a pointer variable which hold the address of the next node. A Circular linked list is similar to Simple linked-list the only difference is, here the pointer variable of the last node point to the head node of the linked list, Rather than Null. When the Last Node’s pointer variable point to the Head node it forms a circular structure, that’s why we call it circular linked list. The interesting thing about Circular linked-list, we can form it using the Simple and Doubly linked list, all we have to do, point the pointer of the last node to the first node instead of Null.
Operations on Circular Linked List
Like a Simple and Doubly linked list, we can perform basic operations on Circular linked list and these operations are.
- Insertion
- Deletion
- Travers
Insertion
In insertion we can insert new elements in the Circular linked list, here we have to perform some extra work when we want to insert an element at the end of a linked list. In Circular linked list the insertion operation for Insert element at the beginning, middle and end, have some different operations structure.
Deletion
In deletion, we simply manipulate the pointer, value so instead of pointing the next element it points the element next to the next pointer.
Traverse
While traversing in a Circular linked list we need to set the base condition, or else we would traverse through it infinitely.
Advantages and Disadvantages of Circular Linked List
Here are some Advantages and Disadvantages of Circular Linked List:
Advantages
- It simply a Circular structure so, any point on a circle could be a starting point, we can traverse the complete list starting from any point.
- It often comes in use when we are implementing advanced data structures.
- With a single node, we can visit any node.
Disadvantages
- If there is no base condition to terminate the circular linked list once the traverse is complete, we can be thrown to an infinite loop.
- It is not easy to reverse the linked list.
Circular linked List Implementation
class Node: def __init__(self,data): self.data = data self.next = None class Linked_List: def __init__(self): self.head = Node(None) self.tail = Node(None) self.head.next = self.tail self.tail.next = self.head def append_list(self,data): newNode = Node(data) if self.head.data is None: self.head = newNode self.tail = newNode newNode.next = self.head else: self.tail.next = newNode self.tail = newNode self.tail.next = self.head def show(self): current = self.head; if self.head is None: print("List is empty") return else: print("Nodes of the circular linked list: ") print(current.data) while(current.next != self.head): current = current.next print(current.data) class CircularLinkedList: c_l = Linked_List() c_l.append_list(1) c_l.append_list(2) c_l.append_list(3) c_l.append_list(4) c_l.show()
People are also reading: