当前位置:首页 > PHP

双向链表php实现

2026-02-15 17:56:13PHP

以下是一个用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实现

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

相关文章

vue实现双向

vue实现双向

Vue 实现双向绑定的方法 Vue 的双向绑定主要通过 v-model 指令实现,它结合了数据绑定和事件监听,适用于表单元素(如 input、select、textarea 等)。以下是几种常见的实现…

链表实现js

链表实现js

链表的基本概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和一个指向下一个节点的指针(单向链表)或两个指针(双向链表)。与数组不同,链表在内存中非连续存储,插入和删除操作更高效,但随机…

php实现双向队列

php实现双向队列

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

vue实现数据双向

vue实现数据双向

在Vue中实现数据双向绑定主要依靠v-model指令,它结合了属性绑定和事件监听,适用于表单元素或自定义组件。以下是具体实现方式: 基础表单元素的双向绑定 对于原生表单元素(如input、texta…

vue怎么实现双向

vue怎么实现双向

Vue 双向绑定的实现方式 Vue 中实现双向绑定的核心机制是 v-model 指令,它结合了属性绑定和事件监听,适用于表单元素或自定义组件。 表单元素的双向绑定 在原生表单元素(如 input、s…

vue双向数据实现

vue双向数据实现

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