首页 > 其他 > 详细

单链表

时间:2014-04-24 10:33:09      阅读:505      评论:0      收藏:0      [点我收藏+]

<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学习” 博客,谢绝转载!

单链表,布布扣,bubuko.com

单链表

原文:http://xiaochaozi.blog.51cto.com/6469085/1401115

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!