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

vue如何实现select

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

vue如何实现加减

vue如何实现加减

在 Vue 中实现加减功能 使用数据绑定和事件处理 通过 Vue 的数据绑定和事件处理机制可以轻松实现加减功能。定义一个数据变量存储数值,通过方法增减该变量。 <template>…

react如何实现keepalive

react如何实现keepalive

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

vue如何实现渲染

vue如何实现渲染

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

vue如何实现分页

vue如何实现分页

实现分页的基本思路 在Vue中实现分页通常需要结合后端API返回的分页数据和前端的分页组件。前端需要处理页码切换、数据请求和渲染逻辑。 后端API分页参数 后端API通常需要接收分页参数,例如pag…

vue如何实现发票

vue如何实现发票

在Vue中实现发票功能通常需要结合前端展示、数据绑定和打印功能。以下是实现发票功能的关键步骤和代码示例: 发票模板设计 使用Vue的模板语法设计发票的HTML结构,结合CSS美化样式。发票模板通常包…