On Sun, Dec 7, 2008 at 10:09 PM, James Mills <[EMAIL PROTECTED]> wrote: > Hi, > > This is really really really pointless code and a really really pointless > exercise, but nonetheless, here is a very very basic and minimal > implementation of what you're expecting. This should almost > *never* be done in Python! Python is a superior dynamic programming > language, but it's NOT C! > > Here goes: > > [EMAIL PROTECTED]:~/tmp$ ./list.py >>>> x[0] > 0 >>>> x[1] > 1 >>>> x[2] > 2 >>>> x[3] > 3 >>>> x[4] > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > File "./list.py", line 36, in __getitem__ > return node.value > AttributeError: 'NoneType' object has no attribute 'value' >>>> > [EMAIL PROTECTED]:~/tmp$ > > And the code: > > #!/home/jmills/bin/python -i > > class Node(object): >
The following three lines serve no purpose and can only lead to confusion: > value = None > prev = None > next = None > > def __init__(self, value, prev=None, next=None): > self.value = value > self.prev = prev > self.next = next > > class List(object): > Same with the following line. Why do you have these? Python does *not* have the concept of instance variable declaration. You are creating *class variables*, which is almost certainly not what you want. > data = None > Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com > def __init__(self, *seq): > if seq: > first = prev = node = None > for x in seq: > if not first: > first = Node(x) > prev = node = first > else: > node = Node(x, prev) > prev.next = node > prev = node > > self.data = first > > def __getitem__(self, x): > node = self.data > for i in xrange(x): > node = node.next > return node.value > > x = List(0, 1, 2, 3) > > > Notes: > > I have not implemented any error checking whatsoever. > I have not implemented any of your normal list > operations whatsoever (except 1). __getitem__. > > Have fun, > > cheers > James > > On Mon, Dec 8, 2008 at 3:26 PM, Group <[EMAIL PROTECTED]> wrote: >> Hello, I'm studying algorithom. For concentrating on the question itself, I >> intend to use Python to implement the algorithoms. >> >> Now, I want to write a Red-Black Tree, and a List structure. In C/C++, I can >> use pointers to refer to children notes (or next notes). But, in Python, >> how >> can I do it? Except the sequence, I know not any way. >> >> You'd better help me understan how can I transform the following C code into >> Python: >> >> /* a List */ >> struct { >> int data; >> int *next; >> int *prev; >> } >> >> That's all. Thanks! >> Kermit >> >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > > > > -- > -- > -- "Problems are solved by method" > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list