当前位置:首页 > PHP

php实现链表操作

2026-02-15 06:04:56PHP

PHP实现链表操作

链表是一种常见的数据结构,通过节点和指针实现动态内存分配。PHP中可以通过类和对象模拟链表操作。

定义链表节点类

链表的基础是节点,每个节点包含数据和指向下一个节点的指针。

class ListNode {
    public $data;
    public $next;

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

创建链表

实例化节点对象并连接它们形成链表。

$node1 = new ListNode(1);
$node2 = new ListNode(2);
$node3 = new ListNode(3);

$node1->next = $node2;
$node2->next = $node3;

遍历链表

使用循环访问链表中的每个节点。

php实现链表操作

function traverseLinkedList($head) {
    $current = $head;
    while ($current != null) {
        echo $current->data . " ";
        $current = $current->next;
    }
}

插入节点

在链表指定位置插入新节点。

function insertNode(&$head, $data, $position) {
    $newNode = new ListNode($data);

    if ($position == 0) {
        $newNode->next = $head;
        $head = $newNode;
        return;
    }

    $current = $head;
    for ($i = 0; $i < $position - 1 && $current != null; $i++) {
        $current = $current->next;
    }

    if ($current == null) return;

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

删除节点

从链表中移除指定位置的节点。

php实现链表操作

function deleteNode(&$head, $position) {
    if ($head == null) return;

    if ($position == 0) {
        $head = $head->next;
        return;
    }

    $current = $head;
    for ($i = 0; $i < $position - 1 && $current != null; $i++) {
        $current = $current->next;
    }

    if ($current == null || $current->next == null) return;

    $current->next = $current->next->next;
}

反转链表

改变链表节点指针方向实现反转。

function reverseLinkedList(&$head) {
    $prev = null;
    $current = $head;

    while ($current != null) {
        $next = $current->next;
        $current->next = $prev;
        $prev = $current;
        $current = $next;
    }

    $head = $prev;
}

检测环

判断链表是否有环结构。

function hasCycle($head) {
    if ($head == null) return false;

    $slow = $head;
    $fast = $head;

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

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

    return false;
}

合并两个有序链表

将两个已排序链表合并为一个新链表。

function mergeTwoLists($l1, $l2) {
    $dummy = new ListNode(0);
    $current = $dummy;

    while ($l1 != null && $l2 != null) {
        if ($l1->data < $l2->data) {
            $current->next = $l1;
            $l1 = $l1->next;
        } else {
            $current->next = $l2;
            $l2 = $l2->next;
        }
        $current = $current->next;
    }

    $current->next = ($l1 != null) ? $l1 : $l2;

    return $dummy->next;
}

以上代码实现了链表的基本操作,包括创建、遍历、插入、删除、反转、检测环和合并等功能。可以根据实际需求调整或扩展这些方法。

标签: 链表操作
分享给朋友:

相关文章

vue操作日志实现

vue操作日志实现

Vue 操作日志实现方案 方案一:基于拦截器的前端日志记录 通过封装 Axios 请求拦截器和响应拦截器,自动记录用户操作。这种方式适合记录 API 调用相关的操作。 // 在 axios 拦截器…

vue实现操作权限控制

vue实现操作权限控制

Vue 权限控制实现方法 路由级权限控制 通过路由守卫拦截未授权访问,结合用户角色动态生成路由表。在路由配置中添加meta字段定义权限标识: // router.js const routes =…

vue项目操作如何实现

vue项目操作如何实现

Vue 项目基本操作实现 创建 Vue 项目 使用 Vue CLI 快速创建项目: npm install -g @vue/cli vue create project-name cd projec…

php实现链表

php实现链表

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

php如何实现链表

php如何实现链表

实现链表的基本结构 在PHP中,链表可以通过类和对象来实现。每个节点是一个对象,包含数据和指向下一个节点的引用。 class ListNode { public $data; pub…

php实现文件读写操作

php实现文件读写操作

文件读取操作 使用file_get_contents函数可以快速读取整个文件内容到一个字符串中。适合读取小文件,简单高效。 $content = file_get_contents('exampl…