当前位置:首页 > PHP

php 链表实现

2026-02-13 14:50:51PHP

PHP 链表实现方法

链表是一种线性数据结构,由节点组成,每个节点包含数据和指向下一个节点的指针。以下是PHP中实现链表的方法。

定义链表节点类

链表的基本单位是节点,每个节点包含数据和指向下一个节点的指针。

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 isEmpty() {
        return $this->head === null;
    }
}

插入节点到链表头部

在链表头部插入一个新节点。

php 链表实现

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->isEmpty()) {
        $this->head = $newNode;
    } else {
        $current = $this->head;
        while ($current->next !== null) {
            $current = $current->next;
        }
        $current->next = $newNode;
    }
}

删除链表中的节点

删除链表中第一个匹配的节点。

public function deleteNode($data) {
    if ($this->isEmpty()) {
        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;
    }
}

遍历链表

打印链表中的所有节点数据。

php 链表实现

public function display() {
    $current = $this->head;
    while ($current !== null) {
        echo $current->data . " ";
        $current = $current->next;
    }
    echo "\n";
}

查找链表中的节点

检查链表中是否存在某个数据。

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

链表反转

反转链表中的所有节点。

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 = new LinkedList();
$list->insertAtHead(10);
$list->insertAtTail(20);
$list->insertAtTail(30);
$list->display(); // 输出: 10 20 30
$list->deleteNode(20);
$list->display(); // 输出: 10 30
$list->reverse();
$list->display(); // 输出: 30 10

以上代码展示了如何在PHP中实现链表的基本操作。可以根据需求扩展更多功能,如插入到指定位置、获取链表长度等。

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

相关文章

php验证码的实现

php验证码的实现

PHP 验证码实现方法 使用 GD 库生成基础验证码 创建画布并生成随机字符串作为验证码内容: session_start(); $width = 120; $height = 40; $image…

php实现https

php实现https

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

php 实现md5

php 实现md5

使用 PHP 内置函数实现 MD5 PHP 提供了 md5() 函数,可以直接计算字符串的 MD5 哈希值。该函数接受一个字符串参数,返回 32 字符的十六进制哈希值。 $string = "H…

php发帖实现

php发帖实现

PHP 发帖实现方法 数据库准备 创建一个数据库表用于存储帖子信息,常见的字段包括帖子ID、标题、内容、作者、发布时间等。SQL示例: CREATE TABLE posts ( id INT…

php实现区块链

php实现区块链

PHP 实现区块链的基本步骤 PHP 可以用于实现一个简单的区块链系统,以下是关键步骤和代码示例: 区块类实现 创建一个 Block 类来表示区块链中的单个区块: class Bloc…

php 实现下载apk

php 实现下载apk

使用 PHP 实现 APK 文件下载 通过 PHP 实现 APK 文件下载的核心是设置正确的 HTTP 头信息,并输出文件内容。以下是一个完整的实现方法。 设置 HTTP 头信息 确保客户端正确识别…