当前位置:首页 > 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底层如何实现

vue底层如何实现

Vue 底层的核心实现机制 Vue 的底层实现主要围绕响应式系统、虚拟 DOM 和组件化架构展开。以下是关键技术的详细解析: 响应式系统 依赖追踪与发布订阅模式 通过 Object.definePr…

vue如何实现放大缩小

vue如何实现放大缩小

Vue 实现放大缩小功能 在 Vue 中实现放大缩小功能可以通过多种方式实现,以下介绍几种常见的方法: 使用 CSS transform 缩放 通过绑定 CSS 的 transform: scal…

vue自动登录如何实现

vue自动登录如何实现

实现自动登录的基本思路 自动登录通常通过结合本地存储(如localStorage或cookie)和token验证机制实现。用户首次登录成功后,服务器返回的认证token会被保存在客户端,下次打开应用时…

vue如何实现排序

vue如何实现排序

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

vue如何实现滚动

vue如何实现滚动

Vue 实现滚动的方法 使用原生滚动 在Vue中可以直接使用HTML原生滚动,通过CSS设置overflow: auto或overflow: scroll来实现滚动效果。适用于简单场景。 <…

vue如何实现高亮

vue如何实现高亮

实现文本高亮的方法 在Vue中实现文本高亮通常可以通过以下几种方式完成: 使用v-html指令结合字符串替换 通过将需要高亮的文本部分替换为HTML标签(如<span class="high…