当前位置:首页 > PHP

php 实现链表

2026-01-15 12:22:27PHP

PHP 实现链表的方法

链表是一种线性数据结构,由节点组成,每个节点包含数据和指向下一个节点的指针。PHP 中可以通过类和对象来实现链表。

定义链表节点类

创建一个 ListNode 类,用于表示链表的节点。每个节点包含 datanext 属性。

php 实现链表

class ListNode {
    public $data = null;
    public $next = null;

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

定义链表类

创建一个 LinkedList 类,用于管理链表操作。包含 head 属性表示链表的头部节点。

class LinkedList {
    public $head = null;

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

    // 删除指定数据的节点
    public function delete($data) {
        if ($this->head === null) {
            return;
        }
        if ($this->head->data === $data) {
            $this->head = $this->head->next;
            return;
        }
        $current = $this->head;
        while ($current->next !== null) {
            if ($current->next->data === $data) {
                $current->next = $current->next->next;
                return;
            }
            $current = $current->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";
    }
}

使用链表

创建一个链表实例,并执行插入、删除、查找和打印操作。

php 实现链表

$list = new LinkedList();
$list->insert(10);
$list->insert(20);
$list->insert(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

实现链表反转

LinkedList 类中添加反转链表的方法。

public function reverse() {
    $prev = null;
    $current = $this->head;
    while ($current !== null) {
        $next = $current->next;
        $current->next = $prev;
        $prev = $current;
        $current = $next;
    }
    $this->head = $prev;
}

调用反转方法后打印链表:

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

链表的时间复杂度

  • 插入操作:O(n)(需要遍历到链表尾部)
  • 删除操作:O(n)(需要遍历找到目标节点)
  • 查找操作:O(n)(需要遍历链表)
  • 反转操作:O(n)(需要遍历整个链表)

通过以上方法,可以在 PHP 中实现链表的基本操作。链表适用于动态数据管理,插入和删除操作效率较高。

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

相关文章

php记住密码怎么实现

php记住密码怎么实现

使用Cookie实现记住密码功能 在PHP中,可以通过设置Cookie来记住用户的登录状态。以下是一个简单的实现方式: // 用户登录验证成功后 if ($login_success) {…

php 实现登录

php 实现登录

PHP 登录功能实现 使用 PHP 实现登录功能通常需要结合数据库验证和会话管理。以下是一个基本的登录实现流程: 数据库准备 创建用户表存储用户名和密码(建议使用哈希加密): CREATE TAB…

php实现异步

php实现异步

PHP 实现异步的方法 PHP 本身是同步执行的脚本语言,但可以通过以下方法模拟异步操作或实现异步效果: 使用多进程(pcntl_fork) 通过 pcntl_fork 创建子进程实现异步,适用于…

php 实现验证码

php 实现验证码

使用 PHP 生成验证码 验证码(CAPTCHA)用于防止自动化脚本提交表单,以下是几种常见的 PHP 实现方法。 使用 GD 库生成图片验证码 GD 库是 PHP 的图像处理扩展,可用于生成验证码…

php实现即时通讯

php实现即时通讯

PHP 实现即时通讯的方案 即时通讯(IM)可以通过多种方式实现,PHP 作为服务器端语言,通常需要结合前端技术(如 WebSocket、AJAX)或第三方服务来实现实时通信。以下是几种常见的实现方式…

php 实现类

php 实现类

PHP 类的基本实现 在 PHP 中,类通过 class 关键字定义,后跟类名和一对花括号。类名通常遵循大驼峰命名法(如 ClassName)。以下是基本语法: class ClassName {…