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

PHP小教程之实现链表

发布:smiling 来源: PHP粉丝网  添加日期:2021-02-10 14:29:30 浏览: 评论:0 

php中没有链表这种数据结构,可以通过数组来实现,看了很久数据结构但是没有怎么用过,在网上看到了关于PHP的数据结构,学习了一下,与大家一起分享一下,代码如下:

  1. class Hero 
  2. { 
  3.     public $no;//排名 
  4.     public $name;//名字 
  5.     public $next=null;//$next是一个引用,指向另外一个Hero的对象实例 
  6.  
  7.     public function __construct($no='',$name='') 
  8.     { 
  9.         $this->no=$no; 
  10.         $this->name=$name; 
  11.     } 
  12.  
  13.     static public function showList($head) 
  14.     { 
  15.         $cur = $head; 
  16.         while($cur->next!=null) 
  17.         { 
  18.             echo "排名:".$cur->next->no.",名字:".$cur->next->name."<br>"; 
  19.             $cur = $cur->next; 
  20.         } 
  21.     } 
  22.     //普通插入 
  23.     static public function addHero($head,$hero) 
  24.     { 
  25.         $cur = $head; 
  26.         while($cur->next!=null) 
  27.         { 
  28.             $cur = $cur->next; 
  29.         } 
  30.         $cur->next=$hero; 
  31.     } 
  32.     //有序的链表的插入  
  33.     static public function addHeroSorted($head,$hero) 
  34.     { 
  35.         $cur = $head; 
  36.         $addNo = $hero->no; 
  37.         while($cur->next->no <= $addNo) 
  38.         { 
  39.             $cur = $cur->next; 
  40.         } 
  41.         /*$tep = new Hero(); 
  42.         $tep = $cur->next; 
  43.         $cur->next = $hero; 
  44.         $hero->next =$tep;*/ 
  45.         $hero->next=$cur->next; 
  46.         $cur->next=$hero; 
  47.     } 
  48.  
  49.     static public function deleteHero($head,$no) 
  50.     { 
  51.         $cur = $head; 
  52.         while($cur->next->no != $no && $cur->next!= null) 
  53.         { 
  54.             $cur = $cur->next; 
  55.         } 
  56.         if($cur->next->no != null) 
  57.         { 
  58.             $cur->next = $cur->next->next; 
  59.             echo "删除成功<br>"; 
  60.         } 
  61.         else 
  62.         { 
  63.             echo "没有找到<br>"; 
  64.         } 
  65.     } 
  66.  
  67.     static public function updateHero($head,$hero) 
  68.     { 
  69.         $cur = $head; 
  70.         while($cur->next->no != $hero->no && $cur->next!= null) 
  71.         { 
  72.             $cur = $cur->next; 
  73.         } 
  74.         if($cur->next->no != null) 
  75.         { 
  76.             $hero->next = $cur->next->next; 
  77.             $cur->next = $hero; 
  78.             echo "更改成功<br>"; 
  79.         } 
  80.         else 
  81.         { 
  82.             echo "没有找到<br>"; 
  83.         } 
  84.     } 
  85. } 
  86. //创建head头 
  87. $head = new Hero(); 
  88. //第一个 
  89. $hero = new Hero(1,'111'); 
  90. //连接 
  91. $head->next = $hero; 
  92. //第二个 
  93. $hero2 = new Hero(3,'333'); 
  94. //连接 
  95. Hero::addHero($head,$hero2); 
  96. $hero3 = new Hero(2,'222'); 
  97. Hero::addHeroSorted($head,$hero3); 
  98. //显示 
  99. Hero::showlist($head); 
  100. //删除 
  101. Hero::deleteHero($head,4); 
  102. //显示 
  103. Hero::showlist($head); 
  104. //更改 
  105. $hero4=new Hero(2,'xxx'); 
  106. Hero::updateHero($head,$hero4); 
  107. //显示 
  108. Hero::showlist($head); 

有序的插入的话需要遍历一遍链表,链表的一些知识就不介绍了哈。这里主要分享一下代码。

Tags: PHP链表

分享到: