• PHP算法每日一练 单链表


     1 <?php
     2 /**单向链表 -- 水浒传英雄排行版*/
     3 class hero{
     4     public $name = '';
     5     public $no ='';
     6     public $cname = '';
     7     public $next = '';
     8     public function __construct($no = '',$name='',$cname=''){
     9         $this->no = $no;
    10         $this->name = $name;
    11         $this->cname = $cname;
    12     }
    13     //显示英雄链表
    14     public static function showHero($head){
    15         $cur = $head;
    16         while($cur->next != null){
    17             echo '英雄编号:'.$cur->next->no.'&nbsp;&nbsp;';
    18             echo '英雄姓名:'.$cur->next->name.'&nbsp;&nbsp;';
    19             echo '英雄昵称:'.$cur->next->cname.'&nbsp;&nbsp;';
    20             echo '<br />';
    21             $cur  = $cur->next;
    22         }
    23         echo '<hr />';
    24     }
    25     //英雄加到链表末端
    26     public static function addHero($head,$hero){
    27         $cur = $head;
    28         while($cur->next != null){
    29             $cur = $cur->next;
    30         }
    31         $cur->next  = $hero;
    32     }
    33     //英雄按排序加入
    34     public static function addHero2($head,$hero){
    35         $cur = $head;
    36         while($cur->next != null){
    37             if($cur->next->no  > $hero->no){
    38                 break;
    39             }
    40             $cur = $cur->next;
    41         }
    42         $hero->next  = $cur->next;
    43         $cur->next  = $hero;
    44     }
    45     //修改英雄
    46     public static function editHero($head,$hero){
    47         $cur = $head;
    48         while($cur->next != null){
    49             if($cur->next == $hero){
    50                 break;
    51             }
    52             $cur = $cur->next;
    53         }
    54         $cur->next->name  = $hero->name;
    55         $cur->next->cname  = $hero->cname;
    56     }
    57     //删除英雄
    58     public static function delHero($head,$hero){
    59         $cur = $head;
    60         while($cur->next != null){
    61             if($cur->next == $hero){
    62                 break;
    63             }
    64             $cur = $cur->next;
    65         }
    66         $cur->next  = $hero->next;
    67         $hero->next  = null;
    68     }
    69 }
    70 
    71 $head = new hero();
    72 $songjiang =new hero(1,'宋江','及时雨');
    73 $lujunyi =new hero(2,'卢俊义','玉麒麟');
    74 
    75 $head->next = $songjiang;
    76 $songjiang->next = $lujunyi;
    77 
    78 //默认
    79 hero::showHero($head);
    80 //加入到链表末端
    81 $linchong = new hero(6,'林冲','豹子头');
    82 hero::addHero($head,$linchong);
    83 hero::showHero($head);
    84 //按排名加入链表
    85 $wuyong = new hero(3,'吴用','智多星');
    86 hero::addHero2($head,$wuyong);
    87 hero::showHero($head);
    88 //修改英雄
    89 $wuyong->name='吴用2';
    90 hero::editHero($head,$wuyong);
    91 hero::showHero($head);
    92 //删除英雄
    93 hero::delHero($head,$wuyong);
    94 hero::showHero($head);
    95 
    96 ?>

    源代码下载:SingleLine_1202.zip

  • 相关阅读:
    实例变量和类变量
    Spring源码解读(一)
    linux shell学习三
    linux shell学习二
    linux shell学习一
    利用Zynq Soc创建一个嵌入式工程
    (转) 使用vivado创建工程 4[完结]
    (转) 使用vivado创建工程 3
    (转) 使用vivado创建工程 2
    (转) 使用vivado创建工程 1
  • 原文地址:https://www.cnblogs.com/linzhenjie/p/2798889.html
Copyright © 2020-2023  润新知