php實(shí)現(xiàn)單鏈表的實(shí)例代碼
<?php
//鏈表節(jié)點(diǎn)
class node {
public $id; //節(jié)點(diǎn)id
public $name; //節(jié)點(diǎn)名稱
public $next; //下一節(jié)點(diǎn)
public function __construct($id, $name) {
$this->id = $id;
$this->name = $name;
$this->next = null;
}
}
//單鏈表
class singelLinkList {
private $header; //鏈表頭節(jié)點(diǎn)
//構(gòu)造方法
public function __construct($id = null, $name = null) {
$this->header = new node ( $id, $name, null );
}
//獲取鏈表長度
public function getLinkLength() {
$i = 0;
$current = $this->header;
while ( $current->next != null ) {
$i ++;
$current = $current->next;
}
return $i;
}
//添加節(jié)點(diǎn)數(shù)據(jù)
public function addLink($node) {
$current = $this->header;
while ( $current->next != null ) {
if ($current->next->id > $node->id) {
break;
}
$current = $current->next;
}
$node->next = $current->next;
$current->next = $node;
}
//刪除鏈表節(jié)點(diǎn)
public function delLink($id) {
$current = $this->header;
$flag = false;
while ( $current->next != null ) {
if ($current->next->id == $id) {
$flag = true;
break;
}
$current = $current->next;
}
if ($flag) {
$current->next = $current->next->next;
} else {
echo "未找到id=" . $id . "的節(jié)點(diǎn)!<br>";
}
}
//獲取鏈表
public function getLinkList() {
$current = $this->header;
if ($current->next == null) {
echo ("鏈表為空!");
return;
}
while ( $current->next != null ) {
echo 'id:' . $current->next->id . ' name:' . $current->next->name . "<br>";
if ($current->next->next == null) {
break;
}
$current = $current->next;
}
}
//獲取節(jié)點(diǎn)名字
public function getLinkNameById($id) {
$current = $this->header;
if ($current->next == null) {
echo "鏈表為空!";
return;
}
while ( $current->next != null ) {
if ($current->id == $id) {
break;
}
$current = $current->next;
}
return $current->name;
}
//更新節(jié)點(diǎn)名稱
public function updateLink($id, $name) {
$current = $this->header;
if ($current->next == null) {
echo "鏈表為空!";
return;
}
while ( $current->next != null ) {
if ($current->id == $id) {
break;
}
$current = $current->next;
}
return $current->name = $name;
}
}
$lists = new singelLinkList ();
$lists->addLink ( new node ( 5, 'eeeeee' ) );
$lists->addLink ( new node ( 1, 'aaaaaa' ) );
$lists->addLink ( new node ( 6, 'ffffff' ) );
$lists->addLink ( new node ( 4, 'dddddd' ) );
$lists->addLink ( new node ( 3, 'cccccc' ) );
$lists->addLink ( new node ( 2, 'bbbbbb' ) );
$lists->getLinkList ();
echo "<br>-----------刪除節(jié)點(diǎn)--------------<br>";
$lists->delLink ( 5 );
$lists->getLinkList ();
echo "<br>-----------更新節(jié)點(diǎn)名稱--------------<br>";
$lists->updateLink ( 3, "222222" );
$lists->getLinkList ();
echo "<br>-----------獲取節(jié)點(diǎn)名稱--------------<br>";
echo $lists->getLinkNameById ( 5 );
echo "<br>-----------獲取鏈表長度--------------<br>";
echo $lists->getLinkLength ();
?>
- php數(shù)據(jù)結(jié)構(gòu)之順序鏈表與鏈?zhǔn)骄€性表示例
- php線性表順序存儲實(shí)現(xiàn)代碼(增刪查改)
- php線性表的入棧與出棧實(shí)例分析
- PHP+MySQL統(tǒng)計(jì)該庫中每個表的記錄數(shù)并按遞減順序排列的方法
- PHP小教程之實(shí)現(xiàn)鏈表
- 淺談PHP鏈表數(shù)據(jù)結(jié)構(gòu)(單鏈表)
- PHP小教程之實(shí)現(xiàn)雙向鏈表
- PHP實(shí)現(xiàn)單鏈表翻轉(zhuǎn)操作示例
- PHP鏈表操作簡單示例
- PHP環(huán)形鏈表實(shí)現(xiàn)方法示例
- php實(shí)現(xiàn)的順序線性表示例
相關(guān)文章
ubutu 16.04環(huán)境下,PHP與mysql數(shù)據(jù)庫,網(wǎng)頁登錄驗(yàn)證實(shí)例講解
下面小編就為大家?guī)硪黄猽butu 16.04環(huán)境下,PHP與mysql數(shù)據(jù)庫,網(wǎng)頁登錄驗(yàn)證實(shí)例講解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
laravel配置Redis多個庫的實(shí)現(xiàn)方法
這篇文章主要介紹了laravel配置Redis多個庫的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
CodeIgniter采用config控制的多語言實(shí)現(xiàn)根據(jù)瀏覽器語言自動轉(zhuǎn)換功能
這篇文章主要介紹了CodeIgniter采用config控制的多語言實(shí)現(xiàn)根據(jù)瀏覽器語言自動轉(zhuǎn)換功能,非常實(shí)用,需要的朋友可以參考下2014-07-07
php 利用array_slice函數(shù)獲取隨機(jī)數(shù)組或前幾條數(shù)據(jù)
這篇文章主要介紹了php 利用array_slice函數(shù)獲取隨機(jī)數(shù)組或前幾條數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2015-09-09

