当前位置:首页 > PHP

php 链表实现

2026-04-02 22:20:33PHP

PHP 链表实现方法

链表是一种线性数据结构,通过节点之间的指针连接实现动态存储。在PHP中可以通过类(Class)和对象(Object)模拟链表结构。

链表节点类定义

链表的基础是节点(Node),每个节点包含数据和指向下一个节点的指针:

php 链表实现

class ListNode {
    public $data; // 节点数据
    public $next; // 指向下一个节点的指针

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

链表类实现

链表类需要实现基本操作如插入、删除、遍历等:

php 链表实现

class LinkedList {
    private $head; // 头节点

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

    // 在链表头部插入新节点
    public function insertAtBeginning($data) {
        $newNode = new ListNode($data);
        $newNode->next = $this->head;
        $this->head = $newNode;
    }

    // 在链表尾部插入新节点
    public function insertAtEnd($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->insertAtEnd(10);
$list->insertAtEnd(20);
$list->insertAtBeginning(5);

// 打印链表: 5 -> 10 -> 20 -> NULL
$list->printList();

// 删除节点
$list->deleteNode(10);

// 打印链表: 5 -> 20 -> NULL
$list->printList();

链表反转实现

链表反转是常见操作,可以通过迭代或递归实现:

// 在LinkedList类中添加反转方法
public function reverse() {
    $prev = null;
    $current = $this->head;

    while ($current !== null) {
        $next = $current->next;
        $current->next = $prev;
        $prev = $current;
        $current = $next;
    }

    $this->head = $prev;
}

双向链表实现

双向链表每个节点包含指向前后节点的指针:

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

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

class DoublyLinkedList {
    private $head;
    private $tail;

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

    // 实现双向链表的插入、删除等方法
}

链表应用场景

链表适用于需要频繁插入删除的场景,相比数组有更好的动态性能。PHP中虽然数组功能强大,但链表在某些算法问题(如LRU缓存、多项式运算等)中仍有应用价值。

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

相关文章

php实现websocket

php实现websocket

PHP 实现 WebSocket WebSocket 是一种在单个 TCP 连接上进行全双工通信的协议。PHP 可以通过多种方式实现 WebSocket 功能,以下是几种常见的方法: 使用 Ratc…

php 购物车实现

php 购物车实现

数据库设计 购物车功能通常需要设计数据库表存储商品和用户信息。常见的表包括products(商品表)、users(用户表)和cart(购物车表)。cart表通常包含字段:id(主键)、user_id(…

php实现跳转

php实现跳转

PHP 实现页面跳转的方法 在 PHP 中实现页面跳转有多种方式,以下是常见的几种方法及其适用场景: 使用 header() 函数 通过 header() 函数发送 HTTP 头部信息实现跳转。注意…

php实现文章

php实现文章

PHP实现文章管理功能 在PHP中实现文章管理功能通常涉及数据库操作、表单处理和页面展示。以下是实现文章管理的基本步骤: 数据库设计 创建articles表,包含字段如id、title、conten…

php 实现md5

php 实现md5

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

php session实现购物车

php session实现购物车

PHP Session 实现购物车 初始化 Session 确保在使用 Session 前调用 session_start() 函数。通常放在 PHP 文件的最顶部。 <?php sessio…