<html>
<head>
<meta http-equiv='content-type' content='text/html;charset=utf-8'>
<title>/高性能的计算器/</title>
</head>
<body>
<?php
class Hero{
public $pre=null;//指向前一个节点的引用
public $no;
public $name;
public $nickname;
public $next=null;
public function __construct($no='',$name='',$nickname=''){
$this->no=$no;
$this->name=$name;
$this->nickname=$nickname;
}
public static function addHero($head,$hero){
$cur=$head;//一个辅助点,不能动头
$isExist=false;
if($cur->next==null){
$cur->next=$hero;
$hero->pre=$cur;
}else{
///如果不是空节点,则按排名来添加
while($cur->next!=null){
if($cur->next->no>$hero->no){
//说明$hero要加入在$cur的后面
}else if($cur->next->no==$hero->no){
$isExist=true;
echo '<br>不能有相同编号';
}
$cur=$cur->next;
}//while
// 退出while时,我们只要把$hero添加到$cure后面即可
if(!$isExist){
//比如加的就是末
if($cur->next !=null){//处理空表
$hero->next=$cur->next;
}
$hero->pre=$cur;
if($cur->next !=null){//处理空表
$cur->next->pre=$hero;
}
$cur->next=$hero;
}
}//if
}//addHero
public static function showHero($head){
$cur=$head;
while($cur->next !=null){
echo '<br>编号:'.$cur->no.'名字:'.$cur->name;
$cur=$cur->next;
}
//当退出while
echo '<br>编号是:'.$cur->no.'名字:'.$cur->name;
}//showHero
public static function delHero($head,$herono){
//不用辅助结点
$cur=$head->next;
if($cur==null){
echo '一个空表,删除了没意义';
}
$isFind=false;
while ($cur !=null) {
if($cur->no == $herono){
//找到了
$isFind=true;
break;
}//if
$cur=$cur->next;//向下找
}//while
if($isFind){
//找到了可以删除
//自我删除,不使用辅助结点
if($cur->next !=null){
$cur->next->pre=$cur->pre;
}
$cur->pre->next=$cur->next;
}else{
echo "找不到了,你输入的不对,没法子删除啊.";
}//if
}//delHero
}//class
$head=new Hero();
$hero=new Hero(1,'宋江','及时狗');
Hero::addHero($head,$hero);
$hero=new Hero(2,'吴用','一个老师');
Hero::addHero($head,$hero);
$hero=new Hero(3,'林冲','教头');
Hero::addHero($head,$hero);
Hero::showHero($head);
//试试删除
Hero::delHero($head,2);
//
echo '<br>'."删除的后的".'<br>';
Hero::showHero($head);
echo '<br><br>'.'其好处是自好删除,以后学习二叉树及其他,打下良好的基础';
?>
</body>
</html>
分享到:
相关推荐
这是我从B站上看韩老师讲的数据结构与算法后整理的笔记 代码经过运行,欢迎批评指正 有些地方我感觉还是挺难的 大都经过我自己的语言进行描述,韩老师中期的表达可能或多或少也影响可阅读性,望先生们见谅
韩顺平轻松搞定HTML+css+js中HTML部分经常使用的帮助文档,清华大学出品!好东西!!
清华大学图书馆主页 html语言教程(html版) 韩顺平老师视频教程同款
数据结构和算法中韩顺平老师讲的是最好的值得大家学习交流 大数据技术之Scala数据结构和算法
这是一个 呢来就能用的SQL数据表 包括建表
韩顺平jsp九讲笔记
韩顺平 玩转oralce第3讲 oracle的基本使用 韩顺平 玩转oralce第3讲 oracle的基本使用
韩顺平PHP149讲完整笔记之整理版本http协议部分
韩顺平PHP149讲笔记之基本语法2完整版本随堂版本
韩顺平servlet 第4、5讲 事例代码韩顺平servlet 第4、5讲 事例代码韩顺平servlet 第4、5讲 事例代码
韩顺平讲linux笔记
韩顺平 servlet 第二讲下 事例 韩顺平 servlet 第二讲下 事
韩顺平 数据库表 就是韩顺平老师讲的SQL 数据库视频里面的其中一个表
韩顺平PHP149讲之htm完整笔记之html部分
韩顺平讲的SQL作业评价答案,我看视频一句一句敲的,上面有我自己的点评
韩顺平计算器
韩顺平 玩转oracle第02讲 oralce安装.wmv 韩顺平 玩转oracle第02讲 oralce安装.wmv 韩顺平 玩转oracle第02讲 oralce安装.wmv 韩顺平 玩转oracle第02讲 oralce安装.wmv
传智播客 韩顺平java基础是视频教程 地址