请高手给个链表和二叉树相关可执行代码
链表的数据结构:
class Lnode
{
private $data;
private $next;
}
二叉树的数据结构:
class Tnode
{
private $data;
private $lchild;
private $rchild;
}
谢谢!
------解决方案--------------------
PHP array 可以模拟很多种结构。
------解决方案--------------------
- PHP code
<?phpclass Lnode{ public $data; public $next; }class Lists { private $root; function Lists() { $this->root = new Lnode; } function append($v) { $sp =& $this->root; while(! empty($sp)) $sp =& $sp->next; $sp = new Lnode; $sp->data = $v; } function delete($v) { $sp =& $this->root; while(! empty($sp) && $sp->next->data != $v) $sp =& $sp->next; if($sp) $sp->next = $sp->next->next; } }$p = new Lists;$p->append('A');$p->append('B');$p->append('C');print_r($p);$p->delete('B');print_r($p);