当前位置:首页 > 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;
    }
}

创建链表并添加节点:

$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

双向链表实现

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

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;
}

链表排序

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

php 链表实现

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

php实现分类

php实现分类

PHP实现分类的方法 数据库设计 创建分类表时,通常需要包含id、名称、父级id等字段。父级id用于实现多级分类结构。 CREATE TABLE categories ( id INT AU…

php实现接口

php实现接口

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

php实现推送消息推送消息

php实现推送消息推送消息

PHP 实现消息推送的方法 使用 WebSocket 实现实时推送 WebSocket 是一种在单个 TCP 连接上进行全双工通信的协议,适合实现实时消息推送。 安装 Ratchet 库(WebSo…

php登陆功能实现

php登陆功能实现

实现PHP登录功能 数据库准备 创建一个用户表存储登录信息,基本字段包括用户名、密码(需加密)等: CREATE TABLE users ( id INT AUTO_INCREMENT PR…

php 路由实现的

php 路由实现的

PHP 路由实现方法 基于原生 PHP 实现路由 创建一个简单的路由解析器,通过解析 URL 路径来调用对应的控制器或函数。 // 定义路由表 $routes = [ '/' => '…