当前位置:首页 > PHP

php 链表实现

2026-01-15 12:20:44PHP

在 PHP 中实现链表可以通过类来模拟链表节点(Node)和链表(LinkedList)结构。以下是链表的完整实现示例,包含基础操作如插入、删除、遍历等。

链表节点类(Node)

链表的基本单元是节点,每个节点包含数据(data)和指向下一个节点的指针(next)。

class Node {
    public $data;
    public $next;

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

链表类(LinkedList)

链表类管理节点的增删查改操作,并维护头节点(head)。

class LinkedList {
    private $head;

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

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

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

    // 删除指定值的节点
    public function deleteNode($data) {
        if ($this->head === null) 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 printList() {
        $current = $this->head;
        while ($current !== null) {
            echo $current->data . " -> ";
            $current = $current->next;
        }
        echo "NULL\n";
    }
}

使用示例

$list = new LinkedList();
$list->insertAtHead(3);     // 链表: 3 -> NULL
$list->insertAtHead(2);     // 链表: 2 -> 3 -> NULL
$list->insertAtTail(4);     // 链表: 2 -> 3 -> 4 -> NULL
$list->deleteNode(3);       // 链表: 2 -> 4 -> NULL
$list->printList();         // 输出: 2 -> 4 -> NULL

扩展操作

  1. 查找节点是否存在

    public function search($data) {
     $current = $this->head;
     while ($current !== null) {
         if ($current->data === $data) {
             return true;
         }
         $current = $current->next;
     }
     return false;
    }
  2. 反转链表

    php 链表实现

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

注意事项

  • PHP 中链表是模拟实现,实际内存管理由语言自动处理。
  • 双向链表可在节点类中增加 prev 指针,并调整插入/删除逻辑。

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

相关文章

php实现树

php实现树

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

php实现接口

php实现接口

PHP 实现接口的方法 在 PHP 中,接口(Interface)用于定义一组方法的规范,而不实现具体功能。类可以实现一个或多个接口,并必须实现接口中定义的所有方法。 定义接口 接口使用 inter…

php实现静态

php实现静态

静态化实现方法 PHP实现静态化主要通过生成HTML文件或缓存机制来减少服务器动态解析的开销。以下是几种常见方法: 生成静态HTML文件 通过file_put_contents()将动态内容保存为H…

php实现过程

php实现过程

PHP 实现过程 PHP 是一种广泛使用的服务器端脚本语言,特别适合 Web 开发。以下是 PHP 实现过程的关键步骤和注意事项。 安装与配置 下载并安装 PHP,可以从官方 PHP 网站获取最新版…

链表实现js

链表实现js

链表的基本概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和一个指向下一个节点的指针(单向链表)或两个指针(双向链表)。与数组不同,链表在内存中非连续存储,插入和删除操作更高效,但随机…

php实现验证码的识别

php实现验证码的识别

验证码识别的基本方法 验证码识别通常分为手动实现和第三方工具两种方式。手动实现需要图像处理和机器学习知识,而第三方工具则更快捷。 使用Tesseract OCR识别验证码 Tesseract是一个开…