PHP数据结构与算法:单链表

一、链表

链表是一种常见的线性结构,但是不像顺序表一样连续存储数据,而是在每一个节点(数据存储单元)里存放下一个节点的位置信息(地址)。

二、单链表

单向链表也叫单链表,是链表中最简单的一种形式。它的每个节点包含两个域,一个信息域(元素域)和一个链接域。这个链接指向链表中的下一个节点,而最后一个节点的链接域则指向空值。

  • 表元素域(elem)用来存放具体的数据
  • 链接域(next)用来存放下一个节点的地址
  • 变量p指向链表的头节点(首节点)的位置,从p出发能找到表中的任意节点

三、单链表PHP实现

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

    /**
     * 下一节点
     * @var
     */
    public $next;

    /**
     * SingleNode constructor.
     * @param $item
     */
    public function __construct($item)
    {
        $this->item = $item;
        $this->next = null;
    }
}
2.链表实现
/**
 * 单链表基本操作
 *
 * 1.isEmpty()    链表是否为空
 * 2.length()      链表长度
 * 3.travel()      遍历整个链表
 * 4.add(item)     链表头部添加元素
 * 5.append(item)  链表尾部添加元素
 * 6.insert(pos, item) 指定位置添加元素
 * 7.remove(item)  删除节点
 * 8.search(item)  查找节点是否存在
 */
class SingleLink
{
    /**
     * 头节点
     * @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 SingleNode($item);
        $node->next = $this->head;
        $this->head = $node;
    }

    /**
     * 链表尾部添加元素
     * @param $item
     */
    public function append($item)
    {
        $node = new SingleNode($item);
        if($this->isEmpty()){
            $this->head = $node;
        }else{
            $cur = $this->head;
            while (!is_null($cur->next)){
                $cur = $cur->next;
            }
            $cur->next = $node;
        }
    }

    /**
     * 指定位置添加元素
     * @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 SingleNode($item);
                $count = 0;
                //$pre用来指向指定位置pos的前一个位置pos-1,初始从头节点开始移动到指定位置
                $pre = $this->head;
                while ($count < ($pos - 1)){
                    $count++;
                    $pre = $pre->next;
                }
                $node->next = $pre->next;
                $pre->next = $node;
        }
    }

    /**
     * 删除节点
     * @param $item
     */
    public function remove($item)
    {
        $cur = $this->head;
        $pre = null;
        while (!is_null($cur->next)){
            //找到元素
            if($cur->item == $item){
                //如果第一个就是删除的节点
                if($cur == $this->head){
                    $this->head = $cur->next;
                }else{
                    $pre->next = $cur->next;
                }
                break;
            }else{
                $pre = $cur;
                $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 SingleLink();
$s->add('23');
$s->add('er');
$s->append('56');
$s->insert(2,'2222');
//var_dump($s->travel());
//var_dump($s->search('er'));
$s->remove('er');
var_dump($s->travel());
3.部分图示

头部添加元素:

指定位置添加元素:

删除节点:

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值