当前位置:首页 > PHP

php 实现链表

2026-01-15 12:22:27PHP

PHP 实现链表的方法

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

定义链表节点类

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

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";
    }
}

使用链表

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

$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实现分页

php实现分页

PHP实现分页的基本方法 使用LIMIT和OFFSET进行数据库查询分页。通过计算当前页码和每页显示数量,动态生成SQL语句。 $page = isset($_GET['page']) ? (in…

php实现文件上传

php实现文件上传

PHP 文件上传实现方法 在 PHP 中实现文件上传需要处理 HTML 表单和服务器端脚本。以下是详细步骤: 创建 HTML 表单 使用 enctype="multipart/form-data"…

php实现购物车

php实现购物车

PHP实现购物车功能 购物车功能是电商网站的核心模块之一,通常涉及商品添加、删除、数量修改和结算等操作。以下是使用PHP实现购物车功能的几种方法。 使用Session存储购物车数据 Session…

php实现文件下载

php实现文件下载

PHP实现文件下载的方法 通过PHP实现文件下载功能,可以采用多种方式,以下是几种常见的实现方法: 使用header()函数强制下载 设置HTTP头信息,强制浏览器下载文件而非直接打开: $fi…

php实现验证码

php实现验证码

PHP实现验证码的方法 使用GD库生成验证码 GD库是PHP中处理图像的扩展,可用于生成验证码图片。 <?php session_start(); $width = 120; $height…

php实现树

php实现树

PHP实现树结构的方法 在PHP中实现树结构通常可以通过递归或迭代的方式完成。以下是几种常见的实现方法: 递归实现树结构 递归是处理树结构的自然方式,尤其适用于具有未知深度的树。 class…