博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP算法每日一练 -- 单链表
阅读量:4708 次
发布时间:2019-06-10

本文共 2344 字,大约阅读时间需要 7 分钟。

1 
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.'  ';18 echo '英雄姓名:'.$cur->next->name.'  ';19 echo '英雄昵称:'.$cur->next->cname.'  ';20 echo '
';21 $cur = $cur->next;22 }23 echo '

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

 

源代码下载:

 

 

转载于:https://www.cnblogs.com/linzhenjie/archive/2012/12/02/2798889.html

你可能感兴趣的文章
NSArray和NSMutableArray的常用方法 (转)
查看>>
java PDF分页打印
查看>>
数链剖分小结
查看>>
应用nslookup命令查看A记录、MX记录、CNAME记录和NS记录
查看>>
APT攻击
查看>>
做衡八的日子(转自VFleaking)
查看>>
day7.条件和循环
查看>>
(转)log4j(二)——如何控制日志信息的输出?
查看>>
JavaScript简介
查看>>
php.ini中safe_mode开启对PHP系统函数的影响
查看>>
gdb
查看>>
字符串与整数、浮点数、无符号整数之间的转换常用函数
查看>>
ubuntu清理旧内核
查看>>
有关UIImageView+AFNetworking 下载图片的线程问题
查看>>
Node之安装篇
查看>>
Android的Animation之LayoutAnimation使用方法
查看>>
二分图最大匹配算法-Hopcroft-Karp模板
查看>>
发布和订阅的删除
查看>>
如何使用qtp12 utf进行功能测试
查看>>
使用LinQ进行增删改查
查看>>