当前位置:首页 > PHP

双向链表php实现

2026-02-15 17:56:13PHP

以下是一个用PHP实现双向链表的代码示例,包含节点类和链表类的基本操作:

双向链表php实现

双向链表节点类

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

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

双向链表类实现

class DoublyLinkedList {
    private $head;
    private $tail;
    private $length;

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

    // 在链表头部插入节点
    public function prepend($data) {
        $newNode = new ListNode($data);

        if ($this->head === null) {
            $this->head = $newNode;
            $this->tail = $newNode;
        } else {
            $newNode->next = $this->head;
            $this->head->prev = $newNode;
            $this->head = $newNode;
        }

        $this->length++;
    }

    // 在链表尾部插入节点
    public function append($data) {
        $newNode = new ListNode($data);

        if ($this->tail === null) {
            $this->head = $newNode;
            $this->tail = $newNode;
        } else {
            $newNode->prev = $this->tail;
            $this->tail->next = $newNode;
            $this->tail = $newNode;
        }

        $this->length++;
    }

    // 在指定位置插入节点
    public function insertAt($position, $data) {
        if ($position < 0 || $position > $this->length) {
            throw new OutOfBoundsException("Invalid position");
        }

        if ($position === 0) {
            $this->prepend($data);
            return;
        }

        if ($position === $this->length) {
            $this->append($data);
            return;
        }

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

        $newNode = new ListNode($data);
        $newNode->prev = $current;
        $newNode->next = $current->next;
        $current->next->prev = $newNode;
        $current->next = $newNode;

        $this->length++;
    }

    // 删除头部节点
    public function deleteFirst() {
        if ($this->head === null) {
            return null;
        }

        $deletedNode = $this->head;

        if ($this->head->next !== null) {
            $this->head = $this->head->next;
            $this->head->prev = null;
        } else {
            $this->head = null;
            $this->tail = null;
        }

        $this->length--;
        return $deletedNode->data;
    }

    // 删除尾部节点
    public function deleteLast() {
        if ($this->tail === null) {
            return null;
        }

        $deletedNode = $this->tail;

        if ($this->tail->prev !== null) {
            $this->tail = $this->tail->prev;
            $this->tail->next = null;
        } else {
            $this->head = null;
            $this->tail = null;
        }

        $this->length--;
        return $deletedNode->data;
    }

    // 删除指定位置的节点
    public function deleteAt($position) {
        if ($position < 0 || $position >= $this->length) {
            throw new OutOfBoundsException("Invalid position");
        }

        if ($position === 0) {
            return $this->deleteFirst();
        }

        if ($position === $this->length - 1) {
            return $this->deleteLast();
        }

        $current = $this->head;
        for ($i = 0; $i < $position; $i++) {
            $current = $current->next;
        }

        $current->prev->next = $current->next;
        $current->next->prev = $current->prev;

        $this->length--;
        return $current->data;
    }

    // 获取链表长度
    public function getLength() {
        return $this->length;
    }

    // 正向遍历链表
    public function traverseForward() {
        $current = $this->head;
        while ($current !== null) {
            echo $current->data . " ";
            $current = $current->next;
        }
        echo "\n";
    }

    // 反向遍历链表
    public function traverseBackward() {
        $current = $this->tail;
        while ($current !== null) {
            echo $current->data . " ";
            $current = $current->prev;
        }
        echo "\n";
    }
}

使用示例

$list = new DoublyLinkedList();
$list->append(1);
$list->append(2);
$list->append(3);
$list->prepend(0);
$list->insertAt(2, 1.5);

echo "Forward traversal: ";
$list->traverseForward(); // 输出: 0 1 1.5 2 3

echo "Backward traversal: ";
$list->traverseBackward(); // 输出: 3 2 1.5 1 0

$list->deleteAt(2);
echo "After deletion: ";
$list->traverseForward(); // 输出: 0 1 2 3

这个实现包含了双向链表的基本操作,包括在头部/尾部插入节点、在指定位置插入节点、删除节点以及正向/反向遍历链表。

标签: 双向链表
分享给朋友:

相关文章

php 实现单链表

php 实现单链表

单链表的基本概念 单链表是一种线性数据结构,由节点组成,每个节点包含数据域和指向下一个节点的指针域。链表的头节点是访问整个链表的入口。 单链表的节点类实现 在PHP中,可以通过类来定义链表节点。每个…

php 链表实现

php 链表实现

在 PHP 中实现链表可以通过类来模拟链表节点(Node)和链表(LinkedList)结构。以下是链表的完整实现示例,包含基础操作如插入、删除、遍历等。 链表节点类(Node) 链表的基本单元是节…

php实现双向队列

php实现双向队列

PHP 实现双向队列的方法 双向队列(Deque,Double-ended Queue)是一种允许在队列两端进行插入和删除操作的数据结构。PHP 中可以通过数组或 SplDoublyLinkedLis…

vue双向绑定实现

vue双向绑定实现

Vue 双向绑定实现原理 Vue 的双向绑定主要通过 v-model 指令实现,其核心是结合数据劫持(Object.defineProperty 或 Proxy)和发布-订阅模式。 数据劫持 Vue…

vue实现数据双向绑定

vue实现数据双向绑定

Vue 数据双向绑定的实现原理 Vue 的数据双向绑定主要通过 v-model 指令实现,其核心是基于数据劫持和发布-订阅模式。以下是关键实现机制: 数据劫持(响应式原理) Vue 使用 Objec…

vue数据双向绑定实现

vue数据双向绑定实现

Vue 数据双向绑定的实现原理 Vue 的双向数据绑定主要通过 数据劫持 和 发布-订阅模式 实现,核心是 Object.defineProperty(Vue 2.x)或 Proxy(Vue 3.x)…