单向链表PHP简单实现

单向链表PHP简单实现

    class Node
    {
        public int|null $id;
        public mixed $data;
        public Node|null $next;

        public function __construct($id = 0, $data = null)
        {
            $this->id = $id;
            $this->data = $data;
            $this->next = null;
        }
    }
class SingleLinkList
    {
        private Node|null $head = null;

        /**
         *
         * @param $id 节点id  从0 开始
         * @param $data
         */
        public function __construct($id = 0, $data = null)
        {
            $this->head = new Node($id, $data);
        }

        /**
         * 增加节点
         * @param Node $node
         * @return bool
         */
        public function addLink(Node $node)
        {
            if ($this->isLinkEmpty()) {
                return false;
            }
            $current = $this->head;
            while ($current->next != null) {
                if ($current->next->id == $node->id) {
                    echo "链表中已存在id={$node->id}的节点!";
                    return false;
                }

                if ($current->next->id > $node->id) {
                    break;
                }
                $current = $current->next;
            }
            $node->next = $current->next;
            $current->next = $node;
            return true;
        }

        /**
         * 删除节点
         * @param $id
         * @return bool
         */
        public function delLink($id)
        {
            if ($this->isLinkEmpty()) {
                return false;
            }
            $current = $this->head;
            $exist = false;
            if($current->id == $id){
                $this->head = $current->next;
                return true;
            }
            while ($current->next != null) {
                if ($current->next->id == $id) {
                    $exist = true;
                    break;
                }
                $current = $current->next;
            }
            if ($exist) {
                $current->next = $current->next->next;
                return true;
            } else {
                echo "链表中没有找到id={$id}的节点!";
                return false;
            }
        }

        public function updateNodeData($id, $data)
        {
            if ($this->isLinkEmpty()) {
                return false;
            }
            $current = $this->head;
            $exist = false;
            while ($current->next != null) {
                if ($current->id == $id) {
                    $exist = true;
                    break;
                }
                $current = $current->next;
            }
            if ($exist) {
                $current->data = $data;
                return true;
            } else {
                echo "链表中没有找到id={$id}的节点!";
                return false;
            }
        }

        public function getLinkLength()
        {
            if ($this->isLinkEmpty()) {
                return 0;
            }
            $current = $this->head;
            $i = 0;
            while ($current->next != null) {
                $i++;
                $current = $current->next;
            }
            return $i;
        }

        public function clear()
        {
            $this->head = null;
        }

        public function isLinkEmpty()
        {
            if ($this->head == null) {
                echo '链表为空!';
            }
            return false;
        }

        /**
         * @param $id
         * @return false|Node|null
         */
        public function getIndex($id)
        {
            if ($this->isLinkEmpty()) {
                return false;
            }
            $current = $this->head;
            $exist = false;
            while ($current->next != null) {
                if ($current->id == $id) {
                    $exist = true;
                    break;
                }
                $current = $current->next;
            }
            if ($exist) {
                return $current;
            } else {
                echo "链表中没有找到id={$id}的节点!";
                return false;
            }
        }
    }
    $lists = new SingleLinkList();
    $lists->addLink(new Node(1, 1));
    $lists->addLink(new Node(1, 1));
    $lists->addLink(new Node(5, 1));
    $lists->addLink(new Node(4, 1));
    $lists->addLink(new Node(3, 1));
    dump($lists->delLink(0));
    dd($lists);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值