Re: Data Structures
| From: | Ernest E Vogelsinger | Date: | Sun, 09 Feb 2003 14:48:03 +0000 |
| Subject: | Re: Data Structures | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-134946@lists.php.net to get a copy of this message | ||
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/