当前位置: 首页 > 后端技术 > PHP

php数据结构-单向链表

时间:2023-03-29 16:23:09 PHP

name=$name;$this->sn=$sn;$this->names=$names;$这个->下一个=$下一个;}}//头节点$head=newhero();//添加链表节点函数addHero($head,$hero){$cul=$head;while($cul->next!=null){如果($cul->next->sn>$hero->sn){$hero->next=$cul->next;$cul->下一个=$英雄;$标志='';休息;$cul=$cul->下一个;}if(!isset($flag))$cul->next=$hero;}//遍历链表函数showHero($head){$cul=$head;while($cul->next!=null){echo$cul->next->sn.' '.$cul->next->name.' '.$cul->next->names.'
';$cul=$cul->下一个;}//echo'a'.$head->name.'
';}//删除链表节点函数delHero($head,$sn){$cul=$head;while($cul->next!=null){if($cul->next->sn==$sn){$cul->next=$cul->next->next;$标志='';休息;$cul=$cul->下一个;}if(!isset($flag))echo$sn.'找不到英雄
';}//修改节点函数editHero($head,$hero){$cul=$head;while($cul->next!=null){if($hero->sn==$cul->next->sn){$hero->next=$cul->next->next;$cul->下一个=$英雄;$标志='';休息;$cul=$cul->下一个;}if(!isset($flag))echo$hero->sn.'找不到英雄
';}//测试//添加节点$hero=newhero(1,'小宝','小宝宝');添加英雄($头,$英雄);$hero=newhero(2,'小贝','小贝贝');添加英雄($头,$英雄);$hero=newhero(3,'小白','小白白');添加英雄($头,$英雄);$hero=newhero('6','胖兰','香兰兰');addHero($head,$hero);$hero=newhero('4','李峰','李峰芬');添加英雄($头,$英雄);$hero=newhero('5','刘雪','刘雪雪');添加英雄($头,$英雄);$hero=newhero('7','韩梅','韩梅梅');添加英雄($头,$英雄);//遍历节点showHero($head);//删除节点echo'

afterdeletion


';delHero($head,2);显示英雄($头);//修改节点echo'

修改后
';$hero=newhero('4','赵峰','赵峰峰');editHero($head,$hero);showHero($head);?>