当前位置:首页 > 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;
    }
}

插入节点到链表头部

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

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

遍历链表

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

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

使用链表

创建一个链表实例并进行操作。

php 链表实现

$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中实现文件上传需要使用$_FILES超全局数组处理上传的文件数据。表单必须设置enctype="multipart/form-data"属性,并采用POST方法提交。 创…

php实现队列

php实现队列

PHP实现队列的方法 队列是一种先进先出(FIFO)的数据结构,PHP可以通过多种方式实现队列功能。 使用数组模拟队列 PHP数组可以模拟队列的基本操作,通过array_push和array_shi…

php 函数实现

php 函数实现

PHP 函数实现基础 PHP 函数的定义使用 function 关键字,后跟函数名和参数列表。函数名不区分大小写,但建议保持一致。 function functionName($param1, $p…

php变量的实现

php变量的实现

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

php 路由实现的

php 路由实现的

PHP 路由实现方法 基于原生 PHP 实现路由 创建一个简单的路由解析器,通过解析 URL 路径来调用对应的控制器或函数。 // 定义路由表 $routes = [ '/' => '…

php 实现锁

php 实现锁

PHP 实现锁的常见方法 文件锁(flock) 利用 flock 函数对文件进行独占锁定,适用于单机环境。 $fp = fopen("lockfile.lock", "w+"); if (flock…