1。一搬来讲,基本数据类型,放在栈区,比如变量;符合数据类型放在堆区,比如对象
==如下为变量$hero 指向堆区的对象
<html>
<head>
<meta http-equiv="content-type" content='text/html;charset=utf-8'/>
</head>
<h1>单向链表完成英雄排行管理</h1>
<hr/>
<a href='#'>查询英雄</a>|
<a href='#'>添加英雄</a>|
<a href='#'>删除英雄</a>|
<a href='#'>修改英雄</a>
<?php
/*
* 单链表学习
* 定义英雄类
*/
class Hero{
public $rank;//人物排名
public $name;//人物姓名
public $nikename;//绰号
public $next=NUll;//指向下一个Hero对象的引用
public function __construct($rank='',$name='',$nikename='') {
$this -> rank = $rank;
$this -> name = $name;
$this -> nikename = $nikename;
}
}
//创建一个head头,该head 只是一个头,不放入数据
//创建一个英雄
/*
$head = new Hero;
$hero = new Hero('1', '宋江','及时雨');
$head -> next = $hero;
$hero2= new Hero('2','卢俊义','玉麒麟');
$hero ->next = $hero2;*/
//写一个函数,专门用于添加英雄.
function addHero($head,$hero){
//1.直接在链表最后加.
//找到链表最后,不能动$head;
$cur = $head;
/*while($cur->next != NULL){
$cur = $cur ->next;
}
//当退出 while循环时,$cur就是链表最后
$cur ->next = $hero;*/
//2.按照英雄的排行加入.(这里我希望能够保证链表的顺序)
$flag = FALSE;
while($cur->next !=NULL){
if($cur->next->rank > $hero->rank){
//找到位置
break;
}else if($cur->next->rank == $hero ->rank){
$flag=true;
echo '<br/>不能抢位置,'.$hero->rank.'位置已经有人了';
}
//继续
$cur = $cur -> next;
}
// 当退出while时候,位置找到.
//加入
//让hero加入
if($flag === FALSE){
$hero ->next = $cur ->next;
$cur -> next = $hero;
}
}
//单链表的遍历怎么做,是从head开始遍历的,
//$head头的值不能变,变化后就不能遍历我们的单链表
function showHeros($head){
//遍历[必须要知道什么时候,到了链表的最后.]
//这里为了不去改变 $head的指向,我们可以使用一个临时的遍历
$cur = $head;
while($cur -> next !=NULL){
echo '<br/>英雄的编号是'.$cur->next->rank.' 名字='.$cur->next->name.' 外号='.$cur->next->nikename;
//让$cur移动
$cur=$cur->next;
}
}
//从链表中删除某个英雄
function delHero($head,$herorank){
//找到这个英雄在哪里
$cur = $head;//临时变量
$flag = FALSE;//假设没有找到
while ($cur -> next != NULL){
if($cur->next->rank ==$herorank){
$flag = ture;
// 找到 $cur的下一个节点就是应该被删除的节点.
break;
}
$cur= $cur ->next;
}
if($flag){
//删除
$cur->next = $cur->next->next;
}else{
echo '<br/>没有你要删除的英雄的编号'.$herorank;
}
}
//修改英雄编号
function updateHero($head,$hero){
//还是先找到这个英雄
$cur=$head;//$cur就是跑龙套.
while ($cur -> next !=NULL){
if($cur->next->rank == $hero->rank){
break;//找到了
}
//继续下走
$cur = $cur->next;
}
//当退出while 后,如果$cur->next==null 说明不存在
if($cur ->next ==NULL){
echo '<br/>你要修改的'.$hero->no.'不存在';
}else{
//编号不能改
$cur->next->name = $hero->name;
$cur->next->nikename = $hero->nikename;
}
}
//添加
//创建一个head头,该head 只是一个头,不放入数据
$head = new Hero;
$hero=new Hero(1,'宋江','及时雨');
addHero($head,$hero);
$hero=new Hero(2,'卢俊义','玉麒麟');
addHero($head,$hero);
$hero=new Hero(7,'秦明','霹雳火');
addHero($head,$hero);
$hero=new Hero(6,'林冲','豹子头');
addHero($head,$hero);
$hero=new Hero(3,'吴用','智多星');
addHero($head,$hero);
$hero=new Hero(3,'吴用2','智多星2');
addHero($head,$hero);
echo '<br/>************当前的英雄排行情况是*******';
showHeros($head);
echo '<br/>************删除后额英雄排行情况是*******';
//delHero($head,1);
delHero($head,21);
showHeros($head);
echo '<br/>************修改后英雄排行情况是*******';
$hero=new Hero(1,'xxx','左青龙,右白虎');
updateHero($head,$hero);
showHeros($head);
?>
</html>