PHP小教程之實(shí)現(xiàn)雙向鏈表
看了很久數(shù)據(jù)結(jié)構(gòu)但是沒(méi)有怎么用過(guò),在網(wǎng)上看到了關(guān)于PHP的數(shù)據(jù)結(jié)構(gòu),學(xué)習(xí)了一下,與大家一起分享一下。上一次分享了《PHP小教程之實(shí)現(xiàn)鏈表》,這次來(lái)補(bǔ)充說(shuō)一下雙向鏈表。
<?php
class Hero
{
public $pre=null;
public $no;
public $name;
public $next=null;
public function __construct($no='',$name='')
{
$this->no=$no;
$this->name=$name;
}
static public function addHero($head,$hero)
{
$cur = $head;
$isExist=false;
//判斷目前這個(gè)鏈表是否為空
if($cur->next==null)
{
$cur->next=$hero;
$hero->pre=$cur;
}
else
{
//如果不是空節(jié)點(diǎn),則安排名來(lái)添加
//找到添加的位置
while($cur->next!=null)
{
if($cur->next->no > $hero->no)
{
break;
}
else if($cur->next->no == $hero->no)
{
$isExist=true;
echo "<br>不能添加相同的編號(hào)";
}
$cur=$cur->next;
}
if(!$isExist)
{
if($cur->next!=null)
{
$hero->next=$cur->next;
}
$hero->pre=$cur;
if($cur->next!=null)
{
$hero->next->pre=$hero;
}
$cur->next=$hero;
}
}
}
//遍歷
static public function showHero($head)
{
$cur=$head;
while($cur->next!=null)
{
echo "<br>編號(hào):".$cur->next->no."名字:".$cur->next->name;
$cur=$cur->next;
}
}
static public function delHero($head,$herono)
{
$cur=$head;
$isFind=false;
while($cur!=null)
{
if($cur->no==$herono)
{
$isFind=true;
break;
}
//繼續(xù)找
$cur=$cur->next;
}
if($isFind)
{
if($cur->next!=null)
{
$cur->next_pre=$cur->pre;
}
$cur->pre->next=$cur->next;
}
else
{
echo "<br>沒(méi)有找到目標(biāo)";
}
}
}
$head = new Hero();
$hero1 = new Hero(1,'1111');
$hero3 = new Hero(3,'3333');
$hero2 = new Hero(2,'2222');
Hero::addHero($head,$hero1);
Hero::addHero($head,$hero3);
Hero::addHero($head,$hero2);
Hero::showHero($head);
Hero::delHero($head,2);
Hero::showHero($head);
?>
- php數(shù)據(jù)結(jié)構(gòu)之順序鏈表與鏈?zhǔn)骄€性表示例
- php線性表順序存儲(chǔ)實(shí)現(xiàn)代碼(增刪查改)
- php線性表的入棧與出棧實(shí)例分析
- PHP+MySQL統(tǒng)計(jì)該庫(kù)中每個(gè)表的記錄數(shù)并按遞減順序排列的方法
- php實(shí)現(xiàn)單鏈表的實(shí)例代碼
- PHP小教程之實(shí)現(xiàn)鏈表
- 淺談PHP鏈表數(shù)據(jù)結(jié)構(gòu)(單鏈表)
- PHP實(shí)現(xiàn)單鏈表翻轉(zhuǎn)操作示例
- PHP鏈表操作簡(jiǎn)單示例
- PHP環(huán)形鏈表實(shí)現(xiàn)方法示例
- php實(shí)現(xiàn)的順序線性表示例
相關(guān)文章
PHP中Session可能會(huì)引起并發(fā)問(wèn)題
Session 中文沒(méi)有一個(gè)統(tǒng)一的譯法,我習(xí)慣上譯為會(huì)話。關(guān)于session的意義大家都應(yīng)該清楚: 其實(shí)是在瀏覽某個(gè)網(wǎng)站時(shí),在瀏覽器沒(méi)有關(guān)閉的情形之下,一個(gè)web應(yīng)用的開(kāi)始和結(jié)束。一個(gè)session可以包括數(shù)次http的請(qǐng)求和應(yīng)答2015-06-06
PHP把小數(shù)轉(zhuǎn)成整數(shù)3種方法
這篇文章主要介紹了PHP把小數(shù)轉(zhuǎn)成整數(shù)3種方法,實(shí)際上是使用的PHP自帶的3個(gè)函數(shù),分別是floor、ceil和round,需要的朋友可以參考下2014-06-06
Codeigniter+PHPExcel實(shí)現(xiàn)導(dǎo)出數(shù)據(jù)到Excel文件
PHPExcel是用來(lái)操作OfficeExcel文檔的一個(gè)PHP類(lèi)庫(kù),Codeigniter是一個(gè)功能強(qiáng)大的PHP框架。二者結(jié)合就能起到非常棒的效果,需要的朋友可以參考下2014-06-06
php使用strtotime和date函數(shù)判斷日期是否有效代碼分享
php使用strtotime和date函數(shù)進(jìn)行檢驗(yàn)判斷日期是否有效代碼分享,大家參考使用吧2013-12-12
PHP使用Redis實(shí)現(xiàn)Session共享的實(shí)現(xiàn)示例
這篇文章主要介紹了PHP使用Redis實(shí)現(xiàn)Session共享的實(shí)現(xiàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05

