当前位置:首页 > PHP

php实现环形链表

2026-02-16 04:35:44PHP

php实现环形链表

环形链表的实现原理

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

php实现环形链表

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() ? "是环形链表" : "不是环形链表";

实现注意事项

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

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

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

相关文章

css制作环形

css制作环形

使用 border-radius 和 border 属性 通过设置元素的 border-radius 为 50% 将其变为圆形,再通过 border 属性控制边框宽度和颜色。调整宽度和高度相同,边框透…

js实现链表

js实现链表

链表的基本概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和指向下一个节点的指针。与数组不同,链表在内存中不是连续存储的,插入和删除操作更高效,但访问元素需要遍历。 链表的实现 定义…

js 实现单链表实现栈

js 实现单链表实现栈

单链表实现栈的原理 栈是一种后进先出(LIFO)的数据结构,单链表实现栈的关键在于利用链表的头部作为栈顶,这样插入和删除操作的时间复杂度均为 O(1)。 单链表节点定义 class Node {…

js实现链表思路

js实现链表思路

实现链表的基本结构 链表由节点(Node)组成,每个节点包含两个部分: value:存储数据 next:指向下一个节点的引用(默认null) class Node { constru…

css制作环形

css制作环形

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

php 实现单链表

php 实现单链表

单链表的基本概念 单链表是一种线性数据结构,由一系列节点组成,每个节点包含数据域和指向下一个节点的指针域。最后一个节点的指针域通常指向 NULL,表示链表结束。 单链表的节点结构 在 PHP 中,单…