当前位置:首页 > PHP

php实现链表

2026-01-29 00:27:31PHP

链表的基本概念

链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和指向下一个节点的指针。与数组不同,链表在内存中是非连续存储的,动态增删节点更高效。

单链表实现

PHP中可以通过类实现单链表。节点类包含数据(data)和指向下一个节点的指针(next),链表类提供插入、删除等操作方法。

class ListNode {
    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 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(3);
$list->insertAtHead(2);
$list->insertAtHead(1);
$list->insertAtTail(4);
$list->printList(); // 输出: 1 -> 2 -> 3 -> 4 -> NULL
$list->deleteNode(2);
$list->printList(); // 输出: 1 -> 3 -> 4 -> NULL

双向链表实现

双向链表的节点包含指向前驱和后继的指针,支持双向遍历。实现时需额外维护prev指针。

php实现链表

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

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

class DoublyLinkedList {
    public $head;

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

    // 在尾部插入节点
    public function insertAtTail($data) {
        $newNode = new DoublyListNode($data);
        if ($this->head === null) {
            $this->head = $newNode;
            return;
        }
        $current = $this->head;
        while ($current->next !== null) {
            $current = $current->next;
        }
        $current->next = $newNode;
        $newNode->prev = $current;
    }

    // 打印链表(正向)
    public function printForward() {
        $current = $this->head;
        while ($current !== null) {
            echo $current->data . " <-> ";
            $current = $current->next;
        }
        echo "NULL\n";
    }
}

// 使用示例
$dll = new DoublyLinkedList();
$dll->insertAtTail(1);
$dll->insertAtTail(2);
$dll->printForward(); // 输出: 1 <-> 2 <-> NULL

链表操作注意事项

  • 内存管理:PHP自动处理垃圾回收,但需注意避免循环引用(尤其在双向链表中)。
  • 边界条件:处理空链表、头节点或尾节点时需特殊判断。
  • 时间复杂度:插入/删除头节点为O(1),而尾部操作需遍历为O(n)。

通过上述实现,可灵活扩展链表功能(如反转、检测环等),适应不同场景需求。

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

相关文章

js 实现链表

js 实现链表

链表的基本概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和指向下一个节点的指针。与数组不同,链表在内存中是非连续存储的,插入和删除操作效率较高。 链表的实现 在 JavaScrip…

php实现分类

php实现分类

PHP实现分类的方法 数据库设计 创建分类表时,通常需要包含id、名称、父级id等字段。父级id用于实现多级分类结构。 CREATE TABLE categories ( id INT AU…

php实现下载

php实现下载

PHP实现文件下载 使用PHP实现文件下载可以通过设置HTTP头部信息强制浏览器下载文件而非直接打开。以下是几种常见的方法: 方法一:直接输出文件内容 $file_path = 'path/to/…

php 实现单链表

php 实现单链表

单链表的基本概念 单链表是一种线性数据结构,由节点组成,每个节点包含数据域和指向下一个节点的指针域。链表的头节点是访问整个链表的入口。 单链表的节点类实现 在PHP中,可以通过类来定义链表节点。每个…

php 实现mvc

php 实现mvc

PHP 实现 MVC 架构的方法 MVC(Model-View-Controller)是一种常见的软件设计模式,用于分离业务逻辑、用户界面和用户输入。以下是 PHP 实现 MVC 架构的关键步骤:…

php数组实现

php数组实现

PHP 数组的基本实现 PHP 数组是一种非常灵活的数据结构,可以存储多个值,并且支持多种类型的键(整数或字符串)。PHP 数组实际上是有序映射(ordered map),可以看作是列表(vector…