Creating Linked Lists in Python

andrew cooke andrew at acooke.org
Sat Mar 21 09:11:48 EDT 2009


J-Burns wrote:
> I need to make a linked list that can do the following:
>
> 1) Point to multiple nodes at one time
> 2) Should have 2 values:
>     a) The node no.
>     b) The value of that node in reference to the next node that it is
> pointing to
>
> For example, this means that there can be a start node supposedly.
> Having a value of 0. It is pointing to node 1 with the value of "a"
> and to node 2 with the value of "b". Trying to make something like an
> NFA. Where id be changing regular expressions to NFAs.
>
> How can I do this? And if I could do this by some other way than using
> linked lists than do tell me about that as well.

when you mention NFAs and regular expressions, you make me think that you
don't want a linked list at all, but a graph.  there are various ways of
storing a graph, but one of the simplest is to use a dict() that maps from
source to destination node(s).

in your case you could use ints for the nodes and a dict([int]) for the
graph.  so:

{1: [2,3], 2: [3], 3: [3]}

is a graph in which 1 and 2 are connected in each direction, both 1 and 2
are linked to 3, and 3 has a loop that links back to itself.

andrew





More information about the Python-list mailing list