At 04:43 09.02.2003, Laborda said:
--------------------[snip]--------------------
>Does anyone know where can I find information about data structures
>implemented in PHP. I need to find something about Linked Lists, implemented
>in PHP.. If anyone has any info, I'd appreciate it. I've tried to google it
>out but I can't find anything. Thanks.
--------------------[snip]--------------------
As another poster already pointed out associative arrays may be one way to
go /although there are multiple solutions to linked lists, as in many
languages).
However there is one caveat you absolutely need to observe: in PHP there
are NO POINTERS. "Normally" assigning variables of any type will create a
copy of the variable. like this:
$a1 = array(1,2,3);
$a2 = $a1;
Here, $a2 is a COPY of $a1:
$a2[3] = 4;
echo join(',',$a1), ' - ', join(',',$a2);
will produce
1,2,3 - 1,2,3,4
OTOH, you may use REFERENCES:
$a2 =& $a1;
$a2[3] = 4;
echo join(',',$a1), ' - ', join(',',$a2);
will now produce
1,2,3,4 - 1,2,3,4
This said, using array entries for linked lists, you must absolutely make
sure to always use references, not simple assignments.
Assuming a "structure" built as PHP array (unfortunately there's no such
thing as a typedef):
$start = array('prev' => null, 'next' => null, 'data' => 'First entry');
$elem = array('prev' => &$start, 'next' => null, 'data' => 'Second
entry');
$start['next'] =& $elem;
You get the picture, I believe. Just make sure the "pointers" are actually
references to the list elements, or everything will break up.
HTH,
--
>O Ernest E. Vogelsinger
(\) ICQ #13394035
^ http://www.vogelsinger.at/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php