日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php数据结构 链表,php数据结构-单链表

發布時間:2024/3/13 php 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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数据结构-单链表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。