当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP小教程之实现双向链表

发布:smiling 来源: PHP粉丝网  添加日期:2021-02-11 10:47:18 浏览: 评论:0 

双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。一般我们都构造双向循环链表。

看了很久数据结构但是没有怎么用过,在网上看到了关于PHP的数据结构,学习了一下,与大家一起分享一下。上一次分享了《PHP小教程之实现链表》,这次来补充说一下双向链表,代码如下:

  1. <?php 
  2.         class Hero 
  3.         { 
  4.             public $pre=null; 
  5.             public $no
  6.             public $name
  7.             public $next=null; 
  8.             public function __construct($no='',$name=''
  9.             { 
  10.                 $this->no=$no
  11.                 $this->name=$name
  12.             } 
  13.             static public function addHero($head,$hero
  14.             { 
  15.                 $cur = $head
  16.                 $isExist=false; 
  17.                 //判断目前这个链表是否为空 
  18.                 if($cur->next==null) 
  19.                 { 
  20.                     $cur->next=$hero
  21.                     $hero->pre=$cur
  22.                 } 
  23.                 else 
  24.                 { 
  25.                     //如果不是空节点,则安排名来添加 
  26.                     //找到添加的位置 
  27.                     while($cur->next!=null) 
  28.                     { 
  29.                         if($cur->next->no > $hero->no) 
  30.                         { 
  31.                             break
  32.                         } 
  33.                         else if($cur->next->no == $hero->no) 
  34.                         { 
  35.                             $isExist=true; 
  36.                             echo "<br>不能添加相同的编号"
  37.                         } 
  38.                         $cur=$cur->next; 
  39.                     } 
  40.                     if(!$isExist
  41.                     { 
  42.                         if($cur->next!=null) 
  43.                         { 
  44.                             $hero->next=$cur->next; 
  45.                         } 
  46.                         $hero->pre=$cur
  47.                         if($cur->next!=null) 
  48.                         { 
  49.                             $hero->next->pre=$hero
  50.                         } 
  51.                         $cur->next=$hero;                    
  52.                     } 
  53.                 } 
  54.             } 
  55.             //遍历 
  56.             static public function showHero($head
  57.             { 
  58.                 $cur=$head
  59.                 while($cur->next!=null) 
  60.                 { 
  61.                     echo "<br>编号:".$cur->next->no."名字:".$cur->next->name; 
  62.                     $cur=$cur->next; 
  63.                 } 
  64.             } 
  65.             static public function delHero($head,$herono
  66.             { 
  67.                 $cur=$head
  68.                 $isFind=false; 
  69.                 while($cur!=null) 
  70.                 { 
  71.                     if($cur->no==$herono
  72.                     { 
  73.                         $isFind=true; 
  74.                         break
  75.                     } 
  76.                     //继续找 
  77.                     $cur=$cur->next; 
  78.                 } 
  79.                 if($isFind
  80.                 { 
  81.                     if($cur->next!=null) 
  82.                     { 
  83.                         $cur->next_pre=$cur->pre; 
  84.                     } 
  85.                     $cur->pre->next=$cur->next; 
  86.                 } 
  87.                 else 
  88.                 { 
  89.                     echo "<br>没有找到目标"
  90.                 }                
  91.             } 
  92.         } 
  93.         $head = new Hero(); 
  94.         $hero1 = new Hero(1,'1111'); 
  95.         $hero3 = new Hero(3,'3333'); 
  96.         $hero2 = new Hero(2,'2222'); 
  97.         Hero::addHero($head,$hero1); 
  98.         Hero::addHero($head,$hero3); 
  99.         Hero::addHero($head,$hero2); 
  100.         Hero::showHero($head); 
  101.         Hero::delHero($head,2); 
  102.         Hero::showHero($head); 
  103. ?> 

Tags: PHP双向链表

分享到:

相关文章