色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

PHP小教程之實現(xiàn)雙向鏈表

看了很久數(shù)據(jù)結構但是沒有怎么用過,在網(wǎng)上看到了關于php的數(shù)據(jù)結構,學習了一下,與大家一起分享一下。上一次分享了《php小教程之實現(xiàn)鏈表》,這次來補充說一下雙向鏈表。

復制代碼 代碼如下:
<?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
                {
                    //如果不是空節(jié)點,則安排名來添加
                    //找到添加的位置
                    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;
                    }
                    //繼續(xù)找
                    $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);
?>

php技術PHP小教程之實現(xiàn)雙向鏈表,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 万源市| 普宁市| 安泽县| 隆安县| 桦南县| 常熟市| 泰宁县| 宿州市| 左权县| 延边| 名山县| 萍乡市| 衢州市| 尼勒克县| 巨野县| 大连市| 景洪市| 绥滨县| 大渡口区| 盘锦市| 兴国县| 陆河县| 大同市| 镇平县| 团风县| 保靖县| 巴楚县| 梓潼县| 永州市| 于都县| 神木县| 金塔县| 上蔡县| 玛纳斯县| 祁东县| 五大连池市| 格尔木市| 霍邱县| 达州市| 炎陵县| 开平市|