当前位置:首页 > 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. 反转链表

    php 链表实现

    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实现分页功能需要结合数据库查询和HTML展示。以下是核心实现步骤: 数据库查询与分页逻辑 // 连接数据库 $conn = new mysqli('loca…

php实现socket

php实现socket

PHP 实现 Socket 通信 PHP 可以通过 socket 扩展实现网络通信,支持 TCP/UDP 协议。以下是实现 Socket 通信的核心步骤和代码示例。 创建 TCP 服务器 //…

php mvc实现

php mvc实现

PHP MVC 实现步骤 模型(Model)层 模型负责数据处理和业务逻辑,通常与数据库交互。创建一个基类模型,其他具体模型继承它。 class Model { protected $db;…

php实现短信验证

php实现短信验证

PHP实现短信验证的步骤 选择合适的短信服务提供商 国内常见的短信服务提供商包括阿里云短信、腾讯云短信、云片等。注册账号并获取API密钥和签名模板是第一步。这些平台通常提供详细的开发文档和SDK。…

php实现重定向

php实现重定向

使用header函数进行重定向 在PHP中,header()函数是实现重定向的常用方法。通过发送HTTP头信息Location,浏览器会自动跳转到指定URL。需确保在调用header()前没有输出任何…

php实现发帖

php实现发帖

PHP 实现发帖功能 要在 PHP 中实现发帖功能,通常需要结合数据库操作(如 MySQL)和前端表单提交。以下是实现发帖功能的关键步骤: 数据库表设计 创建一个帖子表(posts)存储发帖内容,基…