PHP小教程の実現双方向チェーン表


データ構造をたくさん見ましたが、あまり使ったことがありません。インターネットでPHPに関するデータ構造を見ました。勉強して、みんなと共有してみました。前回は「PHP小教程の実現チェーン表」を共有しましたが、今回は双方向リンク表を追加します。

<?php
        class Hero
        {
            public $pre=null;
            public $no;
            public $name;
            public $next=null;
            public function __construct($no='',$name='')
            {
                $this->no=$no;
                $this->name=$name;
            }
            static public function addHero($head,$hero)
            {
                $cur = $head;
                $isExist=false;
                //
                if($cur->next==null)
                {
                    $cur->next=$hero;
                    $hero->pre=$cur;
                }
                else
                {
                    // ,
                    //
                    while($cur->next!=null)
                    {
                        if($cur->next->no > $hero->no)
                        {
                            break;
                        }
                        else if($cur->next->no == $hero->no)
                        {
                            $isExist=true;
                            echo "<br> ";
                        }
                        $cur=$cur->next;
                    }
                    if(!$isExist)
                    {
                        if($cur->next!=null)
                        {
                            $hero->next=$cur->next;
                        }
                        $hero->pre=$cur;
                        if($cur->next!=null)
                        {
                            $hero->next->pre=$hero;
                        }
                        $cur->next=$hero;                   
                    }
                }
            }
            //
            static public function showHero($head)
            {
                $cur=$head;
                while($cur->next!=null)
                {
                    echo "<br> :".$cur->next->no." :".$cur->next->name;
                    $cur=$cur->next;
                }
            }
            static public function delHero($head,$herono)
            {
                $cur=$head;
                $isFind=false;
                while($cur!=null)
                {
                    if($cur->no==$herono)
                    {
                        $isFind=true;
                        break;
                    }
                    //
                    $cur=$cur->next;
                }
                if($isFind)
                {
                    if($cur->next!=null)
                    {
                        $cur->next_pre=$cur->pre;
                    }
                    $cur->pre->next=$cur->next;
                }
                else
                {
                    echo "<br> ";
                }               
            }
        }
        $head = new Hero();
        $hero1 = new Hero(1,'1111');
        $hero3 = new Hero(3,'3333');
        $hero2 = new Hero(2,'2222');
        Hero::addHero($head,$hero1);
        Hero::addHero($head,$hero3);
        Hero::addHero($head,$hero2);
        Hero::showHero($head);
        Hero::delHero($head,2);
        Hero::showHero($head);
?>