当前位置:首页 > 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指针。

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实现链表

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

相关文章

php实现文件下载

php实现文件下载

PHP实现文件下载的方法 通过PHP实现文件下载功能,可以采用多种方式,以下是几种常见的实现方法: 使用header()函数强制下载 设置HTTP头信息,强制浏览器下载文件而非直接打开:…

php实现搜索功能

php实现搜索功能

实现基本的搜索功能 在PHP中实现搜索功能通常涉及数据库查询。以下是一个简单的实现方式,假设使用MySQL数据库: <?php // 连接数据库 $conn = new mysqli('loc…

php实现树

php实现树

PHP实现树结构的方法 在PHP中实现树结构通常可以通过递归或迭代的方式完成。以下是几种常见的实现方法: 递归实现树结构 递归是处理树结构的自然方式,尤其适用于具有未知深度的树。 class Tr…

php实现图片上传

php实现图片上传

实现图片上传的基本步骤 创建HTML表单用于文件上传,表单需设置enctype="multipart/form-data"属性,确保文件数据正确传输。 <form action="upload…

php 实现自动收货

php 实现自动收货

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

php实现增删改查

php实现增删改查

以下是在PHP中实现增删改查(CRUD)操作的详细方法,以MySQL数据库为例: 数据库连接配置 创建config.php文件存储数据库连接信息: <?php $host = 'loca…