Random Singly Linked List Generator using Python
Generating random singly linked list using Python having elements (numbers, primes, alphabets)
Initialize Node for linked list
Singly Linked List data structure is created with insertion at end process.
class Node:
def __init__(self, val):
self.val = val
self.next = None
class SLL:
def __init__(self):
self.head = None
self.tail = None
def insertEnd(self,val):
node = Node(val)
if not self.head:
self.head = self.tail = node
return
self.tail.next = node
self.tail = node
random => start to end both included with
randSLL() takes parameters start (starting number), end (ending number) and count (number of elements in the linked list, default 1). This function generates a singly linked list consist of random numbers from start to end (both start and end included) with length of count and returns the head of the singly linked list.
def randSLL(start:int, end:int, count:int = 1):
sll = SLL()
for _ in range(count):
rval = random.randint(start,end)
sll.insertEnd(rval)
return sll.headrandom => start to end both included odd
randOddSLL() takes parameters start (starting number), end (ending number) and count (number of elements in the linked list, default 1). This function generates a singly linked list consist of random odd numbers from start to end (both start and end included) with length of count and returns the head of the singly linked list.
def randOddSLL(start:int, end:int, count:int = 1):
sll = SLL()
while count>0:
rval = random.randint(start,end)
if rval%2!=0:
sll.insertEnd(rval)
count-=1
return sll.headrandom => start to end both included even
randEvenSLL() takes parameters start (starting number), end (ending number) and count (number of elements in the linked list, default 1). This function generates a singly linked list consist of random even numbers from start to end (both start and end included) with length of count and returns the head of the singly linked list.
def randEvenSLL(start:int, end:int, count:int = 1):
sll = SLL()
while count>0:
rval = random.randint(start,end)
if rval%2==0:
sll.insertEnd(rval)
count-=1
return sll.headrandom => PRIME
randPrime() takes parameters start (starting number), end (ending number) and count (number of elements in the linked list, default 1). This function generates a singly linked list consist of random prime numbers from start to end (both start and end included) with length of count and returns the head of the singly linked list.
def isPrime(n:int):
if n<=3 and n>1:
return 1
if n%2==0 or n%3==0:
return 0
i=5
while i*i<n:
if n%i==0 or n%(i+2)==0:
return False
i+=6
return True
def randPrimeSLL(start:int, end:int, count:int = 1):
arr = []
sll = SLL()
for i in range(start,end+1):
pval = i
if pval<0:
pval*=-1
if isPrime(pval):
arr.append(i)
for _ in range(count):
sll.insertEnd(random.choice(arr))
return sll.headrandom => alphabets (uppercase, lowercase, both uppercase and lowercase)
randAlphaSLL() takes parameters type (type of alphabet), count (number of elements in the linked list, default 1). This function generates a singly linked list consist of random alphabets with length of count and returns the head of the singly linked list. Parameter type takes 3 types of values (“UPPER”, “LOWER”, “MIXED”). “UPPER” for only upper case alphabets. “LOWER” for only lower case alphabets. “MIXED” for both upper and lower case alphabets.
def randAlphaSLL(type:str, count:int = 1):
LCASE = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
sll = SLL()
if type == "UPPER":
for _ in range(count):
sll.insertEnd(random.choice(LCASE).upper())
elif type == "LOWER":
for _ in range(count):
sll.insertEnd(random.choice(LCASE))
elif type == "MIXED":
for _ in range(count):
rval = random.choice(LCASE)
if random.choice([True,False]):
rval = rval.upper()
sll.insertEnd(rval)
return sll.headExample
import randomSLL
def display(head):
curr = head
while curr:
print(curr.val,end=" ")
curr = curr.next
print()
headRand = randomSLL.randSLL(1, 10, 5)
headRandOdd = randomSLL.randOddSLL(1, 20, 5)
headRandEven = randomSLL.randEvenSLL(1, 20, 5)
headRandPrime = randomSLL.randPrimeSLL(1, 20, 5)
headRandUpper = randomSLL.randAlphaSLL("UPPER", 10);
headRandLower = randomSLL.randAlphaSLL("LOWER", 10);
headRandMixed = randomSLL.randAlphaSLL("MIXED", 10);
print("Random Singly Linked List")
display(headRand)
print("Random Singly Linked List - Odd")
display(headRandOdd)
print("Random Singly Linked List - Even")
display(headRandEven)
print("Random Singly Linked List - Prime")
display(headRandPrime)
print("Random Singly Linked List - Uppercase")
display(headRandUpper)
print("Random Singly Linked List - Lowercase")
display(headRandLower)
print("Random Singly Linked List - both Uppercase and Lowercase")
display(headRandMixed)Output for above example
Random Singly Linked List 1 7 6 7 3 Random Singly Linked List - Odd 7 5 9 15 11 Random Singly Linked List - Even 20 14 10 18 16 Random Singly Linked List - Prime 7 19 11 13 13 Random Singly Linked List - Uppercase R G V Y S Z O T N P Random Singly Linked List - Lowercase n x e i o t f f n w Random Singly Linked List - both Uppercase and Lowercase l O V a r I k D M V
Leave a Reply