PHP数据结构与算法:双向链表

一、概念

1.定义

双向链表也叫双面链表。每个节点有两个链接:一个指向前一个节点,当此节点为第一个节点时,指向空值;而另一个指向下一个节点,当此节点为最后一个节点时,指向空值。

2.概述

3.指定位置插入节点

4.删除节点

二、PHP实现

<?php

/**
 * 节点实现
 */
class Node
{
    /**
     * 数据元素
     * @var
     */
    public $item;

    /**
     * 前驱节点
     * @var
     */
    public $prev;

    /**
     * 后继节点
     * @var
     */
    public $next;

    /**
     * Node constructor.
     * @param $item
     */
    public function __construct($item)
    {
        $this->item = $item;
        $this->prev = null;
        $this->next = null;
    }
}

/**
 * 双向链表基本操作
 *
 * 1.isEmpty()    链表是否为空
 * 2.length()      链表长度
 * 3.travel()      遍历整个链表
 * 4.add(item)     链表头部添加元素
 * 5.append(item)  链表尾部添加元素
 * 6.insert(pos, item) 指定位置添加元素
 * 7.remove(item)  删除节点
 * 8.search(item)  查找节点是否存在
 */
class DoubleLink
{
    /**
     * 头节点
     * @var
     */
    private $head;

    /**
     * SingleLink constructor.
     */
    public function __construct()
    {
        $this->head = null;
    }

    /**
     * 链表是否为空
     * @return bool
     */
    public function isEmpty()
    {
        return is_null($this->head);
    }

    /**
     * 链表长度
     * @return int
     */
    public function length()
    {
        $cur = $this->head;
        $count = 0;
        while(!is_null($cur)){
            $count++;
            $cur = $cur->next;
        }
        return $count;
    }

    /**
     * 遍历整个链表
     */
    public function travel()
    {
        $cur = $this->head;
        $tmp = [];

        while (!is_null($cur)) {
            array_push($tmp,$cur->item);
            $cur = $cur->next;
        }
        return $tmp;
    }

    /**
     * 链表头部添加元素
     * @param $item
     */
    public function add($item)
    {
        $node = new Node($item);
        if($this->isEmpty()){
            $this->head = $node;
        }else{
            //待插入节点后继节点为原本头节点
            $node->next = $this->head;
            //待插入节点为原本头节点的前驱节点
            $this->head->prev = $node;
            //待插入节点变为头节点
            $this->head = $node;
        }
    }

    /**
     * 链表尾部添加元素
     * @param $item
     */
    public function append($item)
    {
        $node = new Node($item);
        if($this->isEmpty()){
            $this->head = $node;
        }else{
            //移动到尾节点
            $cur = $this->head;
            while (!is_null($cur->next)){
                $cur = $cur->next;
            }
            //原本尾节点next指向待插入节点
            $cur->next = $node;
            //待插入节点prev指向原本尾节点
            $node->prev = $cur;
        }
    }

    /**
     * 指定位置添加元素
     * @param $pos
     * @param $item
     */
    public function insert($pos, $item)
    {
        switch ($pos){
            //若指定位置pos为第一个元素之前,则执行头部插入
            case $pos <= 0:
                $this->add($item);
                break;
            //若指定位置超过链表尾部,则执行尾部插入
            case $pos > ($this->length() - 1):
                $this->append($item);
                break;
            //找到位置
            default:
                $node = new Node($item);
                $count = 0;
                $cur = $this->head;

                //移到指定位置的前一个位置
                while ($count < ($pos - 1)){
                    $count++;
                    $cur = $cur->next;
                }
                $node->prev = $cur;
                $node->next = $cur->next;
                $cur->next->prev = $node;
                $cur->next = $node;
        }
    }

    /**
     * 删除节点
     * @param $item
     */
    public function remove($item)
    {
        if($this->isEmpty()){
            return;
        }

        $cur = $this->head;
        //如果第一个就是删除的节点
        if($cur->item == $item){
            //如果只有这一个节点
            if(is_null($cur->next)){
                $this->head = null;
            }else{
                $cur->next->prev = null;
                $this->head = $cur->next;
            }
            return;
        }
        while (!is_null($cur)){
            //找到元素
            if($cur->item == $item){
               $cur->prev->next = $cur->next;
               $cur->next->prev = $cur->prev;
                break;
            }
            $cur = $cur->next;
        }
    }

    /**
     * 查找节点是否存在
     * @param $item
     * @return bool
     */
    public function search($item)
    {
        $cur = $this->head;
        while (!is_null($cur)){
            if($cur->item == $item){
                return true;
            }
            $cur = $cur->next;
        }
        return false;
    }
}

$s = new DoubleLink();
$s->add('23');
$s->add('er');
$s->append('56');
$s->insert(2,'2222');
//echo $s->length();
//var_dump($s->travel());
//var_dump($s->search('er'));
$s->remove('er');
var_dump($s->travel());

 

转载于:https://my.oschina.net/programs/blog/1648198

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值