Building a Linked List in Python: From User Input to Display

 Building a Linked List in Python: From User Input to Display

Introduction:

Linked lists are fundamental data structures used to store and manage collections of elements. They offer flexibility, efficient insertion and deletion operations, and are an essential concept in computer science. In this tutorial, we will delve into creating a linked list in Python, taking user input to populate the list, and displaying the final result. This knowledge forms a cornerstone for understanding more complex data structures and algorithms.



Node and Linked List Classes: The Foundation

Our journey begins with defining two classes: Node and LinkedList. The Node class represents an element within the linked list, encapsulating both data and a reference to the next node. The LinkedList class acts as the container for the nodes and manages various operations.

class Node:

    def __init__(self, data):

        self.data = data

        self.next = None


class LinkedList:

    def __init__(self):

        self.head = None



Appending Elements to the Linked List
The append method within the LinkedList class allows us to add elements to the list. We create a new node with the provided data and attach it to the end of the list. If the list is empty, the new node becomes the head.

    def append(self, data):
        new_node = Node(data)
        if self.head is None:
            self.head = new_node
            return
        current = self.head
        while current.next:
            current = current.next
        current.next = new_node

Displaying the Linked List
Our display method showcases the list's contents by iterating through each node and printing its data. The linked list ends with "None," indicating the termination of the list.

    def display(self):
        current = self.head
        while current:
            print(current.data, end=" -> ")
            current = current.next
        print("None")


Taking User Input for the Linked List
To demonstrate the practical use of our linked list, we take user input to populate it. By asking the user for the number of elements they wish to add, followed by the actual elements, we build the linked list dynamically.

# Create an instance of LinkedList
linked_list = LinkedList()

# Take input from the user to add elements to the linked list
n = int(input("Enter the number of elements: "))
for _ in range(n):
    data = int(input("Enter element: "))
    linked_list.append(data)


Displaying the Result
Finally, we display the linked list in its entirety, showcasing the user's provided elements.

# Display the linked list
print("Linked List:")
linked_list.display()


Whole Code

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None
    
    def append(self, data):
        new_node = Node(data)
        if self.head is None:
            self.head = new_node
            return
        current = self.head
        while current.next:
            current = current.next
        current.next = new_node
    
    def display(self):
        current = self.head
        while current:
            print(current.data, end=" -> ")
            current = current.next
        print("None")

# Create an instance of LinkedList
linked_list = LinkedList()

# Take input from the user to add elements to the linked list
n = int(input("Enter the number of elements: "))
for _ in range(n):
    data = int(input("Enter element: "))
    linked_list.append(data)

# Display the linked list
print("Linked List:")
linked_list.display()


Post a Comment

0 Comments