您当前的位置: 首页 > 学无止境 > 网站建设 网站首页网站建设
单链表curd操作
发布时间:2017-03-26 22:25:16编辑:雪饮阅读()
单链表的形成:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<a href="">查询英雄</a>
<a href="">添加英雄</a>
<a href="">删除英雄</a>
<a href="">修改英雄</a>
<?php
class Hero{
public $no;//排名
public $name;//真实名字
public $nickname;//外号
public $next=null;//是一个引用,指向另外一个Hero的对象实例
public function __construct($no="",$name="",$nickname=""){
$this->no=$no;
$this->name=$name;
$this->nickname=$nickname;
}
}
//创建一个head头,该head只是一个头,不放入数据
$head=new Hero();
//创建一个英雄
$hero=new Hero(1,'宋江','及时雨');
//连接
$head->next=$hero;
$hero2=new Hero(2,'卢俊义','玉麒麟');
$hero->next=$hero2;
$hero->next=$hero2;
//head头的值不能变,变化后就不能遍历我们的单链表
function showHeros($head){
$cur=$head;
while($cur->next!=null){
echo "<br/>英雄的编号是:".$cur->next->no." 名字:".$cur->next->name." 外号:".$cur->next->nickname;
$cur=$cur->next;
}
}
echo '<br/>****当前的英雄排行情况是*****';
showHeros($head);
?>
</body>
</html>
单链接节点的动态添加与排序动态添加:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<a href="">查询英雄</a>
<a href="">添加英雄</a>
<a href="">删除英雄</a>
<a href="">修改英雄</a>
<?php
class Hero{
public $no;//排名
public $name;//真实名字
public $nickname;//外号
public $next=null;//是一个引用,指向另外一个Hero的对象实例
public function __construct($no="",$name="",$nickname=""){
$this->no=$no;
$this->name=$name;
$this->nickname=$nickname;
}
}
//创建一个head头,该head只是一个头,不放入数据
$head=new Hero();
//创建一个英雄
function addHero($head,$hero){
//直接在链表最后加。
$cur=$head;
while($cur->next!=null){
$cur=$cur->next;
}
$cur->next=$hero;
}
function addHero2($head,$hero){
//按指定键的键值大小排序添加。
$cur=$head;
while($cur->next!=null){
if($cur->next->no>=$hero->no){
//找到待添加数据所要插入的位置
break;
}
$cur=$cur->next;
}
//经过while后找到插入点,进行插入
$hero->next=$cur->next;
$cur->next=$hero;
/*
举例:当1、2、6都添加到了head中,此时若添加3
当从head开始循环经过1、2即将到6时因为6>=3而不继续向下循环,而是跳出,那么此时位置在2上,
那么此时3(待添加)的next为6,即$hero->next=$cur->next;(3与6结合了)
同时2的next为3(此时3与6已经结合了),即$cur->next=$hero;
相当于调换了位置
*/
}
$hero=new Hero(1,'宋江','及时雨');
addHero2($head,$hero);
$hero=new Hero(2,'宋江2','及时雨2');
addHero2($head,$hero);
$hero=new Hero(6,'宋江6','及时雨6');
addHero2($head,$hero);
$hero=new Hero(3,'宋江3','及时雨3');
addHero2($head,$hero);
//head头的值不能变,变化后就不能遍历我们的单链表
function showHeros($head){
$cur=$head;
while($cur->next!=null){
echo "<br/>英雄的编号是:".$cur->next->no." 名字:".$cur->next->name." 外号:".$cur->next->nickname;
$cur=$cur->next;
}
}
echo '<br/>****当前的英雄排行情况是*****';
showHeros($head);
?>
</body>
</html>
单链表节点的删除:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<a href="">查询英雄</a>
<a href="">添加英雄</a>
<a href="">删除英雄</a>
<a href="">修改英雄</a>
<?php
class Hero{
public $no;//排名
public $name;//真实名字
public $nickname;//外号
public $next=null;//是一个引用,指向另外一个Hero的对象实例
public function __construct($no="",$name="",$nickname=""){
$this->no=$no;
$this->name=$name;
$this->nickname=$nickname;
}
}
//创建一个head头,该head只是一个头,不放入数据
$head=new Hero();
//创建一个英雄
function addHero($head,$hero){
//直接在链表最后加。
$cur=$head;
while($cur->next!=null){
$cur=$cur->next;
}
$cur->next=$hero;
}
function addHero2($head,$hero){
//按指定键的键值大小排序添加。
$cur=$head;
while($cur->next!=null){
if($cur->next->no>=$hero->no){
//找到待添加数据所要插入的位置
break;
}
$cur=$cur->next;
}
//经过while后找到插入点,进行插入
$hero->next=$cur->next;
$cur->next=$hero;
/*
举例:当1、2、6都添加到了head中,此时若添加3
当从head开始循环经过1、2即将到6时因为6>=3而不继续向下循环,而是跳出,那么此时位置在2上,
那么此时3(待添加)的next为6,即$hero->next=$cur->next;(3与6结合了)
同时2的next为3(此时3与6已经结合了),即$cur->next=$hero;
相当于调换了位置
*/
}
function delHero($head,$herono){
$cur=$head;
$flag=false;//初始化为没有找到英雄
while($cur->next!=null){
if($cur->next->no==$herono){
$flag=true;//找到英雄
break;
}
$cur=$cur->next;
}
if($flag){
$cur->next=$cur->next->next;
}
else{
echo "没有找到要删除的英雄";
}
/*举例:
我要删除编号为2的英雄,当我找到某个节点的next的no为2时就跳出了,此时$cur就是1,也就是2的上一节点
由于要删除2这个节点,所以1的下一节点应该就是“1的下一节点的下一节点”即$cur->next=$cur->next->next;
*/
}
$hero=new Hero(1,'宋江','及时雨');
addHero2($head,$hero);
$hero=new Hero(2,'宋江2','及时雨2');
addHero2($head,$hero);
$hero=new Hero(6,'宋江6','及时雨6');
addHero2($head,$hero);
$hero=new Hero(3,'宋江3','及时雨3');
addHero2($head,$hero);
//head头的值不能变,变化后就不能遍历我们的单链表
function showHeros($head){
$cur=$head;
while($cur->next!=null){
echo "<br/>英雄的编号是:".$cur->next->no." 名字:".$cur->next->name." 外号:".$cur->next->nickname;
$cur=$cur->next;
}
}
echo '<br/>****当前的英雄排行情况是*****';
showHeros($head);
delHero($head,1);
echo "<br/>删除英雄后";
showHeros($head);
?>
</body>
</html>
单链表节点的修改:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<a href="">查询英雄</a>
<a href="">添加英雄</a>
<a href="">删除英雄</a>
<a href="">修改英雄</a>
<?php
class Hero{
public $no;//排名
public $name;//真实名字
public $nickname;//外号
public $next=null;//是一个引用,指向另外一个Hero的对象实例
public function __construct($no="",$name="",$nickname=""){
$this->no=$no;
$this->name=$name;
$this->nickname=$nickname;
}
}
//创建一个head头,该head只是一个头,不放入数据
$head=new Hero();
//创建一个英雄
function addHero($head,$hero){
//直接在链表最后加。
$cur=$head;
while($cur->next!=null){
$cur=$cur->next;
}
$cur->next=$hero;
}
function addHero2($head,$hero){
//按指定键的键值大小排序添加。
$cur=$head;
while($cur->next!=null){
if($cur->next->no>=$hero->no){
//找到待添加数据所要插入的位置
break;
}
$cur=$cur->next;
}
//经过while后找到插入点,进行插入
$hero->next=$cur->next;
$cur->next=$hero;
/*
举例:当1、2、6都添加到了head中,此时若添加3
当从head开始循环经过1、2即将到6时因为6>=3而不继续向下循环,而是跳出,那么此时位置在2上,
那么此时3(待添加)的next为6,即$hero->next=$cur->next;(3与6结合了)
同时2的next为3(此时3与6已经结合了),即$cur->next=$hero;
相当于调换了位置
*/
}
function delHero($head,$herono){
$cur=$head;
$flag=false;//初始化为没有找到英雄
while($cur->next!=null){
if($cur->next->no==$herono){
$flag=true;//找到英雄
break;
}
$cur=$cur->next;
}
if($flag){
$cur->next=$cur->next->next;
}
else{
echo "没有找到要删除的英雄";
}
/*举例:
我要删除编号为2的英雄,当我找到某个节点的next的no为2时就跳出了,此时$cur就是1,也就是2的上一节点
由于要删除2这个节点,所以1的下一节点应该就是“1的下一节点的下一节点”即$cur->next=$cur->next->next;
*/
}
function updateHero($head,$hero){
$cur=$head;
while($cur->next!=null){
if($cur->next->no==$hero->no){
break;
}
$cur=$cur->next;
}
if($cur->next==null){
//next为null只有末尾才会出现,所以若为null就说明你要修改的编号在从头至尾都没有找到
echo "你要修改的英雄不存在";
}
else{
//编号不能改
$cur->next->name=$hero->name;
$cur->next->nickname=$hero->nickname;
}
}
$hero=new Hero(1,'宋江','及时雨');
addHero2($head,$hero);
$hero=new Hero(2,'宋江2','及时雨2');
addHero2($head,$hero);
$hero=new Hero(6,'宋江6','及时雨6');
addHero2($head,$hero);
$hero=new Hero(3,'宋江3','及时雨3');
addHero2($head,$hero);
//head头的值不能变,变化后就不能遍历我们的单链表
function showHeros($head){
$cur=$head;
while($cur->next!=null){
echo "<br/>英雄的编号是:".$cur->next->no." 名字:".$cur->next->name." 外号:".$cur->next->nickname;
$cur=$cur->next;
}
}
echo '<br/>****当前的英雄排行情况是****';
showHeros($head);
delHero($head,1);
echo "<br/>****删除英雄后**************";
showHeros($head);
$hero=new Hero(3,'雪饮','杜敏捷');
updateHero($head,$hero);
echo "<br/>****修改英雄后**************";
showHeros($head);
?>
</body>
</html>
关键字词:
上一篇:单链表在内存中存在形式剖析
下一篇:堆栈在内存中存在形式剖析
相关文章
-
无相关信息