[thelist] PHP, MySQL and Linked List

Matt Warden mwarden at gmail.com
Tue May 31 12:05:05 CDT 2005


Hershel,

On 5/31/05, Hershel Robinson <hershelr at netvision.net.il> wrote:
> I have a linked list structure which I am storing in a MySQL database. The
> site code is in PHP. A few tasks which the code must do:
> 
> 1 Display the nodes of the list in order (i.e. traversing the links).
...
> The only algorithm about which I am not thrilled is the first one above, to
> display the entire list in order. The method I am using is to load the
> entire table, putting each node (and all of its relevant data) into a node
> object, an instance of a class I defined. Once the entire table is loaded,
> the code can then loop through the nodes, beginning with ID 1 (the root),
> following the links, until we come to ID 0 which means done.
> 
> I am not aware of any better way to do this, and the only negative I really
> see in this method is that the entire table must be loaded into PHP data
> structures before processing can begin, but this done seem non-ideal.
> 
> Anyone have any better ideas?


It depends on how many different lists (i.e., how many in addition to
the list of interest) are in the table. If the number is large, you
might be better off selecting the root node, finding the nextId, then
selecting that node, and finding its nextId, etc. (recursion would
obviously be a good choice here). However, if the number is
manageable, you are probably using hte best solution already.

Your requirements didn't mention the need to move a node from one list
to another, so another option is to have a list identifier for each
node. Then you could filter on that identifier in the where clause and
only return the relevant records.

One caveat: one might argue that this defeats part of the purpose of a
linked list, as the node must know about the entire list in which it
is contained, and not simply the next (and previous, for a doubly
linked list) node.

-- 
Matt Warden
Miami University
Oxford, OH, USA
http://mattwarden.com


This email proudly and graciously contributes to entropy.


More information about the thelist mailing list