<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>PHP操作mysql多功能类</title>
<body>
<?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 delHero($head,$herono){
$cur=$head;
while ($cur->next !=null) {
if($cur->next->no ==$herono){
//找到了退出吧
break;
}//if
$cur=$cur->next;
}//while
if($cur->next !=null){
//删除了
$cur->next=$cur->next->next;
}
}
}//Hero
//修改英雄
function updateHero($head,$hero){
//跑龙套的$cur
$cur=$head;
while($cur->next !=null){
if($cur->next->no==$hero->no){
break;
}//if
$cur=$cur->next;
}
//当退出while时,$cur->next==null为空,再到了队尾了
if($cur->next==null){
echo "no";
}else{
$cur->next->name=$hero->name;
}
}//updateHero
function addHero($head,$hero){
//1.直接加在链表最后加
//2.安英雄的排行
$cur=$head;
/*while ( $cu->next !=null) {
$cur=$cur->next;
}//while
$cur->next=$hero;*/
while($cur->next !=null){
if($cur->next->no >=$hero->no){
break;
}
$cur=$cur->next;
}
$hero->next=$cur->next;
$cur->next=$hero;
}//addHero
function showHeros($head){
//遍历[必须知道什么时候到了最后]
//为了不去改变$head的指向,我们可以使用一个临时变量$cur
$cur=$head;
while($cur->next !=NULL){
echo '英雄的编号:'.$cur->next->no.'名字:'.$cur->next->name;
//让cur移动,否则是死了
$cur=$cur->next;
}//while
}//showHeros
$head=new Hero();
$hero=new Hero(1,'宋江','及时狗');
//addHero($head,$hero);
$head->next=$hero;
$hero2=new Hero(2,"卢俊义",'一兄称');
//addHero($head,$hero);
$hero->next=$hero2;
//单链表的遍历,是从head开始遍历的
//$head头的值不能亦空,
showHeros($head);
?>
</body>
</html>