利用PHP怎么实现一个双向链表功能-创新互联

利用PHP怎么实现一个双向链表功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

创新互联公司始终坚持【策划先行,效果至上】的经营理念,通过多达10年累计超上千家客户的网站建设总结了一套系统有效的全网推广解决方案,现已广泛运用于各行各业的客户,其中包括:成都卫生间隔断等企业,备受客户夸奖。

代码如下:



<?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);
?>


看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联网站建设公司,的支持。

分享标题:利用PHP怎么实现一个双向链表功能-创新互联
链接分享:https://www.cdcxhl.com/article0/eegio.html

成都网站建设公司_创新互联,为您提供软件开发App设计服务器托管标签优化建站公司定制开发

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

h5响应式网站建设