php数据结构 链表,php数据结构-单链表
//單鏈表
class hero{
var $name;
var $sn;
var $names;
var $next;
public function __construct($sn='',$name='',$names='',$next=''){
$this->name=$name;
$this->sn=$sn;
$this->names=$names;
$this->next=$next;
}
}
//頭結點
$head=new hero();
//增加鏈表節點
function addHero($head,$hero){
$cul=$head;
while($cul->next!=null){
if($cul->next->sn>$hero->sn){
$hero->next=$cul->next;
$cul->next=$hero;
$flag='';
break;
}
$cul=$cul->next;
}
if(!isset($flag))
$cul->next=$hero;
}
//遍歷鏈表
function showHero($head){
$cul=$head;
while($cul->next!=null){
echo $cul->next->sn.' '.$cul->next->name.' '.$cul->next->names.'
';
$cul=$cul->next;
}
//echo 'a'.$head->name.'
';
}
//刪除鏈表節點
function delHero($head,$sn){
$cul=$head;
while($cul->next!=null){
if($cul->next->sn==$sn){
$cul->next=$cul->next->next;
$flag='';
break;
}
$cul=$cul->next;
}
if(!isset($flag))echo $sn.'的英雄沒找到
';
}
//修改節點
function editHero($head,$hero){
$cul=$head;
while($cul->next!=null){
if($hero->sn==$cul->next->sn){
$hero->next=$cul->next->next;
$cul->next=$hero;
$flag='';
break;
}
$cul=$cul->next;
}
if(!isset($flag))echo $hero->sn.'的英雄沒找到
';
}
//測試
//增加節點
$hero=new hero(1,'小寶','小寶寶');
addHero($head,$hero);
$hero=new hero(2,'小貝','小貝貝');
addHero($head,$hero);
$hero=new hero(3,'小白','小白白');
addHero($head,$hero);
$hero=new hero('6','香蘭','向蘭蘭');
addHero($head,$hero);
$hero=new hero('4','李峰','李峰分');
addHero($head,$hero);
$hero=new hero('5','劉雪','劉雪雪');
addHero($head,$hero);
$hero=new hero('7','寒梅','韓梅梅');
addHero($head,$hero);
//遍歷節點
showHero($head);
//刪除節點
echo '
刪除后
';
delHero($head,2);
showHero($head);
//修改節點
echo '
修改后
';
$hero=new hero('4','趙峰','趙鳳鳳');
editHero($head,$hero);
showHero($head);
?>
總結
以上是生活随笔為你收集整理的php数据结构 链表,php数据结构-单链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Noip2011 Day1 T1 铺地毯
- 下一篇: rog主板php,功能丰富的主板BIOS