Javatpoint Logo
Javatpoint Logo

Sorted insert for circular linked list

Circular linked lists are linked data structures in which the last node returns to the first node, forming a circular loop. This circular connectivity allows the traversal of the list in a circular manner indefinitely. Circular linked lists find use cases in several applications, such as buffer implementations, representing cyclic data, and more.

Inserting a new node in a sorted circular linked list requires locating the correct insertion point while preserving circular connectivity. This article will look at implementing sorted insert operation on circular linked lists in Python. The sorted insert algorithm involves traversing the list starting from the head node while comparing the node data to find the proper place for inserting the new node.

What is a Circular Linked List?

A circular linked list is a variation of a linked list in which the last node points back to the first node, forming a circular loop. This makes the traversal of the list possible indefinitely without ever reaching the end.

Sorted insert for circular linked list

For example:

Here, we create a CircularLinkedList object and three Node objects holding data 1, 2 and 3.

  • The first node is assigned as the head of the linked list.
  • The next pointer of the head points to the second node.
  • The next pointer of the second node points to the third node.
  • The next pointer of the third node points back to the first node, creating a circular loop.
  • This circular connectivity allows us to traverse the list endlessly. For example:

Output:

1
2
3

The main benefit of a circular linked list is that we can traverse the list continuously, which is helpful in cases like ring buffers. We also avoid exceptional circumstances for handling the end of the list.

The disadvantage is that inserting and deleting nodes requires more care to maintain the circular structure.

Python Program

Output:

1 3 5 7 9 10

Explanation

The program has two classes - Node and CircularLinkedList.

Node class is simple - it just stores data and a next pointer.

CircularLinkedList class contains:

  • init() method to initialize the head to None
  • sortedInsert() method to insert a new node in sorted order. It handles 3 cases:
    1. If the list is empty, make a new node as the head
    2. If the new node's data is smaller than the head's data, insert the new node before the head
    3. Else, traverse the list to find the insertion point before a node with higher data
  • printList() method prints the list data

The sortedInsert() method first checks if the list is empty. If yes, it makes the new node as head.

Then, it checks if the new node should become head. This will happen if the new node's data exceeds the current head's. In this case, we traverse to the end, connect the back to the new node, and the new node to the original head.

Finally, if the new node is to be inserted in between, we traverse until we find a node with higher data. Insert a new node before this node.

Main

  • Create CircularLinkedList object
  • Insert nodes in sorted order by calling sortedInsert()
  • Call printList() to display contents

This performs step-wise sorted insertion in the circular linked list by handling all cases like empty list, new head insertion, and intermediate insertion.

The traversal handles go around the circular list correctly, checking for the heads while traversing.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA