How to Create and Display Linked List in Python

In this tutorial, we will learn how to program "How to Create and Display a Linked List in Python." The objective is to create and display a linked list. This tutorial will guide you step by step through the process of managing a linked 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 creating and displaying a linked 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.
  1. class Node:
  2.     def __init__(self, data):
  3.         self.data = data
  4.         self.next = None
  5.  
  6. class LinkedList:
  7.     def __init__(self):
  8.         self.head = None
  9.         self.last_node = None
  10.  
  11.     def append(self, data):
  12.         if self.last_node is None:
  13.             self.head = Node(data)
  14.             self.last_node = self.head
  15.         else:
  16.             self.last_node.next = Node(data)
  17.             self.last_node = self.last_node.next
  18.  
  19.     def display(self):
  20.         current = self.head
  21.         while current is not None:
  22.             print(current.data, end=' ')
  23.             current = current.next
  24.         print()  # for newline
  25.  
  26. while True:
  27.     print("\n============= Create and Display Linked List =============\n")
  28.  
  29.     a_llist = LinkedList()
  30.     n = int(input('How many elements would you like to add? '))
  31.     for i in range(n):
  32.         data = int(input('Enter data item: '))
  33.         a_llist.append(data)
  34.  
  35.     print('The linked list: ', end='')
  36.     a_llist.display()
  37.  
  38.     # Ask user if they want to try again
  39.     opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
  40.     if opt == 'no':
  41.         print("Exiting program...")
  42.         break
  43.     elif opt != 'yes':
  44.         print("Invalid choice. Exiting program...")
  45.         break

This program creates and displays a singly linked list. Users can append multiple integer elements to the list, and the program prints all the elements in order. After displaying, the user can choose to create another list or exit.

Output:

There you have it we successfully created How to Create and Display Linked 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

Python Tutorials