当前位置:首页 > PHP

php 链表实现

2026-01-15 12:20:44PHP

在 PHP 中实现链表可以通过类来模拟链表节点(Node)和链表(LinkedList)结构。以下是链表的完整实现示例,包含基础操作如插入、删除、遍历等。

链表节点类(Node)

链表的基本单元是节点,每个节点包含数据(data)和指向下一个节点的指针(next)。

class Node {
    public $data;
    public $next;

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

链表类(LinkedList)

链表类管理节点的增删查改操作,并维护头节点(head)。

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

扩展操作

  1. 查找节点是否存在

    public function search($data) {
     $current = $this->head;
     while ($current !== null) {
         if ($current->data === $data) {
             return true;
         }
         $current = $current->next;
     }
     return false;
    }
  2. 反转链表

    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 中链表是模拟实现,实际内存管理由语言自动处理。
  • 双向链表可在节点类中增加 prev 指针,并调整插入/删除逻辑。

php 链表实现

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

相关文章

php实现文件下载

php实现文件下载

PHP 实现文件下载的方法 使用 header() 函数强制下载 设置正确的 HTTP 头信息,强制浏览器下载文件而不是直接打开。 $file = 'path/to/file.pdf'; if (…

php实现分页

php实现分页

PHP实现分页的基本方法 使用LIMIT和OFFSET进行数据库查询分页。通过计算当前页码和每页显示数量,动态生成SQL语句。 $page = isset($_GET['page']) ? (int…

php实现购物车

php实现购物车

PHP实现购物车功能 购物车功能是电商网站的核心模块之一,通常涉及商品添加、删除、数量修改和结算等操作。以下是使用PHP实现购物车功能的几种方法。 使用Session存储购物车数据 Session是…

php实现文件下载

php实现文件下载

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

php实现搜索功能

php实现搜索功能

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

php实现验证码

php实现验证码

PHP实现验证码的方法 使用GD库生成验证码 GD库是PHP中处理图像的扩展,可用于生成验证码图片。 <?php session_start(); $width = 120; $height…