
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to sort the elements of the Circular Linked List
When it is required to sort the elements of a circular linked list, 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’.
Multiple methods are defined by the user to add node to the linked list, sort the linked list in ascending or descending order and to print the node values.
Below is a demonstration for the same −
Example
class Node: def __init__(self,data): self.data = data self.next = None class list_creation: def __init__(self): self.head = Node(None) self.tail = Node(None) self.head.next = self.tail self.tail.next = self.head def add_data(self,my_data): new_node = Node(my_data) if self.head.data is None: self.head = new_node self.tail = new_node new_node.next = self.head else: self.tail.next = new_node self.tail = new_node self.tail.next = self.head def sort_list(self): curr = self.head if(self.head == None): print("The list is empty") else: while(True): index_val = curr.next while(index_val != self.head): if(curr.data > index_val.data): temp = curr.data curr.data = index_val.data index_val.data = temp index_val = index_val.next curr =curr.next if(curr.next == self.head): break; def print_it(self): curr = self.head if self.head is None: print("The list is empty"); return; else: print(curr.data) while(curr.next != self.head): curr = curr.next print(curr.data) print("\n") class circular_linked_list: my_cl = list_creation() print("Nodes are being added to the list") my_cl.add_data(21) my_cl.add_data(54) my_cl.add_data(78) my_cl.add_data(99) my_cl.add_data(27) print("The list is :") my_cl.print_it() print("The list is being sorted") my_cl.sort_list() print("The sorted list is : ") my_cl.print_it()
Output
Nodes are being added to the list The list is : 21 54 78 99 27 The list is being sorted The sorted list is : 21 27 54 78 99
Explanation
- The ‘Node’ class is created.
- Another class with required attributes is created.
- Another method named ‘sort_list’ is defined, that is used to sort the elements in the circular linked list in an ascending or descending order.
- Another method named ‘print_it’ is defined, that displays the nodes of the circular linked list.
- An object of the ‘list_creation’ class is created, and the methods are called on it to add data.
- An ‘init’ method is defined, that the first and last nodes of the circular linked list to None.
- The ‘sort_list’ method is called.
- It iterates through the list, and places the elements in their relevant position based on the value.
- This is displayed on the console using the ‘print_it’ method.
- Related Articles
- Python Circular Linked List Program
- Python program to remove duplicate elements from a Circular Linked List
- Python program to delete a node from the end of the Circular Linked List
- Python program to delete a node from the middle of the Circular Linked List
- Python Program to Create a Linked List & Display the Elements in the List
- Python program to insert a new node at the beginning of the Circular Linked List
- Python program to insert a new node at the end of the Circular Linked List
- Python program to insert a new node at the middle of the Circular Linked List
- Python program to create and display a Circular Linked List
- Golang program to traverse a circular linked list and print its elements
- Python program to sort a List according to the Length of the Elements?
- Python Program to Convert a given Singly Linked List to a Circular List
- Python Program To Add Elements To A Linked List
- Sort a list according to the Length of the Elements in Python program
- Python program to search an element in a Circular Linked List

Advertisements