In this tutorial, we are going to learn the python program to create a singly linked list and traverse its elements.
Problem Statement
Our program will take the elements of the linked list from the user as the input and then print the elements of the linked list.
For example
Case1: If the user inputs the elements of the linked list as 1, 2, 3, 4.
The output should be 1234.
Case2: If the user inputs the elements of the linked list as 7, 9, 1, 0.
The output should be 7910.
What is a Linked List?
A linked list is the collection of elements that are connected via the memory address of successor elements. It is an unordered collection of data that is connected via links. Each data elements have another block that contains the address of the next elements of the linked list.
Creation Of Linked List:
The singly linked list is a linear records shape wherein every element of the listing incorporates a pointer which factors to the following detail inside the listing. Each element within the singly connected list is referred to as a node. Each node has two components: data and next pointer, which factors the following node within the listing. The first node of the list is the head, and the closing node of the list is called a tail. The last node of the list contains a pointer to the null. Each node in the list can be linearly accessed with the help of traversing through the list from head to tail. The linked list is created using the node class. We create a node object and another class to use this node object.
Traversal of a linked list
Traversing is the most common operation of the linked list, it means visiting every node of the linked list once and performing any operation usually, we insert, delete or display the elements of the linked list. For traversing basic idea is to visit every node from head to the null value.
Our Logic to create and traverse singly linked list
- Our program will take a list of elements as input from the user.
- Store the elements inputs by the user in linked list format(Linked list creation)
- Traverse through each element of the linked list and print the elements as the output (Traverse of linked list)
Let’s code the above algorithm in the python programming language.
Python Program to create and traverse singly linked list
#Creating node class of the singly linked list
class Node:
def __init__(self, data):
self.data = data
self.next = None
class SinglyLinkedList:
def __init__(self):
self.head = None
self.tail = None
#Creating addNode() to add newly created nodes.
def addNode(self, data):
if self.tail is None:
self.head = Node(data)
self.tail = self.head
else:
self.tail.next = Node(data)
self.tail = self.tail.next
#Creating display() to print newly created nodes via traversing.
def display(self):
current = self.head
while current is not None:
print(current.data, end = ' ')
current = current.next
s = SinglyLinkedList()
n = int(input('enter the number of elements in linked list : '))
for i in range(n):
data = int(input('Enter data: '))
s.addNode(data)
print('The linked list: ', end = '')
s.display()
Output
enter the number of elements in linked list : 4
Enter data: 1
Enter data: 2
Enter data: 3
Enter data: 4
The linked list: 1 2 3 4
Explanations
For the input size 4, the data elements of the linked list are input as 1, 2, 3, and 4. The elements are connected using the pointer which is the pointer to the next node.
1–>2–>3–>4–>Null