这次给大家带来PHP双向链表使用详解,PHP双向链表使用的注意事项有哪些,下面就是实战案例,一起来看一下。
由于需要对一组数据多次进行移动操作,所以写个双向链表。但对php实在不熟悉,虽然测试各个方法没啥问题,就是不知道php语言深层的这些指针和unset有什么注意的地方,贴出来让大家教育吧。效率没测试....求谅解~
<?php /** * **双向链表 * @author zhiyuan12@ */ /** * 链表元素结点类 */ class Node_Element { public $pre = NULL; // 前驱 public $next = NULL; // 后继 public $key = NULL; // 元素键值 public $data = NULL; // 结点值 function Construct($key, $data) { $this->key = $key; $this->data = $data; } } /** * 双向链表类 */ class DoubleLinkedList { private $head; // 头指针 private $tail; // 尾指针 private $current; // 当前指针 private $len; // 链表长度 function Construct() { $this->head = self::_getNode ( null, null ); $this->curelement = $this->head; $this->tail = $this->head; $len = 0; } /** * @ desc: 读取链表全部结点 */ public function readAll() { $tmp = $this->head; while ( $tmp->next !== null ) { $tmp = $tmp->next; var_dump ( $tmp->key, $tmp->data ); } } public function move($pos1, $pos2) { $pos1Node = $this->findPosition ( $pos1 ); $pos2Node = $this->findPosition ( $pos2 ); if ($pos1Node !== null && $pos2Node !== null) { $tmpKey = $pos1Node->key; $tmpData = $pos1Node->data; $pos1Node->key = $pos2Node->key; $pos1Node->data = $pos2Node->data; $pos2Node->key = $tmpKey; $pos2Node->data = $tmpData; return true; } return false; } /** * @ desc: 在指定关键词删除结点 * * @param : $key * 指定位置的链表元素key */ public function delete($key) { $pos = $this->find ( $key ); if ($pos !== null) { $tmp = $pos; $last = null; $first = true; while ( $tmp->next !== null && $tmp->next->key === $key ) { $tmp = $tmp->next; if (! $first) { $this->delNode ( $last ); } else { $first = false; } $last = $tmp; } if ($tmp->next !== null) { $pos->pre->next = $tmp->next; $tmp->next->pre = $pos->pre; } else { $pos->pre->next = null; } $this->delNode ( $pos ); $this->delNode ( $tmp ); } } /** * @ desc: 在指定位置删除结点 * * @param : $key * 指定位置的链表元素key */ public function deletePosition($pos) { $tmp = $this->findPosition ( $pos ); if ($tmp === null) { return true; } if ($tmp === $this->getTail ()) { $tmp->pre->next = null; $this->delNode ( $tmp ); return true; } $tmp->pre->next = $tmp->next; $tmp->next->pre = $tmp->pre; $this->delNode ( $tmp ); } /** * @ desc: 在指定键值之前插入结点 * * @param : $key * //指定位置的链表元素key * @param : $data * //要插入的链表元素数据 * @param : $flag * //是否顺序查找位置进行插入 */ public function insert($key, $data, $flag = true) { $newNode = self::_getNode ( $key, $data ); $tmp = $this->find ( $key, $flag ); if ($tmp !== null) { $newNode->pre = $tmp->pre; $newNode->next = $tmp; $tmp->pre = $newNode; $newNode->pre->next = $newNode; } else { $newNode->pre = $this->tail; $this->tail->next = $newNode; $this->tail = $newNode; } $this->len ++; } /** * @ desc: 在指定位置之前插入结点 * * @param : $pos * 指定插入链表的位置 * @param : $key * 指定位置的链表元素key * @param : $data * 要插入的链表元素数据 */ public function insertPosition($pos, $key, $data) { $newNode = self::_getNode ( $key, $data ); $tmp = $this->findPosition ( $pos ); if ($tmp !== null) { $newNode->pre = $tmp->pre; $newNode->next = $tmp; $tmp->pre = $newNode; $newNode->pre->next = $newNode; } else { $newNode->pre = $this->tail; $this->tail->next = $newNode; $this->tail = $newNode; } $this->len ++; return true; } /** * @ desc: 根据key值查询指定位置数据 * * @param : $key * //指定位置的链表元素key * @param : $flag * //是否顺序查找 */ public function find($key, $flag = true) { if ($flag) { $tmp = $this->head; while ( $tmp->next !== null ) { $tmp = $tmp->next; if ($tmp->key === $key) { return $tmp; } } } else { $tmp = $this->getTail (); while ( $tmp->pre !== null ) { if ($tmp->key === $key) { return $tmp; } $tmp = $tmp->pre; } } return null; } /** * @ desc: 根据位置查询指定位置数据 * * @param : $pos * //指定位置的链表元素key */ public function findPosition($pos) { if ($pos <= 0 || $pos > $this->len) return null; if ($pos < ($this->len / 2 + 1)) { $tmp = $this->head; $count = 0; while ( $tmp->next !== null ) { $tmp = $tmp->next; $count ++; if ($count === $pos) { return $tmp; } } } else { $tmp = $this->tail; $pos = $this->len - $pos + 1; $count = 1; while ( $tmp->pre !== null ) { if ($count === $pos) { return $tmp; } $tmp = $tmp->pre; $count ++; } } return null; } /** * @ desc: 返回链表头节点 */ public function getHead() { return $this->head->next; } /** * @ desc: 返回链表尾节点 */ public function getTail() { return $this->tail; } /** * @ desc: 查询链表节点个数 */ public function getLength() { return $this->len; } private static function _getNode($key, $data) { $newNode = new Node_Element ( $key, $data ); if ($newNode === null) { echo "new node fail!"; } return $newNode; } private function delNode($node) { unset ( $node ); $this->len --; } } $myList = new DoubleLinkedList (); $myList->insert ( 1, "test1" ); $myList->insert ( 2, "test2" ); $myList->insert ( "2b", "test2-b" ); $myList->insert ( 2, "test2-c" ); $myList->insert ( 3, "test3" ); $myList->insertPosition ( 5, "t", "testt" ); $myList->readAll (); echo "+++"; $myList->deletePosition(0); $myList->readAll (); echo "..." . $myList->getLength (); var_dump ( $myList->findPosition ( 3 )->data ); ?>
运行结果:
int(1) string(5) "test1" int(2) string(7) "test2-c" int(2) string(5) "test2" string(2) "2b" string(7) "test2-b" string(1) "t" string(5) "testt" int(3) string(5) "test3" +++int(1) string(5) "test1" int(2) string(7) "test2-c" int(2) string(5) "test2" string(2) "2b" string(7) "test2-b" string(1) "t" string(5) "testt" int(3) string(5) "test3" ...6string(5) "test2"
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
PHP删除数组中指定下标元素步骤详解
以上是PHP双向链表使用详解的详细内容。更多信息请关注PHP中文网其他相关文章!

PHP在现代Web开发中仍然重要,尤其在内容管理和电子商务平台。1)PHP拥有丰富的生态系统和强大框架支持,如Laravel和Symfony。2)性能优化可通过OPcache和Nginx实现。3)PHP8.0引入JIT编译器,提升性能。4)云原生应用通过Docker和Kubernetes部署,提高灵活性和可扩展性。

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。

PHP在现代编程中仍然是一个强大且广泛使用的工具,尤其在web开发领域。1)PHP易用且与数据库集成无缝,是许多开发者的首选。2)它支持动态内容生成和面向对象编程,适合快速创建和维护网站。3)PHP的性能可以通过缓存和优化数据库查询来提升,其广泛的社区和丰富生态系统使其在当今技术栈中仍具重要地位。

在PHP中,弱引用是通过WeakReference类实现的,不会阻止垃圾回收器回收对象。弱引用适用于缓存系统和事件监听器等场景,需注意其不能保证对象存活,且垃圾回收可能延迟。

\_\_invoke方法允许对象像函数一样被调用。1.定义\_\_invoke方法使对象可被调用。2.使用$obj(...)语法时,PHP会执行\_\_invoke方法。3.适用于日志记录和计算器等场景,提高代码灵活性和可读性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

禅工作室 13.0.1
功能强大的PHP集成开发环境