您当前的位置: 首页 > 网站编程 > PHP教程 > PHP小教程之实现双向链表

PHP小教程之实现双向链表

作者:不详 来源:网络 发布时间: 2014-07-30 09:23 点击:
看了很久数据结构但是没有怎么用过,在网上看到了关于PHP的数据结构,学习了一下,与大家一起分享一下。 .代码如下: ?php class Hero { public $pre=null; public $no; public $name; public $next=null; public function __construct($no='',$name='') { $this-no=$no

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

  ?>
分享到:
本文"PHP小教程之实现双向链表"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4