How to create linked list automatically

Shahriar Shamil Uulu shamilsons at yahoo.com
Mon Dec 19 15:12:46 EST 2005


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[i]
    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
None
>>> 
can 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.






More information about the Python-list mailing list