实战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语言特点的深入研究。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一些概念 数据结构就是研究数据的逻辑结构和物理结构以及它们之间相互关系,并对这种结构定义相应的运算,而且确保经过这...
    Winterfell_Z阅读 6,615评论 0 13
  • 1 序 2016年6月25日夜,帝都,天下着大雨,拖着行李箱和同学在校门口照了最后一张合照,搬离寝室打车去了提前租...
    RichardJieChen阅读 5,395评论 0 12
  • 本文内容取自于小甲鱼的数据结构与算法。http://www.jianshu.com/p/230e6fde9c75 ...
    阿阿阿阿毛阅读 3,105评论 0 7
  • 本文内容:1、 什么是链表?2、 链表共分几类?3、 链表的 C 实现! 总表:《数据结构?》 工程代码 Gith...
    半纸渊阅读 40,210评论 0 54
  • 姓名:覃杏留 公司:河池南门时尚商贸有限公司 组别:258期利他二组 【日精进打卡第0067天】 【知~学习】 诵...
    隔世的风阅读 192评论 0 0

友情链接更多精彩内容