468,249 Members | 1,489 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,249 developers. It's quick & easy.

Making a Linked List

I'm trying to create a heirarchical linked list of sorts, and I'm
looking for advice as to how best to set up the arrays. In other
languages, I might use a pointer array to other pointer arrays, but in
PHP, I'm a bit stumped.

What I have is a list of simple, small arrays. I'd like to figure out
the appropriate data structure so that each element knows which
element is it's "parent," and so that each element can have multiple
children. The best way to think of this is like an outline wherein
each fork has a variable number of elements.

The tricky part is that given a specific element, I'd like to be able
to easily list its children's IDs.

I'm storing the individual elements in a MySQL database. So for each
element, I have a field with the data (just text) and a field telling
the unique ID number of the element; and then I suppose I can have an
additional field containing the ID of the parent.

My best guess so far as to how to do this is to read all of the
elements from the database. As they come in one by one, I create an
array for each one to conain a list of its children. When a specific
element comes in, we ask for its parent ID and then add its ID to the
parent's array-of-children.

Will this work, or is there a better way?

Thanks,
M. Katz
Jul 17 '05 #1
2 2153
MK******@onebox.com (M. Katz) wrote in message news:<4a**************************@posting.google. com>...
I'm trying to create a heirarchical linked list of sorts, and I'm
looking for advice as to how best to set up the arrays. In other
languages, I might use a pointer array to other pointer arrays, but in
PHP, I'm a bit stumped.

Will this work, or is there a better way?

Thanks,
M. Katz

Ditto that. Here's a code fragment that I thought should work.
But NO. It has to do with call by value/reference:
------------------------------------------------------------------
<?
class Hierarchy
{
var $name;
var $parent;
var $member;
function show($msg){ print "\n$msg:\t$this->name\n"; var_export($this); }
function Hierarchy($na, $pa)
{
print "function Hierarchy($na, $pa)\n";
$this->name = $na;
$this->parent = $pa;
if ($pa)
{
print "PA is GOOD\n";
$pa->member[$na] = $this;
print_r($pa);
}
}
}
$top = new Hierarchy("TOP", NULL);
$midl = new Hierarchy("Middle", $top);
$botm = new Hierarchy("Bottom A1", $midl);
$botm = new Hierarchy("Bottom B2", $midl);
$midl->show("Bottom: ");
// $top->show("TOP: ");
// $midl->parent->show("Middle 1");
// $midl->parent->parent->show("Bottom");

?>
--------------------------------------------------------------------

the "print_r($pa) shows the Parent and its sub-structure,
avoiding unnecessary recursion. However, after exiting the
"New Hierarchy", the "$midl->show()" for example, has none
of the substructure. I tried the "pass by reference"
notation, both in the calling, and called argument list.
The latter is a syntax error, the former a warning.
Replaceing the "$top" and "$midl" arguements with "$r_top"
and "$r_midl", having "$r_top = & $top; ..." was of no avail.

Help?

-- Marty McGowan
Jul 17 '05 #2
Pass $parent by reference, and assign it to the class variable using =&.

Also beware of the fact that the new operator returns a copy of the newly
created object. So referencing $this in the constructor doesn't work.

Here's some excerpts from my ISO class illustrating this:

class ISO_JolietDirectory extends ISO_Directory {

function ISO_JolietDirectory(&$iso) {
parent::ISO_Directory($iso);
}

function AppendChild($identifier, &$child, $is_dir) {
$unicode_id = ConvertToUCS16($identifier);
$this->children[$unicode_id] =& $child;
if($is_dir) {
$this->has_sub_dir = true;
}
return $unicode_id;
}

/* inherited from ISO_Directory, actually */
function AttachParent(&$parent) {
$this->parent =& $parent;
}
}

The code that add a directory to the tree is like this:

$jo_parent->AppendChild($dir_id, $jo_dir, true);
$jo_dir->AttachParent($jo_parent);

Uzytkownik "Marty McGowan" <mc*****@alum.mit.edu> napisal w wiadomosci
news:39**************************@posting.google.c om...
MK******@onebox.com (M. Katz) wrote in message

news:<4a**************************@posting.google. com>...
I'm trying to create a heirarchical linked list of sorts, and I'm
looking for advice as to how best to set up the arrays. In other
languages, I might use a pointer array to other pointer arrays, but in
PHP, I'm a bit stumped.

Will this work, or is there a better way?

Thanks,
M. Katz

Ditto that. Here's a code fragment that I thought should work.
But NO. It has to do with call by value/reference:
------------------------------------------------------------------
<?
class Hierarchy
{
var $name;
var $parent;
var $member;
function show($msg){ print "\n$msg:\t$this->name\n"; var_export($this); }
function Hierarchy($na, $pa)
{
print "function Hierarchy($na, $pa)\n";
$this->name = $na;
$this->parent = $pa;
if ($pa)
{
print "PA is GOOD\n";
$pa->member[$na] = $this;
print_r($pa);
}
}
}
$top = new Hierarchy("TOP", NULL);
$midl = new Hierarchy("Middle", $top);
$botm = new Hierarchy("Bottom A1", $midl);
$botm = new Hierarchy("Bottom B2", $midl);
$midl->show("Bottom: ");
// $top->show("TOP: ");
// $midl->parent->show("Middle 1");
// $midl->parent->parent->show("Bottom");

?>
--------------------------------------------------------------------

the "print_r($pa) shows the Parent and its sub-structure,
avoiding unnecessary recursion. However, after exiting the
"New Hierarchy", the "$midl->show()" for example, has none
of the substructure. I tried the "pass by reference"
notation, both in the calling, and called argument list.
The latter is a syntax error, the former a warning.
Replaceing the "$top" and "$midl" arguements with "$r_top"
and "$r_midl", having "$r_top = & $top; ..." was of no avail.

Help?

-- Marty McGowan

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by C++fan | last post: by
5 posts views Thread by Dream Catcher | last post: by
6 posts views Thread by Steve Lambert | last post: by
12 posts views Thread by Eugen J. Sobchenko | last post: by
12 posts views Thread by joshd | last post: by
9 posts views Thread by tiwarinitin.3108 | last post: by
reply views Thread by Atos | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.