当前位置:首页 > PHP

php如何实现链表

2026-04-03 08:15:36PHP

PHP 实现链表的方法

基础链表节点类

链表由节点构成,每个节点包含数据和指向下一个节点的指针。以下是一个简单的节点类实现:

php如何实现链表

class ListNode {
    public $data;
    public $next;

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

单链表实现

单链表包含头节点和基本操作方法:

php如何实现链表

class LinkedList {
    private $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 search($data) {
        $current = $this->head;
        while ($current !== null) {
            if ($current->data === $data) {
                return true;
            }
            $current = $current->next;
        }
        return false;
    }

    // 打印链表
    public function printList() {
        $current = $this->head;
        while ($current !== null) {
            echo $current->data . " -> ";
            $current = $current->next;
        }
        echo "NULL\n";
    }
}

双向链表实现

双向链表节点包含指向前一个和后一个节点的指针:

class DoublyListNode {
    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;

    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;
        } else {
            $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;
        } else {
            $newNode->prev = $this->tail;
            $this->tail->next = $newNode;
            $this->tail = $newNode;
        }
    }

    // 删除节点
    public function deleteNode($data) {
        $current = $this->head;
        while ($current !== null) {
            if ($current->data === $data) {
                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;
                }
                return;
            }
            $current = $current->next;
        }
    }

    // 打印链表
    public function printList() {
        $current = $this->head;
        while ($current !== null) {
            echo $current->data . " <-> ";
            $current = $current->next;
        }
        echo "NULL\n";
    }
}

使用示例

// 单链表使用示例
$list = new LinkedList();
$list->insertAtHead(3);
$list->insertAtHead(2);
$list->insertAtHead(1);
$list->insertAtTail(4);
$list->printList();  // 输出: 1 -> 2 -> 3 -> 4 -> NULL

// 双向链表使用示例
$dlist = new DoublyLinkedList();
$dlist->insertAtHead(2);
$dlist->insertAtHead(1);
$dlist->insertAtTail(3);
$dlist->printList();  // 输出: 1 <-> 2 <-> 3 <-> NULL

以上代码展示了PHP中实现单链表和双向链表的基本方法,包括节点的插入、删除和遍历操作。

分享给朋友:

相关文章

vue如何实现id

vue如何实现id

在 Vue 中实现 ID 绑定 Vue 提供了多种方式为元素或组件绑定唯一的 ID,可以根据具体需求选择合适的方法。 方法一:使用 v-bind 绑定静态或动态 ID <template&g…

vue如何实现登录

vue如何实现登录

Vue 实现登录功能的方法 创建登录表单组件 在 Vue 项目中创建一个登录表单组件,通常命名为 Login.vue。表单包含用户名和密码输入框,以及提交按钮。 <template>…

vue如何实现tap

vue如何实现tap

Vue 中实现类似移动端 tap 事件的方法 在 Vue 中可以通过以下几种方式实现类似移动端 tap(轻触)事件的效果: 使用第三方库 安装 v-tap 指令库可以快速实现 tap 事件: np…

php 链表实现

php 链表实现

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

vue router如何实现

vue router如何实现

Vue Router 的实现方法 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是实现 Vue Router 的具体方法: 安装 Vue Router…

vue如何实现vmodel

vue如何实现vmodel

Vue 中实现 v-model 的方法 v-model 是 Vue 中用于实现表单元素和数据双向绑定的指令。其本质是语法糖,结合了 value 属性和 input 事件的封装。以下是实现 v-mode…