Is this a bug I've found or am I doing something wrong?
I'm creating a linked list using classes, with each node instantiated holding pointers to the parent, and its child and next nodes.
Debugging output shows the nodes are getting linked into the tree and the tree built as expected, but calling function dump() to recursively traverse the tree tells me that no child nodes are being stored.
Here's the code:
<?
$root = new node("root");
$n1 = new node("Node 1",&$root);
$n2 = new node("Node 2",&$root);
$n3 = new node("Node 3",&$root);
$n4= new node("Node 4",&$root);
$n5 = new node("Node 5",&$root);
$n11 = new node("Node 1.1",&$n1);
$n111 = new node("Node 1.1.1",&$n11);
$n21 = new node("Node 2.1",&$n2);
$n22 = new node("Node 2.2",&$n2);
$n23 = new node("Node 2.3",&$n2);
$n24 = new node("Node 2.4",&$n2);
$n221 = new node("Node 2.2.1",&$n22);
print("<p>");
$root->dump();
class node
{
var $parent;
var $child;
var $next;
var $label;
function node($label,$parent=0)
{
$this->label = $label;
$this->parent = $parent;
$this->child = array();
$this->next = array();
if ($parent)
{
$parent->linkNewNode(&$this);
}
}
function dump($ind=0)
{
for ($i=0;$i<$ind*5;$i++) print(" ");
print("$this->label<br>");
if ($this->child) $this->child->dump($ind+1);
if ($this->next) $this->next->dump($ind);
}
function linkNewNode(&$newnode)
{
if (!$this->child)
{
print("Added node '$newnode->label' as child 1 of $this->label<br>");
$this->child = $newnode;
return;
}
$ptr = &$this->child;
$ctr = 2;
while ($ptr->next) {
$ptr = &$ptr->next;
$ctr++;
}
print("Added node '$newnode->label' as child $ctr of $this->label<br>");
$ptr->next = $newnode;
}
}
?>