How to Convert a Singly Linked List to a Circular List in Python
In this tutorial, we will learn how to program “How to Convert a Singly Linked List to a Circular List in Python.” The objective is to convert a singly linked list into a circular list. This tutorial will guide you step by step through the process of converting a singly linked list into a circular list. By the end of this tutorial, you will have a solid understanding of how to implement this task effectively in Python, helping you strengthen your problem-solving abilities and improve your coding skills.
This topic is straightforward and easy to understand. Simply follow the instructions provided, and you will complete it with ease. The program will guide you step by step through the process of converting a singly linked list into a circular list. So, let’s dive into the coding process!
Getting Started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Creating Main Function
This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.- class Node:
- def __init__(self, data):
- self.data = data
- self.next = None
- class LinkedList:
- def __init__(self):
- self.head = None
- self.last_node = None
- def append(self, data):
- if self.last_node is None:
- self.head = Node(data)
- self.last_node = self.head
- else:
- self.last_node.next = Node(data)
- self.last_node = self.last_node.next
- def convert_to_circular(llist):
- if llist.last_node:
- llist.last_node.next = llist.head
- def print_last_node_points_to(llist):
- last = llist.last_node
- if last is None:
- print("List is empty.")
- elif last.next is None:
- print("Last node points to None.")
- else:
- print("Last node points to element with data {}."
- .format(last.next.data))
- # ---------------- MAIN PROGRAM ----------------
- while True:
- print("\n=============== Convert a Singly Linked List to a Circular List ===============\n")
- a_llist = LinkedList()
- data_list = input(
- "Please enter the elements in the linked list: ").split()
- for data in data_list:
- a_llist.append(int(data))
- print_last_node_points_to(a_llist)
- print("Converting linked list to a circular linked list...")
- convert_to_circular(a_llist)
- print_last_node_points_to(a_llist)
- opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
- if opt == "no":
- print("Exiting program...")
- break
- elif opt != "yes":
- print("Invalid choice. Exiting program...")
- break
This Python program demonstrates how to convert a singly linked list into a circular linked list. It begins by creating a standard singly linked list using `Node` and `LinkedList` classes, where user-entered elements are appended sequentially. Initially, the program checks and displays what the last node is pointing to, which is `None` in a normal singly linked list. The `convert_to_circular` function then modifies the structure by setting the `next` reference of the last node to point back to the head node, effectively making the list circular. After conversion, the program confirms the change by showing that the last node now points to the first element. The entire process runs inside a loop, allowing users to repeat the operation with different inputs until they choose to exit.
Output:
There you have it we successfully created How to Convert a Singly Linked List to a Circular List in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
More Tutorials for Python Language