<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<?php
class Hero {
public $no;
public $name;
public $nickname;
public $next=null;
public function __construct($no=‘‘,$name=‘‘,$nickname=‘‘){
$this->no=$no;
$this->name=$name;
$this->nickname=$nickname;
}
}
function updatehelo($head,$hero){
$cur=$head;
while($cur->next!=null){
if($cur->next->no==$hero->no){
break;
}
$cur=$cur->next;
}
if($cur->next==null){
echo "no";
}
else{
$cur->next->name=$hero->name;
}
}
function delhero($head,$herono){
$cur=$head->next;
while($cur!=null){
if($cur->next->no==$herono){
break;
}
$cur=$cur->next;
}
$cur->next=$cur->next->next;
}
function addhero($head,$hero){
$cur=$head;
//直接加到英雄最后
while($cur->next!=null){
$cur=$cur->next;
}
$cur->next=$hero;
}
function showhero($head){
$cur=$head;
while($cur->next!=null){
echo ‘英雄的编号:‘.$cur->next->no.‘名字:‘.$cur->next->name."<br>";
$cur=$cur->next;
}//while
}
$head=new Hero();
$hero=new Hero(1,‘宋江‘,‘及时狗‘);
$head->next=$hero;
$hero2=new Hero(2,"卢俊义",‘一兄称‘);
//addHero($head,$hero);
$hero->next=$hero2;
$hero3=new Hero(3,"卢俊3",‘111‘);
$hero2->next=$hero3;
delhero($head,3);
//单链表的遍历,是从head开始遍历的
//$head头的值不能亦空,
showHero($head);
?>
本文出自 “php学习” 博客,谢绝转载!
原文:http://xiaochaozi.blog.51cto.com/6469085/1401115