当前位置:首页 > PHP

php 链表实现

2026-04-02 22:20:33PHP

PHP 链表实现方法

链表是一种线性数据结构,通过节点之间的指针连接实现动态存储。在PHP中可以通过类(Class)和对象(Object)模拟链表结构。

链表节点类定义

链表的基础是节点(Node),每个节点包含数据和指向下一个节点的指针:

class ListNode {
    public $data; // 节点数据
    public $next; // 指向下一个节点的指针

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

链表类实现

链表类需要实现基本操作如插入、删除、遍历等:

class LinkedList {
    private $head; // 头节点

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

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

    // 在链表尾部插入新节点
    public function insertAtEnd($data) {
        $newNode = new ListNode($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->insertAtEnd(10);
$list->insertAtEnd(20);
$list->insertAtBeginning(5);

// 打印链表: 5 -> 10 -> 20 -> NULL
$list->printList();

// 删除节点
$list->deleteNode(10);

// 打印链表: 5 -> 20 -> NULL
$list->printList();

链表反转实现

链表反转是常见操作,可以通过迭代或递归实现:

// 在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;
}

双向链表实现

双向链表每个节点包含指向前后节点的指针:

php 链表实现

class DoublyListNode {
    public $data;
    public $next;
    public $prev;

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

class DoublyLinkedList {
    private $head;
    private $tail;

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

    // 实现双向链表的插入、删除等方法
}

链表应用场景

链表适用于需要频繁插入删除的场景,相比数组有更好的动态性能。PHP中虽然数组功能强大,但链表在某些算法问题(如LRU缓存、多项式运算等)中仍有应用价值。

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

相关文章

php实现登录

php实现登录

PHP 登录功能实现 数据库连接与用户表创建 创建一个数据库连接文件 db.php,用于管理数据库连接。MySQL 示例代码如下: <?php $host = 'localhost'; $db…

php实现验证码的识别

php实现验证码的识别

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

php实现秒杀

php实现秒杀

PHP实现秒杀功能的关键技术 秒杀系统需要解决高并发、数据一致性、系统稳定性等问题。以下是PHP实现秒杀功能的核心技术方案: 数据库优化 使用InnoDB引擎的行级锁和事务特性,确保数据一致性。商品…

php实现获取验证码

php实现获取验证码

使用GD库生成验证码 在PHP中,可以通过GD库来生成验证码图片。以下是一个简单的实现示例: <?php session_start(); $width = 120; $height = 40…

php商城 设计与实现

php商城 设计与实现

PHP商城设计与实现的关键步骤 需求分析与规划 明确商城类型(B2B/B2C)、目标用户群体及核心功能模块。基础功能需包含用户注册登录、商品展示、购物车、订单管理、支付接口集成、后台管理。扩展功能可考…

php实现在线客服

php实现在线客服

PHP实现在线客服系统 在线客服系统通常需要实时通信功能,可通过PHP结合WebSocket或长轮询实现。以下是几种常见实现方式: 使用WebSocket实现实时通信 安装Ratchet库(PHP…