How to create linked list automatically

  • Thread starter Shahriar Shamil Uulu
  • Start date
S

Shahriar Shamil Uulu

Hi All,
i have this program,
=================================================================
class Node:
def __init__(self,name=None,next=None):
self.name=name
self.next=next

def __str__(self):
return str(self.name)

w=[]
for i in range(10):
node=Node(i)
w.append(node)

for i in range(10):
a=w
if i+1>9:
b=w[9]
a.next=b

print "connection done"

def printNodes(node):
while node:
node=node.next
print node,

a=w[0]
printNodes(a)
====================================================================
i want to create 10 node objects and i want to connect them to each
other as linked list, so i have created two loops in first one iam
creating 10 objects and inserting them to list and in the second i
am trying to connect them, but when i try to print the connection i
got this:
===================================================================connection done
Nonecan anybody tell me how to overcome this
====================================================================
2. is there any way to create objects something like this
for i in range(10):
node.i=Node(i) # some thing like node1=Node(1) etc.

Please help me with this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,125
Latest member
VinayKumar Nevatia_
Top