当前位置:首页 > PHP

php单链表实现

2026-02-16 06:50:49PHP

PHP 单链表实现

单链表是一种线性数据结构,由节点组成,每个节点包含数据和指向下一个节点的指针。以下是 PHP 实现单链表的基本方法。

节点类定义

单链表的节点类包含数据(data)和指向下一个节点的指针(next)。

class ListNode {
    public $data;
    public $next;

    public function __construct($data) {
        $this->data = $data;
        $this->next = null;
    }
}

链表类定义

链表类包含头节点(head)和基本操作方法。

php单链表实现

class LinkedList {
    private $head;

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

    // 判断链表是否为空
    public function isEmpty() {
        return $this->head === null;
    }

    // 在链表头部插入节点
    public function insertAtHead($data) {
        $newNode = new ListNode($data);
        $newNode->next = $this->head;
        $this->head = $newNode;
    }

    // 在链表尾部插入节点
    public function insertAtTail($data) {
        $newNode = new ListNode($data);
        if ($this->isEmpty()) {
            $this->head = $newNode;
            return;
        }
        $current = $this->head;
        while ($current->next !== null) {
            $current = $current->next;
        }
        $current->next = $newNode;
    }

    // 删除指定数据的节点
    public function delete($data) {
        if ($this->isEmpty()) {
            return;
        }
        if ($this->head->data === $data) {
            $this->head = $this->head->next;
            return;
        }
        $current = $this->head;
        while ($current->next !== null && $current->next->data !== $data) {
            $current = $current->next;
        }
        if ($current->next !== null) {
            $current->next = $current->next->next;
        }
    }

    // 查找节点是否存在
    public function search($data) {
        $current = $this->head;
        while ($current !== null) {
            if ($current->data === $data) {
                return true;
            }
            $current = $current->next;
        }
        return false;
    }

    // 打印链表
    public function display() {
        $current = $this->head;
        while ($current !== null) {
            echo $current->data . " -> ";
            $current = $current->next;
        }
        echo "NULL\n";
    }
}

使用示例

$list = new LinkedList();
$list->insertAtHead(10);
$list->insertAtTail(20);
$list->insertAtTail(30);
$list->display(); // 输出: 10 -> 20 -> 30 -> NULL

$list->delete(20);
$list->display(); // 输出: 10 -> 30 -> NULL

echo $list->search(30) ? "Found\n" : "Not Found\n"; // 输出: Found

复杂度分析

  • 插入操作:头部插入为 O(1),尾部插入为 O(n)。
  • 删除操作:最坏情况下为 O(n)。
  • 查找操作:O(n)。

通过以上实现,可以完成单链表的基本操作。

标签: 链表php
分享给朋友:

相关文章

php实现增删改查

php实现增删改查

以下是在PHP中实现增删改查(CRUD)操作的详细方法,以MySQL数据库为例: 数据库连接配置 创建config.php文件存储数据库连接信息: <?php $host = 'localh…

php实现mysql

php实现mysql

PHP 连接 MySQL 的基本方法 PHP 提供了多种方式与 MySQL 数据库进行交互,以下是最常用的几种方法。 使用 MySQLi 扩展 MySQLi 是 MySQL 的改进扩展,支持面向对…

php实现聊天

php实现聊天

实现 PHP 聊天功能的方法 使用 WebSocket 和 PHP 实现实时聊天 WebSocket 是一种全双工通信协议,适合实现实时聊天功能。PHP 可以通过 Ratchet 库实现 WebSo…

php 实现单链表

php 实现单链表

单链表的基本概念 单链表是一种线性数据结构,由节点组成,每个节点包含数据域和指向下一个节点的指针域。链表的头节点是访问整个链表的入口。 单链表的节点类实现 在PHP中,可以通过类来定义链表节…

php 实现mvc

php 实现mvc

PHP 实现 MVC 架构的方法 MVC(Model-View-Controller)是一种常见的软件设计模式,用于分离业务逻辑、用户界面和用户输入。以下是 PHP 实现 MVC 架构的关键步骤:…

php实现文章

php实现文章

PHP实现文章管理功能 在PHP中实现文章管理功能通常涉及数据库操作、表单处理和页面展示。以下是实现文章管理的基本步骤: 数据库设计 创建articles表,包含字段如id、title、conten…