Random Singly Linked List Generator using Python
Last Updated :
25 Jan, 2023
In this article, we will learn how to generate a singly linked list using random numbers.
A Singly Linked List is a linear data structure. A linked list is a collection of nodes and in a singly linked list every node contains two things:
- Data
- Reference to the next node
In this article, we will learn to generate a Singly Linked List without asking for data from the user.
Generating a Singly Linked List of random length:
Python3
class Node:
def __init__( self ,data):
self .data = data
self . next = None
|
- Define Singly Linked List class: In this class, we defined two methods, one is to insert an element at the end of the Linked List and the other method is to print Linked List.
tail variable is to store last node of Singly Linked List, this help us to insert element without traversing
whole linked list again and again.
By using this variable complexity of insertion is reduced to O(1) form O(n).
Python3
class Singly_Linked_List:
def __init__( self ):
self .head = None
self .tail = None
def insert_at_last( self , data):
new_node = Node(data)
if self .head = = None :
self .head = new_node
self .tail = new_node
else :
self .tail. next = new_node
self .tail = new_node
def print_Singly_Linked_List( self ):
t_head = self .head
print ( "\n Our Singly_Linked_List is : " , end = "")
while (t_head):
print (t_head.data, end = " -> " )
t_head = t_head. next
print ( "Null \n" )
|
- Define lambda function to generate and return a random number. Start and end are variables that define the range of numbers that will be generated by the lambda function.
Python3
start = 0
end = 20
random_int = lambda : random.randint(start,end)
|
Python3
def main():
linked_list = Singly_Linked_List()
for i in range (random_int()):
linked_list.insert_at_last(i)
linked_list.print_Singly_Linked_List()
if __name__ = = "__main__" :
main()
|
Complete Code:
This code generates a Singly Linked List of random size (size is between 0 and 20) and the data of each node is its index.
Python3
import random
class Node:
def __init__( self ,data):
self .data = data
self . next = None
class Singly_Linked_List:
def __init__( self ):
self .head = None
self .tail = None
def insert_at_last( self ,data):
new_node = Node(data)
if self .head = = None :
self .head = new_node
self .tail = new_node
else :
self .tail. next = new_node
self .tail = new_node
def print_Singly_Linked_List( self ):
t_head = self .head
print ( "\n Our Singly_Linked_List is : " ,end = "")
while (t_head):
print (t_head.data,end = " -> " )
t_head = t_head. next
print ( "Null \n" )
start = 0
end = 20
random_int = lambda : random.randint(start,end)
def main():
linked_list = Singly_Linked_List()
for i in range (random_int()):
linked_list.insert_at_last(i)
linked_list.print_Singly_Linked_List()
if __name__ = = "__main__" :
main()
|
Output:
Generating a Singly Linked List of random length where data in every node is a random number:
- For doing this we need to define a new lambda function to generate a random number and use it in the main function while inserting a node in the Singly Linked List.
Python3
random_data = lambda : random.randint( 0 , 100000 )
def main():
linked_list = Singly_Linked_List()
for i in range (random_int()):
linked_list.insert_at_last(random_data())
linked_list.print_Singly_Linked_List()
|
Code:
Python3
import random
class Node:
def __init__( self ,data):
self .data = data
self . next = None
class Singly_Linked_List:
def __init__( self ):
self .head = None
self .tail = None
def insert_at_last( self ,data):
new_node = Node(data)
if self .head = = None :
self .head = new_node
self .tail = new_node
else :
self .tail. next = new_node
self .tail = new_node
def print_Singly_Linked_List( self ):
t_head = self .head
print ( "\n Our Singly_Linked_List is : " ,end = "")
while (t_head):
print (t_head.data,end = " -> " )
t_head = t_head. next
print ( "Null \n" )
start = 0
end = 20
random_int = lambda : random.randint(start,end)
random_data = lambda : random.randint( 0 , 100000 )
def main():
linked_list = Singly_Linked_List()
for i in range (random_int()):
linked_list.insert_at_last(random_data())
linked_list.print_Singly_Linked_List()
if __name__ = = "__main__" :
main()
|
Output:
Share your thoughts in the comments
Please Login to comment...