[Tutor] following the chain

jfouhy@paradise.net.nz jfouhy at paradise.net.nz
Wed May 11 03:17:40 CEST 2005


Quoting Liam Clarke <cyresse at gmail.com>:

> While we're on challenge 4, I assume a linked list is important. Can
> anyone point me at a good introduction to linked lists? 

A linked list, at a high level, is a data structure made of "nodes", where each
node contains some data and a pointer to the next node.

Here is a really simple python example:

>>> class Node:
...  pass
...
>>> root = Node()
>>> root.value = "Start"
>>> n = root
>>> for i in range(10):
...  n.next = Node()
...  n.next.value = str(i)
...  n = n.next
... else:
...  n.next = None
...
>>> # We now have a linked list starting at root.
...
>>> n = root
>>> while n != None:
...  print n.value
...  n = n.next
...
Start
0
1
2
3
4
5
6
7
8
9
>>>

Of course, the pointer to the next node could be anything; as long as your
program logic knows how to use it to get to the next node.

One of the advantages linked lists have over arrays in languages like C++ is
that they can be as big as you like, whereas arrays are created with a fixed
side.  But python lists don't have that problem, so they're a lot less useful
here...

-- 
John.


More information about the Tutor mailing list