当前位置:首页 > PHP

php实现环形链表

2026-02-16 04:35:44PHP

环形链表的实现原理

环形链表是一种特殊的链表数据结构,其最后一个节点的指针不是指向null,而是指向链表中的某个节点(通常是头节点),形成一个闭环。这种结构常用于实现循环队列、轮询调度等场景。

PHP实现环形链表的基本结构

class ListNode {
    public $data;
    public $next;

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

class CircularLinkedList {
    private $head;

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

插入节点到环形链表

public function insert($data) {
    $newNode = new ListNode($data);

    if ($this->head === null) {
        $this->head = $newNode;
        $newNode->next = $this->head;
        return;
    }

    $current = $this->head;
    while ($current->next !== $this->head) {
        $current = $current->next;
    }

    $current->next = $newNode;
    $newNode->next = $this->head;
}

删除环形链表中的节点

public function delete($key) {
    if ($this->head === null) {
        return;
    }

    $current = $this->head;
    $prev = null;

    do {
        if ($current->data === $key) {
            if ($prev !== null) {
                $prev->next = $current->next;
                if ($current === $this->head) {
                    $this->head = $current->next;
                }
            } else {
                if ($current->next === $this->head) {
                    $this->head = null;
                } else {
                    $prev = $this->head;
                    while ($prev->next !== $this->head) {
                        $prev = $prev->next;
                    }
                    $this->head = $current->next;
                    $prev->next = $this->head;
                }
            }
            return;
        }
        $prev = $current;
        $current = $current->next;
    } while ($current !== $this->head);
}

遍历环形链表

public function display() {
    if ($this->head === null) {
        echo "List is empty\n";
        return;
    }

    $current = $this->head;
    do {
        echo $current->data . " ";
        $current = $current->next;
    } while ($current !== $this->head);
    echo "\n";
}

检测环形链表

public function isCircular() {
    if ($this->head === null) {
        return true;
    }

    $slow = $this->head;
    $fast = $this->head;

    while ($fast !== null && $fast->next !== null) {
        $slow = $slow->next;
        $fast = $fast->next->next;

        if ($slow === $fast) {
            return true;
        }
    }
    return false;
}

使用示例

$list = new CircularLinkedList();
$list->insert(1);
$list->insert(2);
$list->insert(3);
$list->display(); // 输出: 1 2 3

$list->delete(2);
$list->display(); // 输出: 1 3

echo $list->isCircular() ? "是环形链表" : "不是环形链表";

实现注意事项

环形链表的实现需要注意边界条件处理,特别是空链表和单节点链表的情况。所有操作都应维护环形结构,确保最后一个节点始终指向头节点。删除操作需要特别处理头节点的删除情况。

php实现环形链表

对于大规模数据操作,环形链表的遍历操作需要谨慎,避免无限循环。可以使用计数器或设置最大迭代次数来防止这种情况发生。

标签: 环形链表
分享给朋友:

相关文章

vue实现环形进度组件

vue实现环形进度组件

实现环形进度组件 环形进度组件可以通过SVG或Canvas实现,以下是基于SVG的实现方法: 定义组件模板 <template> <div class="circle-pro…

js实现单向链表

js实现单向链表

单向链表的基本概念 单向链表是一种线性数据结构,由多个节点组成,每个节点包含数据和指向下一个节点的指针。与数组不同,链表中的元素在内存中不是连续存储的,而是通过指针链接起来。 实现单向链表的步骤 定…

css制作环形

css制作环形

使用CSS制作环形 制作环形有多种方法,以下是几种常见且实用的CSS实现方式: 使用border-radius和border属性 通过设置元素的border-radius为50%使其变为圆形,再通过…

php 链表实现

php 链表实现

PHP 链表实现方法 链表是一种线性数据结构,由节点组成,每个节点包含数据和指向下一个节点的指针。以下是PHP中实现链表的方法。 定义链表节点类 链表的基本单位是节点,每个节点包含数据和指向下一个节…

php链表实现

php链表实现

PHP 链表实现方法 链表是一种常见的数据结构,由一系列节点组成,每个节点包含数据和指向下一个节点的指针。以下是 PHP 中实现链表的几种方法。 定义链表节点类 链表的基本单元是节点,通常包含数据和…

php实现环形队列

php实现环形队列

PHP实现环形队列的方法 环形队列是一种特殊的队列数据结构,它通过固定大小的数组实现,利用模运算实现循环利用空间。以下是PHP实现环形队列的几种方法: 使用数组和指针实现 通过维护队首(front)…