当前位置:首页 > 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中实现搜索功能通常涉及数据库查询。以下是一个简单的实现方式,假设使用MySQL数据库: <?php // 连接数据库 $conn = new mysqli('loc…

php记住密码怎么实现

php记住密码怎么实现

使用Cookie实现记住密码功能 在PHP中,可以通过设置Cookie来记住用户的登录状态。以下是一个简单的实现方式: // 用户登录验证成功后 if ($login_success) {…

php长连接实现

php长连接实现

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

php 路由实现

php 路由实现

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

php 实现接口

php 实现接口

PHP 实现接口的方法 在 PHP 中,接口(Interface)用于定义一组方法的规范,而不包含具体的实现。类可以实现(implement)接口,并必须实现接口中定义的所有方法。 定义接口 接口通…

php 实现无限分类

php 实现无限分类

无限分类的实现方法 无限分类通常用于构建多层级结构的数据,如商品分类、菜单管理等。以下是几种常见的实现方式: 邻接列表模型(Adjacency List) 这是最简单的实现方式,每个分类记录存储其父…