当前位置:首页 > PHP

php如何实现链表

2026-01-29 02:22:27PHP

实现链表的基本结构

在PHP中,链表可以通过类和对象来实现。每个节点是一个对象,包含数据和指向下一个节点的引用。

class ListNode {
    public $data;
    public $next;

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

创建链表类

链表类包含头节点以及基本的操作方法,如插入、删除和遍历。

class LinkedList {
    public $head;

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

    // 在链表头部插入节点
    public function insertAtHead($data) {
        $newNode = new ListNode($data);
        $newNode->next = $this->head;
        $this->head = $newNode;
    }

    // 在链表尾部插入节点
    public function insertAtTail($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 deleteNode($data) {
        if ($this->head === null) {
            return;
        }
        if ($this->head->data === $data) {
            $this->head = $this->head->next;
            return;
        }
        $current = $this->head;
        while ($current->next !== null && $current->next->data !== $data) {
            $current = $current->next;
        }
        if ($current->next !== null) {
            $current->next = $current->next->next;
        }
    }

    // 遍历链表并打印数据
    public function printList() {
        $current = $this->head;
        while ($current !== null) {
            echo $current->data . " ";
            $current = $current->next;
        }
        echo "\n";
    }
}

使用链表

创建链表实例并执行插入、删除和遍历操作。

$list = new LinkedList();
$list->insertAtHead(1);
$list->insertAtTail(2);
$list->insertAtTail(3);
$list->printList(); // 输出: 1 2 3

$list->deleteNode(2);
$list->printList(); // 输出: 1 3

实现双向链表

双向链表的每个节点包含指向前一个和后一个节点的引用。

class DoublyListNode {
    public $data;
    public $next;
    public $prev;

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

class DoublyLinkedList {
    public $head;
    public $tail;

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

    // 在头部插入节点
    public function insertAtHead($data) {
        $newNode = new DoublyListNode($data);
        if ($this->head === null) {
            $this->head = $newNode;
            $this->tail = $newNode;
            return;
        }
        $newNode->next = $this->head;
        $this->head->prev = $newNode;
        $this->head = $newNode;
    }

    // 在尾部插入节点
    public function insertAtTail($data) {
        $newNode = new DoublyListNode($data);
        if ($this->tail === null) {
            $this->head = $newNode;
            $this->tail = $newNode;
            return;
        }
        $newNode->prev = $this->tail;
        $this->tail->next = $newNode;
        $this->tail = $newNode;
    }

    // 删除指定数据的节点
    public function deleteNode($data) {
        $current = $this->head;
        while ($current !== null && $current->data !== $data) {
            $current = $current->next;
        }
        if ($current === null) {
            return;
        }
        if ($current->prev !== null) {
            $current->prev->next = $current->next;
        } else {
            $this->head = $current->next;
        }
        if ($current->next !== null) {
            $current->next->prev = $current->prev;
        } else {
            $this->tail = $current->prev;
        }
    }

    // 遍历链表并打印数据
    public function printList() {
        $current = $this->head;
        while ($current !== null) {
            echo $current->data . " ";
            $current = $current->next;
        }
        echo "\n";
    }
}

使用双向链表

创建双向链表实例并执行插入、删除和遍历操作。

php如何实现链表

$dlist = new DoublyLinkedList();
$dlist->insertAtHead(1);
$dlist->insertAtTail(2);
$dlist->insertAtTail(3);
$dlist->printList(); // 输出: 1 2 3

$dlist->deleteNode(2);
$dlist->printList(); // 输出: 1 3

分享给朋友:

相关文章

vue如何实现排序

vue如何实现排序

实现数组排序 在Vue中可以通过计算属性或方法对数组进行排序。使用JavaScript的sort()方法结合Vue的响应式特性实现动态排序。 data() { return { item…

vue如何实现图

vue如何实现图

Vue 实现图表的方法 Vue 中实现图表可以通过多种方式,常见的有使用第三方库或自定义实现。以下是几种常见的方法: 使用 ECharts ECharts 是一个功能强大的图表库,支持多种图表类型。…

vue如何实现轮播

vue如何实现轮播

使用 Vue 实现轮播 使用第三方库(推荐) Vue 生态中有许多成熟的轮播组件库,例如 vue-awesome-swiper 或 swiper。以下是使用 vue-awesome-swiper 的示…

react如何实现菜单

react如何实现菜单

使用 React 实现菜单的方法 使用状态管理控制菜单显示/隐藏 通过 useState 管理菜单的展开和收起状态,结合点击事件切换状态。 import { useState } from 'rea…

vue如何实现渲染

vue如何实现渲染

Vue 渲染实现方法 Vue 的渲染主要通过模板编译、虚拟 DOM 和响应式系统实现。以下是具体实现方式: 模板编译 Vue 模板会被编译成渲染函数。模板可以写在 .vue 文件的 <temp…

vue如何实现replaceAll

vue如何实现replaceAll

实现字符串全局替换的方法 在Vue中实现类似JavaScript的replaceAll功能,可以通过以下几种方式: 使用String.prototype.replaceAll()(ES2021+)…