当前位置:首页 > 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-p…

vue实现立体环形图

vue实现立体环形图

实现立体环形图的方法 在Vue中实现立体环形图,可以结合ECharts或D3.js等可视化库。以下是使用ECharts的实现方法: 安装ECharts 通过npm或yarn安装ECharts: n…

css制作环形

css制作环形

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

css环形图制作

css环形图制作

使用CSS制作环形图 CSS环形图可以通过多种方法实现,包括使用border-radius、conic-gradient以及SVG等技术。以下是几种常见的方法: 使用border-radius和伪元…

php实现链表

php实现链表

链表的基本概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和指向下一个节点的指针。与数组不同,链表在内存中是非连续存储的,动态增删节点更高效。 单链表实现 PHP中可以通过类实现…

php链表实现

php链表实现

PHP 链表实现 链表是一种线性数据结构,由节点组成,每个节点包含数据和指向下一个节点的指针。在 PHP 中,可以通过类来实现链表。 链表节点类 链表的基本单位是节点,通常包含数据(data)和指…