登录  /  注册
PHP数据结构基础之双链表
不言
发布:2023-04-02 18:30:02
原创
1048人浏览过

这篇文章主要介绍了关于PHP数据结构基础之双链表,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

什么是双链表?

上一篇实战PHP数据结构基础之单链表说到

单链表由一个一个的作为节点的对象构成的,每一个节点都有指向下一个节点的指针,最后一个节点的指针域指向空。每个节点可以存储任何数据类型。

而双链表每个节点有两个指针域,分别指向前驱和后继节点。单链表是单向的,而双链表是双向的。

常见操作

对双链表我们常见的操作有如下:

  • insert

  • insertBefore

  • insertAfter

  • insertAtFirst

  • insertAtLast

  • deleteFirst

  • deleteLast

  • delete

  • reverse

  • getNthNode

  • ...

PHP语言实现

首先我们根据定义实现一个双链表的ListNode类。

class ListNode
{
    public $data = null;
    public $next = null;
    public $prev = null;

    public function __construct(string $data)
    {
        $this->data = $data;
    }
}
登录后复制

再来看链表类,首先需要3个私有属性,分别是头节点、尾巴节点和长度。

class DoubleLinkedList
{
    private $head = null;
    private $last = null;
    private $length = 0;
}
登录后复制

接下来还是老规矩,直接看如何实现第一个即常用的插入,这是是一个平均时间复杂度为O(n)的操作。

/**
 * 插入一个节点
 * @param string|null $data
 * @return bool
 * complexity O(n)
 */
public function insert(string $data = null)
{
    $newNode = new ListNode($data);
    if ($this->head) {
        $currentNode = $this->head;
        while ($currentNode) {
            if ($currentNode->next === null) {
                $currentNode->next = $newNode;
                $newNode->prev = $currentNode;
                $this->last = $newNode;
                $this->length++;
                return true;
            }

            $currentNode = $currentNode->next;
        }
    } else {
        $this->head = &$newNode;
        $this->last = $newNode;
        $this->length++;
        return true;
    }

}
登录后复制

再来看如何删除节点。

/**
 * 删除一个节点
 * @param string $data
 * @return bool|ListNode
 * complexity O(n)
 */
public function delete(string $query = null)
{
if ($this->head) {
    $currentNode = $this->head;
    $prevNode = null;

    while ($currentNode) {
        if ($currentNode->data === $query) {
            if ($nextNode = $currentNode->next) {
                if ($prevNode) {
                    $prevNode->next = $nextNode;
                    $nextNode->prev = $prevNode;
                } else {
                    $this->head = $nextNode;
                    $nextNode->prev = null;
                }

                unset($currentNode);
            } else {
                if ($prevNode) {
                    $this->last = $prevNode;
                    $prevNode->next = null;
                    unset($currentNode);
                } else {
                    $this->head = null;
                    $this->last = null;
                }
            }

            $this->length--;
            return true;
        }

        $prevNode = $currentNode;
        $currentNode = $currentNode->next;
    }
}

    return false;
}
登录后复制

反转双链表也不是很复杂。

public function reverse()
{
    if ($this->head !== null) {

        if ($this->head->next !== null) {
            $reversedList = null;
            $currentNode = $this->head;

            while ($currentNode !== null) {
                $next = $currentNode->next;
                $currentNode->next = $reversedList;
                $currentNode->prev = $next;

                $reversedList = $currentNode;
                $currentNode = $next;

            }

            $this->last = $this->head;
            $this->head = $reversedList;
        }
    }
}
登录后复制

双链表其他操作的详细实现可以参考 这里。

双链表是链表这种链式存取数据结构中相对于单链表比较特殊的部分,同样属于链表结构的还有单链表,环形链表和多链表。

专题系列

PHP基础数据结构专题系列目录地址:https://github.com/... 主要使用PHP语法总结基础的数据结构和算法。还有我们日常PHP开发中容易忽略的基础知识和现代PHP开发中关于规范、部署、优化的一些实战性建议,同时还有对Javascript语言特点的深入研究。

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

PHP数据结构基础之栈

php7+的php-fpm参数配置的注意事项

以上就是PHP数据结构基础之双链表的详细内容,更多请关注php中文网其它相关文章!

相关标签:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
  • 中小型创业创意公司网站模板
  • 管道维修家政服务登录页模板
  • 儿童公益慈善机构宣传网站模板
网站特效
网站源码
网站素材
前端模板
关于我们免责申明意见反馈讲师合作广告合作技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023//m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学