Program to Create a Linked List & Display the Elements in the List in Python

Linked List Data Structure:

A linked list is a type of data structure that consists of a chain of nodes, each of which contains a value and a pointer to the next node in the chain.

The list’s head pointer points to the first node, and the list’s last element points to null. The head pointer points to null when the list is empty.

Linked lists can grow in size dynamically, and inserting and deleting elements from them is simple because, unlike arrays, we only need to change the pointers to the previous and next elements to insert or delete an element.

Linked lists are commonly used in the construction of file systems, adjacency lists, and hash tables.

Python Program to Create a Linked List & Display the Elements in the List

Approach:

  • Create a class that creates a node of the LinkedList.
  • Create a constructor that accepts val as an argument and initializes the class variable val with the given argument val.
  • Initializing the next Pointer(Last Node Pointer to None i.e Null).
  • Create a Class Which Creates the LinkedList by connecting all the nodes.
  • Inside the class, the constructor Initialize the head pointer to None(Null) and the last Node pointer to Null.
  • Create a function addElements() inside the class which accepts the data value as an argument and add this node to the LinkedList.
  • Check if the lastNode Pointer is None using the if conditional statement.
  • If the above if condition is true(If there are no elements in the linked list) then Create a Node using the Node class by passing the data as an argument.
  • Initialize the head with the above node.
  • Else Create the new node and initialize the last node pointer Value to the new node.
  • Set the last node Pointer Value to the Next Pointer.
  • Create a function DisplayElements inside the class which prints all the elements of the LinkedList.
  • Take a Pointer that points to the nodes of the LinkedList and initialize its value to the Head Pointer.
  • Loop till the temPtr is None(null) using the while loop.
  • Print the value at the Pointer Node.
  • Increment the temPtr to the Next Node.
  • Take an object for the LinkedList Class and store it in a variable.
  • Add elements to the LinkedList by using the below steps.
  • Loop till the above number of elements using the For loop.
  • Give the data value as user input and store it in a variable.
  • Pass the above value as an argument to the addElements() function to add this value as a node to the LinkedList.
  • Printing all the elements/Node values of the LinkedList by calling the displayElements() of the above object.
  • The Exit of the Program.

Below is the Implementation:

#Create a class that creates a node of the LinkedList.
class Node:
    #Create a constructor that accepts val as an argument and initializes the class variable val with the given argument val.
    def __init__(self, val):
       self.val = val
       #Initializing the next Pointer(Last Node Pointer to None i.e Null).
       self.nextPtr = None
 #Create a Class Which Creates the LinkedList by connecting all the nodes.
class LinkedList:
  #Inside the class, the constructor Initialize the head pointer to None(Null) and the last Node pointer to Null.
    def __init__(self):
        self.headPtr = None
        self.lastNode = None
    #Create a function addElements() inside the class which accepts the data value as an argument and add this node to the LinkedList.
    def addElements(self, val):
        #Check if the lastNode Pointer is None using the if conditional statement.
        if self.lastNode is None:
          #If the above if condition is true(If there are no elements in the linked list)
          # then Create a Node using the Node class by passing the data as an argument.
          #Initialize the head with the above node.
            self.headPtr = Node(val)
            self.lastNode = self.headPtr
        else:
          #Else Create the new node and initialize the last node pointer Value to the new node.
            self.lastNode.nextPtr = Node(val)
            #Set the last node Pointer Value to the Next Pointer.
            self.lastNode = self.lastNode.nextPtr
    #Create a function DisplayElements inside the class which prints all the elements of the LinkedList.
    def displayElements(self):
      #Take a Pointer that points to the nodes of the LinkedList and initialize its value to the Head Pointer.
        temPtr = self.headPtr
        #Loop till the temPtr is None(null) using the while loop.
        while temPtr is not None:
          #Print the value at the Pointer Node
            print(temPtr.val, end = ' ')
            #Increment the temPtr to the Next Node.
            temPtr = temPtr.nextPtr
#Take an object for the LinkedList Class and store it in a variable.
lkdList = LinkedList()
#Add elements to the LinkedList by using the below steps.
n = int(input('Enter the number of elements you wish to add in the linked list = '))
#Loop till the above number of elements using the For loop.
for i in range(n):
    #Give the data value as user input and store it in a variable.
    val = int(input('Enter data item: '))
    #Pass the above value as an argument to the addElements() function to add this value as a node to the LinkedList.
    lkdList.addElements(val)
#Printing all the elements/Node values of the LinkedList by calling the displayElements() of the above object.
print('The Elements of the Linked List are: ')
lkdList.displayElements()

Output:

Enter the number of elements you wish to add in the linked list = 4
Enter data item: 3
Enter data item: 8
Enter data item: 2
Enter data item: 4
The Elements of the Linked List are: 
3 8 2 4