Can double-linked lists be implemented in python?

Peter Hansen peter at engcorp.com
Wed Apr 2 08:38:42 EST 2003


sdieselil wrote:
> 
> How can I implement double-linked lists and other complex tree-like
> structures in Python?

In precisely the same way you would go about doing it with any
other language, really...  you could take a stab at it, say, using 
the interactive interpreter and see how far you get, then ask for
help when you've hit a wall.

For a start: (untested code)

>>> class Node:
...   def __init__(self):
...     self.prev = self.next = None
...
>>> class LinkedList:
...   def __init__(self):
...     self.head = self.tail = None
...   def append(self, node):
...     if self.tail:
...       self.tail.next = node
...       node.prev = self.tail
...     else:
...       self.head = self.tail = node

and other stuff like that...

Of course, you might also want to do a Google search and see
if there is an existing implementation.  Ask here again if you
need help using Google. ;-)

-Peter




More information about the Python-list mailing list