Open In App

Random Singly Linked List Generator using Python

Last Updated : 25 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to generate a singly linked list using random numbers.

Prerequisite: Insertion in Linked List

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:

  1. Data 
  2. 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:

  • Import Modules 

Python




import random


  • Define Node class

Python3




# Node class
class Node:
    # constructor to initialize the node object
    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:
    # constructor to initialize the Singly_Linked_List object
    def __init__(self):
        self.head = None
        self.tail = None
 
    def insert_at_last(self, data):
        # create temporary node
        new_node = Node(data)
 
        # if head is None, it means that Singly_Linked_List is empty and therefore tail is also None
        if self.head == None:
            # make head to new node that is inserted
            self.head = new_node
            # at this point Singly_Linked_List contains only 1 node , therefore head and tail both will point to it
            self.tail = new_node
        else:
            # Insert new node at the end of Singly_Linked_List
            self.tail.next = new_node
            # update tail of Singly_Linked_List after inserting 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)


  • Define the Main function

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
 
# Node class
class Node:
    # constructor to initialize the node object
    def __init__(self,data):
        self.data = data
        self.next = None
 
# Singly Linked List class
class Singly_Linked_List:
    # constructor to initialize the Singly_Linked_List object
    def __init__(self):
        self.head = None
        self.tail = None
 
    def insert_at_last(self,data):
        # create temporary node
        new_node = Node(data)
 
        # if head is None, it means that Singly_Linked_List is empty and therefore tail is also None
        if self.head == None:
            # make head to new node that is inserted
            self.head = new_node
            # at this point Singly_Linked_List contains only 1 node , therefore head and tail both will point to it 
            self.tail = new_node
        else:
            # Insert new node at the end of Singly_Linked_List
            self.tail.next = new_node
            # update tail of Singly_Linked_List after inserting 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 will return a number between 0 and 100000
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
 
# Node class
class Node:
    # constructor to initialize the node object
    def __init__(self,data):
        self.data = data
        self.next = None
 
# Singly Linked List class
class Singly_Linked_List:
    # constructor to initialize the Singly_Linked_List object
    def __init__(self):
        self.head = None
        self.tail = None
 
    def insert_at_last(self,data):
        # create temporary node
        new_node = Node(data)
 
        # if head is None, it means that Singly_Linked_List is empty and therefore tail is also None
        if self.head == None:
            # make head to new node that is inserted
            self.head = new_node
            # at this point Singly_Linked_List contains only 1 node , therefore head and tail both will point to it 
            self.tail = new_node
        else:
            # Insert new node at the end of Singly_Linked_List
            self.tail.next = new_node
            # update tail of Singly_Linked_List after inserting 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 will generate random number between 0 and 100000
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:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads