当前位置:首页 > PHP

php实现链表操作

2026-03-13 05:04:20PHP

PHP 实现链表的基本结构

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

class Node {
    public $data;
    public $next;

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

class LinkedList {
    public $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 insertAtPosition($data, $position) {
    if ($position < 0) {
        throw new Exception("Invalid position");
    }
    $newNode = new Node($data);
    if ($position === 0) {
        $newNode->next = $this->head;
        $this->head = $newNode;
        return;
    }
    $current = $this->head;
    for ($i = 0; $i < $position - 1 && $current !== null; $i++) {
        $current = $current->next;
    }
    if ($current === null) {
        throw new Exception("Position out of bounds");
    }
    $newNode->next = $current->next;
    $current->next = $newNode;
}

删除链表中的节点

删除操作可以根据节点值或位置进行。

根据值删除

php实现链表操作

public function deleteByValue($value) {
    if ($this->head === null) {
        return;
    }
    if ($this->head->data === $value) {
        $this->head = $this->head->next;
        return;
    }
    $current = $this->head;
    while ($current->next !== null && $current->next->data !== $value) {
        $current = $current->next;
    }
    if ($current->next !== null) {
        $current->next = $current->next->next;
    }
}

根据位置删除

public function deleteByPosition($position) {
    if ($position < 0 || $this->head === null) {
        throw new Exception("Invalid position");
    }
    if ($position === 0) {
        $this->head = $this->head->next;
        return;
    }
    $current = $this->head;
    for ($i = 0; $i < $position - 1 && $current !== null; $i++) {
        $current = $current->next;
    }
    if ($current === null || $current->next === null) {
        throw new Exception("Position out of bounds");
    }
    $current->next = $current->next->next;
}

遍历链表

遍历链表可以打印节点数据或查找特定值。

打印链表

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

查找节点

php实现链表操作

public function search($value) {
    $current = $this->head;
    while ($current !== null) {
        if ($current->data === $value) {
            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;
}

递归法

public function reverseRecursive($node) {
    if ($node === null || $node->next === null) {
        return $node;
    }
    $rest = $this->reverseRecursive($node->next);
    $node->next->next = $node;
    $node->next = null;
    return $rest;
}

链表长度计算

计算链表的长度可以通过遍历实现。

public function getLength() {
    $count = 0;
    $current = $this->head;
    while ($current !== null) {
        $count++;
        $current = $current->next;
    }
    return $count;
}

示例用法

以下是一个完整的示例,展示如何使用链表类。

$list = new LinkedList();
$list->insertAtTail(10);
$list->insertAtTail(20);
$list->insertAtHead(5);
$list->insertAtPosition(15, 2);
$list->printList(); // 输出: 5 -> 10 -> 15 -> 20 -> NULL

$list->deleteByValue(10);
$list->deleteByPosition(1);
$list->printList(); // 输出: 5 -> 20 -> NULL

$list->reverse();
$list->printList(); // 输出: 20 -> 5 -> NULL

echo "Length: " . $list->getLength(); // 输出: Length: 2

标签: 链表操作
分享给朋友:

相关文章

链表实现js

链表实现js

链表的基本概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和一个指向下一个节点的指针(单向链表)或两个指针(双向链表)。与数组不同,链表在内存中非连续存储,插入和删除操作更高效,但随机…

react如何操作节点数据

react如何操作节点数据

操作节点数据的常见方法 在React中操作DOM节点数据通常通过ref实现,以下是几种典型场景的解决方案: 使用useRef创建引用 通过useRef钩子创建引用并绑定到DOM元素,后续可直接操作节…

react如何操作引用页面的值

react如何操作引用页面的值

获取引用页面的值 在React中,可以通过useRef钩子或createRef方法创建引用(ref),用于直接访问DOM节点或React组件实例。以下为具体实现方式: 使用函数组件与useRef…

php实现链表

php实现链表

链表的基本概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和指向下一个节点的指针。与数组不同,链表在内存中是非连续存储的,动态增删节点更高效。 单链表实现 PHP中可以通过类…

php实现链表操作

php实现链表操作

PHP实现链表基础结构 链表由节点(Node)构成,每个节点包含数据域和指向下一个节点的指针。以下是单向链表的节点类实现: class ListNode { public $data;…

js实现数据库操作

js实现数据库操作

JavaScript 实现数据库操作的方法 JavaScript 可以通过多种方式操作数据库,包括浏览器端的本地存储、服务器端的数据库连接以及使用 ORM 框架等。以下是几种常见的实现方法: 浏览器…