当前位置:首页 > PHP

php 链表实现

2026-02-27 13:32:05PHP

PHP 链表实现方法

链表是一种常见的数据结构,由节点组成,每个节点包含数据和指向下一个节点的指针。以下是几种PHP实现链表的方法。

基础链表实现

定义节点类,包含数据和指向下一个节点的指针:

class ListNode {
    public $data;
    public $next;

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

创建链表并添加节点:

php 链表实现

$head = new ListNode(1);
$second = new ListNode(2);
$third = new ListNode(3);

$head->next = $second;
$second->next = $third;

链表操作封装

将链表操作封装成类,便于管理:

class LinkedList {
    private $head;

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

    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 display() {
        $current = $this->head;
        while ($current !== null) {
            echo $current->data . " ";
            $current = $current->next;
        }
    }
}

$list = new LinkedList();
$list->insertAtEnd(10);
$list->insertAtEnd(20);
$list->display(); // 输出: 10 20

双向链表实现

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

php 链表实现

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

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

class DoublyLinkedList {
    private $head;

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

    public function insertAtEnd($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 reverse() {
    $prev = null;
    $current = $this->head;
    while ($current !== null) {
        $next = $current->next;
        $current->next = $prev;
        $prev = $current;
        $current = $next;
    }
    $this->head = $prev;
}

链表排序

使用归并排序对链表进行排序:

public function sortList($head) {
    if ($head === null || $head->next === null) {
        return $head;
    }
    $slow = $head;
    $fast = $head->next;
    while ($fast !== null && $fast->next !== null) {
        $slow = $slow->next;
        $fast = $fast->next->next;
    }
    $mid = $slow->next;
    $slow->next = null;
    $left = $this->sortList($head);
    $right = $this->sortList($mid);
    return $this->merge($left, $right);
}

private function merge($left, $right) {
    $dummy = new ListNode(0);
    $current = $dummy;
    while ($left !== null && $right !== null) {
        if ($left->data < $right->data) {
            $current->next = $left;
            $left = $left->next;
        } else {
            $current->next = $right;
            $right = $right->next;
        }
        $current = $current->next;
    }
    $current->next = $left !== null ? $left : $right;
    return $dummy->next;
}

这些方法涵盖了链表的基本操作和常见算法,可以根据需求选择适合的实现方式。

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

相关文章

php长连接实现

php长连接实现

PHP 长连接实现方法 长连接(Keep-Alive)允许客户端与服务器在一次TCP连接中发送和接收多个HTTP请求/响应,减少重复建立连接的开销。以下是PHP中实现长连接的几种方式: 配置HTTP…

php实现登录

php实现登录

PHP 登录功能实现 数据库连接与用户表创建 创建一个数据库连接文件 db.php,用于管理数据库连接。MySQL 示例代码如下: <?php $host = 'localhost'; $db…

php 路由实现

php 路由实现

PHP 路由实现方法 在 PHP 中实现路由功能有多种方式,以下是几种常见的实现方法: 使用原生 PHP 实现 通过解析 URL 并匹配对应的控制器和动作: $request = $_SERV…

php 登录实现

php 登录实现

PHP 登录实现 PHP 登录功能通常包括用户认证、会话管理和安全性措施。以下是实现 PHP 登录功能的关键步骤。 数据库准备 创建一个用户表存储用户信息,通常包括用户名、密码(加密存储)和其他必…

php实现接口

php实现接口

PHP 实现接口的方法 在 PHP 中,接口(Interface)用于定义一组方法的规范,而不实现具体功能。类可以实现一个或多个接口,并必须实现接口中定义的所有方法。 定义接口 接口使用 inter…

php实现静态

php实现静态

静态化实现方法 PHP实现静态化主要通过生成HTML文件或缓存机制来减少服务器动态解析的开销。以下是几种常见方法: 生成静态HTML文件 通过file_put_contents()将动态内容保存为H…