当前位置:首页 > PHP

php如何实现链表

2026-02-14 00:32:51PHP

PHP实现链表的方法

在PHP中实现链表可以通过类和对象的方式完成。链表由节点组成,每个节点包含数据和指向下一个节点的引用。以下是单向链表和双向链表的实现示例。

单向链表的实现

单向链表每个节点只有一个指向下一个节点的指针。

class ListNode {
    public $data = null;
    public $next = null;

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

class LinkedList {
    private $head = null;

    public function insert($data) {
        $newNode = new ListNode($data);
        if ($this->head === null) {
            $this->head = $newNode;
        } else {
            $current = $this->head;
            while ($current->next !== null) {
                $current = $current->next;
            }
            $current->next = $newNode;
        }
    }

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

$list = new LinkedList();
$list->insert(10);
$list->insert(20);
$list->insert(30);
$list->display(); // 输出: 10 20 30

双向链表的实现

双向链表每个节点有两个指针,分别指向前一个和后一个节点。

php如何实现链表

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

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

class DoublyLinkedList {
    private $head = null;
    private $tail = null;

    public function insert($data) {
        $newNode = new DoublyListNode($data);
        if ($this->head === null) {
            $this->head = $newNode;
            $this->tail = $newNode;
        } else {
            $newNode->prev = $this->tail;
            $this->tail->next = $newNode;
            $this->tail = $newNode;
        }
    }

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

    public function displayBackward() {
        $current = $this->tail;
        while ($current !== null) {
            echo $current->data . " ";
            $current = $current->prev;
        }
    }
}

$dList = new DoublyLinkedList();
$dList->insert(10);
$dList->insert(20);
$dList->insert(30);
$dList->displayForward(); // 输出: 10 20 30
$dList->displayBackward(); // 输出: 30 20 10

链表操作的常见方法

链表支持多种操作,如插入、删除、查找等。以下是几种常见操作的实现。

在链表头部插入节点

php如何实现链表

public function insertAtHead($data) {
    $newNode = new ListNode($data);
    $newNode->next = $this->head;
    $this->head = $newNode;
}

删除指定数据的节点

public function delete($data) {
    if ($this->head === null) {
        return;
    }
    if ($this->head->data === $data) {
        $this->head = $this->head->next;
        return;
    }
    $current = $this->head;
    while ($current->next !== null) {
        if ($current->next->data === $data) {
            $current->next = $current->next->next;
            return;
        }
        $current = $current->next;
    }
}

查找节点是否存在

public function search($data) {
    $current = $this->head;
    while ($current !== null) {
        if ($current->data === $data) {
            return true;
        }
        $current = $current->next;
    }
    return false;
}

链表的应用场景

链表适用于动态数据结构的场景,如实现队列、栈或哈希表的冲突解决。其优势在于插入和删除操作的高效性,时间复杂度为O(1)(在已知位置的情况下)。

分享给朋友:

相关文章

vue如何实现select

vue如何实现select

Vue 实现 Select 组件的方法 在 Vue 中实现 Select 组件可以通过原生 HTML <select> 元素或使用第三方 UI 库(如 Element UI、Ant Des…

react如何实现keepalive

react如何实现keepalive

React 实现 KeepAlive 的方法 React 本身没有内置的 KeepAlive 组件,但可以通过以下方式模拟类似功能,实现组件状态保持或避免重复渲染。 使用 CSS 隐藏组件 通过 C…

java如何实现单点登录

java如何实现单点登录

单点登录(SSO)的基本概念 单点登录是一种用户认证机制,允许用户通过一次登录访问多个相互信任的应用系统。核心原理是通过共享认证状态(如Token或Cookie)实现跨系统身份验证。 基于Token…

vue如何实现渲染

vue如何实现渲染

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

vue如何实现跳转

vue如何实现跳转

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转可以通过以下几种方式,具体取决于项目结构和需求。 使用 router-link 组件 router-link 是 Vue Router 提供的组…

vue 分页如何实现

vue 分页如何实现

实现 Vue 分页的几种方法 使用 Element UI 分页组件 Element UI 提供了现成的分页组件 el-pagination,适合快速集成。安装 Element UI 后,直接引入组件并…