当前位置:首页 > PHP

php 实现链表

2026-04-02 22:22:12PHP

PHP 实现链表的基本方法

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

定义链表节点类

创建一个 ListNode 类表示链表的节点,包含数据 (data) 和指向下一个节点的指针 (next)。

php 实现链表

class ListNode {
    public $data;
    public $next;

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

定义链表类

创建一个 LinkedList 类管理链表的基本操作,如插入、删除和遍历。

php 实现链表

class LinkedList {
    public $head;

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

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

    // 在链表尾部插入节点
    public function insertAtTail($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->insertAtHead(10);
$list->insertAtHead(20);
$list->insertAtTail(30);

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

// 删除节点
$list->deleteNode(10);
$list->printList(); // 输出: 20 -> 30 -> NULL

链表的高级操作

链表还可以实现反转、查找中间节点等高级操作。

反转链表

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

查找中间节点

public function findMiddle() {
    $slow = $this->head;
    $fast = $this->head;
    while ($fast !== null && $fast->next !== null) {
        $slow = $slow->next;
        $fast = $fast->next->next;
    }
    return $slow->data;
}

链表的应用场景

链表适用于动态数据管理,如实现队列、栈或哈希表的冲突解决。PHP 中链表的使用较少,但在特定场景下(如内存优化或频繁插入删除)仍有价值。

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

相关文章

php 实现自动收货

php 实现自动收货

PHP 实现自动收货功能 自动收货功能通常用于电商系统,在订单超过一定时间未手动确认收货时自动完成订单。以下是实现方法: 数据库设计 需要订单表包含以下关键字段: CREATE TABLE…

php实现登录

php实现登录

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

php 实现登录

php 实现登录

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

php实现https

php实现https

PHP 实现 HTTPS 请求 在 PHP 中实现 HTTPS 请求,可以通过多种方式完成,包括使用内置函数、cURL 扩展或第三方库。以下是几种常见的方法: 使用 file_get_content…

php https实现

php https实现

PHP 中实现 HTTPS 的方法 在 PHP 中实现 HTTPS 主要通过配置服务器和编写代码来确保通信的安全性。以下是几种常见的方法: 配置服务器支持 HTTPS 确保服务器已安装 SSL/T…

php变量的实现

php变量的实现

PHP变量的实现机制 PHP变量的实现基于Zend引擎的底层数据结构。核心结构体zval用于存储变量类型和值,其设计允许动态类型转换和高效内存管理。 zval结构体 PHP变量通过zval结构体实现…