当前位置:首页 > 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. 反转链表

    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 实现打印功能的方法 PHP 可以通过多种方式实现打印功能,具体取决于打印的目标(如打印机、PDF、网页等)和需求(如直接打印、生成打印预览等)。以下是几种常见的实现方式: 使用 JavaSc…

php实现定时任务

php实现定时任务

PHP 实现定时任务的几种方法 在 PHP 中实现定时任务可以通过多种方式完成,具体选择取决于项目需求和服务器环境。以下是常见的实现方法: 使用 Cron 任务 Cron 是 Linux 系统中常…

php怎么实现登录功能

php怎么实现登录功能

数据库准备 创建用户表存储登录信息,通常包括用户名、密码(需加密)等字段。示例SQL: CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY…

php 实现秒杀

php 实现秒杀

PHP 实现秒杀的关键技术 秒杀系统需要解决高并发、超卖、性能瓶颈等问题。以下是实现秒杀的核心方案: 使用 Redis 预减库存 将商品库存提前加载到 Redis 中,通过原子操作保证库存准确性:…

php搜索功能实现的

php搜索功能实现的

PHP 搜索功能实现方法 数据库搜索实现 使用 SQL 的 LIKE 语句进行模糊匹配,适用于小型网站和简单搜索需求。 $searchTerm = $_GET['search']; $query =…

php实现的进度条

php实现的进度条

PHP 实现的进度条方法 在 PHP 中实现进度条通常可以通过命令行脚本(CLI)或 Web 页面两种方式完成。以下是几种常见的实现方法: 命令行进度条 使用 PHP CLI 实现进度条,可以通过…