php mysql 链表_php实现数据结构的单向链表
啥是單向鏈表
鏈表是以鏈式存儲數(shù)據(jù)的結(jié)構(gòu),其不需要連續(xù)的存儲空間,鏈表中的數(shù)據(jù)以節(jié)點來表示,每個節(jié)點由元素(存儲數(shù)據(jù))和指針(指向后繼節(jié)點)組成。
單向鏈表(也叫單鏈表)是鏈表中最簡單的一種形式,每個節(jié)點只包含一個元素和一個指針。
它有一個表頭,并且除了最后一個節(jié)點外,所有節(jié)點都有其后繼節(jié)點。
它的存儲結(jié)構(gòu)如下圖所示
代碼實現(xiàn)
定義節(jié)點
class Node
{
public $data;
/**
* @var null | Node
*/
public $next;
public function __construct($data)
{
$this->data = $data;
$this->next = null;
}
}
單鏈表實現(xiàn)
/**
* Class SingleLinkList
* 單鏈接的實現(xiàn)示例,實現(xiàn)簡單的填加,插入,刪除, 查詢,長度,遍歷這幾個簡單操作
*/
class SingleLinkList
{
/**
* 鏈表頭結(jié)點,頭節(jié)點必須存在,
* @var Node
*/
public $header;
private $size = 0;
/**
* 構(gòu)造函數(shù),默認填加一個哨兵節(jié)點,該節(jié)點元素為空
* SingleLinkList constructor.
*/
public function __construct()
{
$this->header = new Node(null);
}
/**
* 在鏈表末尾添加節(jié)點
* @param Node $node
* @return int
*/
public function addNode(Node $node)
{
$current = $this->header;
while ($current->next != null) {
$current = $current->next;
}
$current->next = $node;
return ++$this->size;
}
/**
* 在指定位置插入節(jié)點
* @param int $index 節(jié)點位置,從1開始計數(shù)
* @param Node $node
* @return int
* @throws Exception
*/
public function insertNodeByIndex($index, Node $node)
{
if ($index < 1 || $index > ($this->size + 1)) {
throw new Exception(sprintf('你要插入的位置,超過了鏈表的長度 %d', $this->size));
}
$current = $this->header;
$tempIndex = 1;
do {
if ($index == $tempIndex++) {
$node->next = $current->next;
$current->next = $node;
break;
}
} while ($current->next != null && ($current = $current->next));
return ++$this->size;
}
/**
* 刪除節(jié)點
* @param int $index 節(jié)點位置,從1開始計數(shù)
* @return int
* @throws Exception
*/
public function deleteNodeByIndex($index)
{
if ($index < 1 || $index > ($this->size + 1)) {
throw new Exception('你刪除的節(jié)點不存在');
}
$current = $this->header;
$tempIndex = 1;
do {
if ($index == $tempIndex++) {
$current->next = $current->next->next;
break;
}
} while ($current->next != null && ($current = $current->next));
return --$this->size;
}
/**
* 查詢節(jié)點
* @param int $index 節(jié)點位置,從1開始計數(shù)
* @return Node|null
* @throws Exception
*/
public function searchNodeByIndex($index) {
if ($index < 1 || $index > ($this->size + 1)) {
throw new Exception('你查詢的節(jié)點不存在');
}
$current = $this->header;
$tempIndex = 1;
do {
if ($index == $tempIndex++) {
return $current->next;
}
} while ($current->next != null && ($current = $current->next));
}
/**
* 獲取節(jié)點長度
* @return int
*/
public function getLength()
{
return $this->size;
}
/**
* 遍歷列表
*/
public function showNode()
{
$current = $this->header;
$index = 1;
while ($current->next != null) {
$current = $current->next;
echo 'index --- ' . $index++ . ' --- ';
echo var_export($current->data);
echo PHP_EOL;
}
}
}
示例
$link = new SingleLinkList();
$link->addNode(new Node(1));
$link->addNode(new Node(2));
$link->insertNodeByIndex(3, new Node(3));
$link->addNode(new Node(4));
$link->addNode(new Node(5));
echo $link->getLength(), PHP_EOL;
$link->showNode();
echo '-----------', PHP_EOL;
var_dump($link->searchNodeByIndex(3));
echo '-----------', PHP_EOL;
$link->deleteNodeByIndex(3);
$link->showNode();
以上內(nèi)容希望幫助到大家,很多PHPer在進階的時候總會遇到一些問題和瓶頸,業(yè)務(wù)代碼寫多了沒有方向感,不知道該從那里入手去提升,對此我整理了一些資料,包括但不限于:分布式架構(gòu)、高可擴展、高性能、高并發(fā)、服務(wù)器性能調(diào)優(yōu)、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql優(yōu)化、shell腳本、Docker、微服務(wù)、Nginx等多個知識點高級進階干貨需要的可以免費分享給大家,需要戳這里
總結(jié)
以上是生活随笔為你收集整理的php mysql 链表_php实现数据结构的单向链表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 实时 网口 速率_linux
- 下一篇: java 倒计时_java倒计时器