Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python program to select random node #1409

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions 1.linked list/random_node_from_SLL.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

# coding: utf-8

# In[7]:


import random

# Creation of a node
class Node:

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

# Linking of nodes
class LinkedList:

# function to initialize head
def __init__(self):
self.head = None

# function to print a random node from a linkd list
def printRandom(self):

# Checks if list is empty
if self.head is None:
return
if self.head and not self.head.next:
print("Randomly selected key is %d" %(self.head.data))

# Use a different seed value so that we don't get
# same result each time we run this program
random.seed()

# Initialize result as first node
result = self.head.data

current = self.head.next
n = 2
while(current is not None):

if (random.randrange(n) == 0 ):
result = current.data

# Move to next node
current = current.next
n += 1

print ("Randomly selected key is %d" %(result))

# function to insert a new node at the beginning
def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node

# Utility function to print the LinkedList
def printList(self):
temp = self.head
while(temp):
print (temp.data),
temp = temp.next


llist = LinkedList()
llist.push(51)
llist.push(100)
llist.push(60)
llist.push(89)
llist.push(32)
llist.push(87)
llist.push(55)
llist.push(72)
llist.push(90)
llist.push(93)
llist.push(40)
llist.printRandom()