php单链表实现代码分享
php  /  管理员 发布于 7年前   122
本文实例为大家分享了php单链表的具体代码,供大家参考,具体内容如下 以上就是本文的全部内容,希望对大家实现php单链表有所帮助。id = $id; $this->name = $name; } static public function show ($head) { $cur = $head; while ($cur->next) { echo $cur->next->id,'###',$cur->next->name,'
'; $cur = $cur->next; } echo '
'; } //尾插法 static public function push ($head, $node) { $cur = $head; while (NULL != $cur->next) { $cur = $cur->next; } $cur->next = $node; return $head; } static public function insert($head, $node) { $cur = $head; while (NULL != $cur->next) { if ($cur->next->id > $node->id) { break; } $cur = $cur->next; } $node->next = $cur->next; $cur->next = $node; return $head; } static public function edit($head, $node) { $cur = $head; while (NULL != $cur->next) { if ($cur->next->id == $node->id) { break; } $cur = $cur->next; } $cur->next->name = $node->name; return $head; } static public function pop ($head, $node) { $cur = $head; while (NULL != $cur->next) { if ($cur->next == $node) { break; } $cur = $cur->next; } $cur->next = $node->next; return $head; }}$team = new Demo();$node1 = new Demo(1, '唐三藏');Demo::push($team, $node1);$node1->name = '唐僧';Demo::show($team);// Demo::show($team);$node2 = new Demo(2, '孙悟空');Demo::insert($team, $node2);// Demo::show($team);$node3 = new Demo(5, '白龙马');Demo::push($team, $node3);// Demo::show($team);$node4 = new Demo(3, '猪八戒');Demo::insert($team, $node4);// Demo::show($team);$node5 = new Demo(4, '沙和尚');Demo::insert($team, $node5);// Demo::show($team);$node4->name = '猪悟能';//php对象传引用,所以Demo::edit没有必要// unset($node4);// $node4 = new Demo(3, '猪悟能');// Demo::edit($team, $node4);Demo::pop($team, $node1);Demo::show($team);您可能感兴趣的文章:
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号