当前位置:首页 > 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;
    }
}

创建链表类

链表类包含头节点以及基本的操作方法,如插入、删除和遍历。

php如何实现链表

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";
    }
}

使用链表

创建链表实例并执行插入、删除和遍历操作。

php如何实现链表

$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";
    }
}

使用双向链表

创建双向链表实例并执行插入、删除和遍历操作。

$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如何实现两栏布局

vue如何实现两栏布局

使用 Flexbox 实现两栏布局 通过 Flexbox 可以快速实现两栏布局,适合现代浏览器。在 Vue 的模板中,使用 display: flex 和 flex 属性控制两栏的宽度比例。 <…

vue如何实现分离

vue如何实现分离

Vue 实现组件分离的方法 Vue 可以通过多种方式实现组件分离,提升代码的可维护性和复用性。以下是几种常见的方法: 单文件组件 (SFC) Vue 的单文件组件(.vue 文件)将模板、脚本和样式…

vue如何实现目录组件

vue如何实现目录组件

实现目录组件的基本思路 在Vue中实现目录组件通常需要结合页面内容的结构化数据(如标题层级),通过动态渲染生成可交互的目录。核心步骤包括提取标题、生成目录结构、实现滚动联动等。 提取标题信息 通过d…

h5响应式布局如何实现

h5响应式布局如何实现

使用媒体查询(Media Queries) 通过CSS媒体查询针对不同屏幕尺寸设置不同的样式规则。例如: @media screen and (max-width: 768px) { .co…

如何实现语音react

如何实现语音react

语音识别基础设置 在React中实现语音识别功能通常需要借助浏览器的Web Speech API或第三方库。Web Speech API提供了SpeechRecognition接口,允许应用程序直接访…

java如何实现多线程

java如何实现多线程

实现多线程的方法 在Java中,实现多线程主要有两种方式:继承Thread类和实现Runnable接口。此外,还可以使用ExecutorService等高级工具类来管理线程。 继承Thread类…