單鏈表

<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);  

?>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章