php中怎么实现数据结构的单向链表
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,本篇文章给大家分享的是有关php中怎么实现数据结构的单向链表,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。代码实现定义节点class N
千家信息网最后更新 2026年11月03日php中怎么实现数据结构的单向链表
本篇文章给大家分享的是有关php中怎么实现数据结构的单向链表,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
代码实现
定义节点
class Node{ public $data; /** * @var null | Node */ public $next; public function __construct($data) { $this->data = $data; $this->next = null; }}单链表实现
/** * Class SingleLinkList * 单链接的实现示例,实现简单的填加,插入,删除, 查询,长度,遍历这几个简单操作 */class SingleLinkList{ /** * 链表头结点,头节点必须存在, * @var Node */ public $header; private $size = 0; /** * 构造函数,默认填加一个哨兵节点,该节点元素为空 * SingleLinkList constructor. */ public function __construct() { $this->header = new Node(null); } /** * 在链表末尾添加节点 * @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; } /** * 在指定位置插入节点 * @param int $index 节点位置,从1开始计数 * @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; } /** * 删除节点 * @param int $index 节点位置,从1开始计数 * @return int * @throws Exception */ public function deleteNodeByIndex($index) { if ($index < 1 || $index > ($this->size + 1)) { throw new Exception('你删除的节点不存在'); } $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; } /** * 查询节点 * @param int $index 节点位置,从1开始计数 * @return Node|null * @throws Exception */ public function searchNodeByIndex($index) { if ($index < 1 || $index > ($this->size + 1)) { throw new Exception('你查询的节点不存在'); } $current = $this->header; $tempIndex = 1; do { if ($index == $tempIndex++) { return $current->next; } } while ($current->next != null && ($current = $current->next)); } /** * 获取节点长度 * @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();
以上就是php中怎么实现数据结构的单向链表,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。
节点
位置
长度
查询
单向
数据
数据结构
结构
更多
知识
示例
篇文章
实用
代码
元素
函数
哨兵
就是
工作会
文章
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
广东万易互联网科技有限公司惠州分公司
亿人帮互联网科技有限公司
互联网科技的
互联网 科技
科技服务与互联网
互联网科技革命
互联网科技论文
互联网 视角下的科技创新
西藏七维空间互联网科技有限公司
阿里巴巴算科技企业还是互联网
武汉健翔互联网络科技有限公司
消费医药和互联网科技
互联网公司黑科技创始人
翼展科技 首家互联网医院
长沙茂晟互联网科技有限公司
世界互联网领先科技成果获奖
工业互联网山东科技大学
互联网科技新闻
青岛易易互联网科技
网互联网络科技
上海挚品互联网科技有限公司
互联网属科技
镁科技互联网资讯平台
西藏七维空间互联网科技有限公司
互联网金融中的现代科技
知域互联网科技有限公司大骗子
互联网硬科技公司
互联网时代高科技布局
互联网科技创新创业项目
恒生互联网科技趋势