How to Find the Largest Element in a Doubly Linked List in Python

In this tutorial, we will learn how to program “How to Find the Largest Element in a Doubly Linked List in Python.” The objective is to find the largest element in a doubly linked list. This tutorial will guide you step by step through the process of identifying the largest element. 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 finding the largest element in a doubly 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.         self.prev = None
  6.  
  7.  
  8. class DoublyLinkedList:
  9.     def __init__(self):
  10.         self.first = None
  11.         self.last = None
  12.  
  13.     def append(self, data):
  14.         self.insert_at_end(Node(data))
  15.  
  16.     def insert_at_end(self, new_node):
  17.         if self.last is None:
  18.             self.first = new_node
  19.             self.last = new_node
  20.         else:
  21.             new_node.prev = self.last
  22.             self.last.next = new_node
  23.             self.last = new_node
  24.  
  25.  
  26. def find_largest(dllist):
  27.     if dllist.first is None:
  28.         return None
  29.  
  30.     largest = dllist.first.data
  31.     current = dllist.first.next
  32.  
  33.     while current:
  34.         if current.data > largest:
  35.             largest = current.data
  36.         current = current.next
  37.  
  38.     return largest
  39.  
  40.  
  41. # ---------- MAIN PROGRAM ----------
  42.  
  43. while True:
  44.     print("\n=============== Find the Largest Element in a Doubly Linked List ===============\n")
  45.  
  46.     a_dllist = DoublyLinkedList()
  47.  
  48.     data_list = input(
  49.         "Please enter the elements in the doubly linked list: ").split()
  50.  
  51.     for data in data_list:
  52.         a_dllist.append(int(data))
  53.  
  54.     largest = find_largest(a_dllist)
  55.  
  56.     if largest is not None:
  57.         print(f"The largest element is {largest}.")
  58.     else:
  59.         print("The list is empty.")
  60.  
  61.     opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
  62.     if opt == "no":
  63.         print("Exiting program...")
  64.         break
  65.     elif opt != "yes":
  66.         print("Invalid choice. Exiting program...")
  67.         break

This program illustrates how to find the largest element in a doubly linked list using Python. It defines a `Node` class containing data along with pointers to both the next and previous nodes, and a `DoublyLinkedList` class that manages node insertion at the end of the list. The user enters a sequence of numbers, which are stored as nodes in the doubly linked list. The `find_largest` function then traverses the list from the first node to the last, comparing each element to determine the maximum value present. If the list is empty, an appropriate message is displayed. The program is interactive and allows users to repeat the process or exit, making it a clear and practical demonstration of traversing and processing data in a doubly linked list.

Output:

There you have it we successfully created How to Find the Largest Element in a Doubly 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