Open In App

Random Singly Linked List Generator using Python

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 random




# Node class
class Node:
    # constructor to initialize the node object
    def __init__(self,data):
        self.data = data
        self.next = None

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).




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()

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.






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:




# 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:




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:

 


Article Tags :