How to create linked list automatically

Shahriar Shamil Uulu shamilsons at gmail.com
Tue Dec 20 08:49:42 CET 2005


Thank you very much to all,
we have figured it out, how to make it work,
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
    else:
           b=w[i+1]
           a.next=b

we have runned in this way

Dave Hansen wrote:
> On Mon, 19 Dec 2005 20:51:39 +0000 in comp.lang.python, Simon Brunning
> <simon at brunningonline.net> wrote:
>
> >I haven't the time (or inclination) to sort out all your problems
> >here, but one thing jumps out at me:
> >
> >On 12/19/05, Shahriar Shamil Uulu <shamilsons at yahoo.com> wrote:
> >> class Node:
> >>     def __init__(self,name=None,next=None):
> >>         self.name=name
> >>         self.next=next
> >>
> >>     def __str__(self):
> >>         return str(self.name)
> >
> >Your Node classes str() prints the instances self.name attribute,
> >which is set by an optional named argument on the initialiser...
> >
> >> w=[]
> >> for i in range(10):
> >>     node=Node(i)
> >
> >... but you aren't providing this argument when you build your Node objects...
>
> Actually, he is.  Look closer.
>
> As /F pointed out, his problem is in the linking.  By making a small
> modification to his code, I was able to get it to print
>
>    1 2 3 4 5 6 7 8 9 None
>
> As (I assume) was expected.  I'd post the code, but the margin is too
> small to contain it...  Besides, I'd like the OP to figure it out
> himself.
>
> Regards,
>                                         -=Dave
> 
> -- 
> Change is inevitable, progress is not.




More information about the Python-list mailing list