当前位置:首页 > PHP

php链表实现

2026-01-29 23:14:45PHP

PHP 链表实现

链表是一种线性数据结构,由节点组成,每个节点包含数据和指向下一个节点的指针。在 PHP 中,可以通过类来实现链表。

链表节点类

链表的基本单位是节点,通常包含数据(data)和指向下一个节点的指针(next)。

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 printList() {
        $current = $this->head;
        while ($current !== null) {
            echo $current->data . " -> ";
            $current = $current->next;
        }
        echo "NULL\n";
    }
}

使用链表

创建链表并执行插入、删除和打印操作。

$list = new LinkedList();
$list->insertAtHead(10);
$list->insertAtHead(20);
$list->insertAtTail(30);
$list->printList(); // 输出: 20 -> 10 -> 30 -> NULL

$list->deleteNode(10);
$list->printList(); // 输出: 20 -> 30 -> NULL

双向链表实现

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

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;
            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 printList() {
        $current = $this->head;
        while ($current !== null) {
            echo $current->data . " <-> ";
            $current = $current->next;
        }
        echo "NULL\n";
    }
}

使用双向链表

$dList = new DoublyLinkedList();
$dList->insertAtHead(10);
$dList->insertAtHead(20);
$dList->insertAtTail(30);
$dList->printList(); // 输出: 20 <-> 10 <-> 30 <-> NULL

通过以上代码,可以在 PHP 中实现单向链表和双向链表的基本功能。链表适用于动态数据存储和频繁插入删除操作的场景。

标签: 链表php
分享给朋友:

相关文章

php 实现队列

php 实现队列

PHP 实现队列的方法 PHP 可以通过多种方式实现队列功能,以下是几种常见的实现方法: 使用数组实现队列 PHP 数组可以通过 array_push 和 array_shift 函数模拟队列的先进…

php 实现评论

php 实现评论

实现 PHP 评论功能 数据库设计 创建数据库表存储评论数据,通常包含以下字段: id:评论唯一标识 user_id:用户ID content:评论内容 created_at:评论时间 parent…

php实现注册登陆

php实现注册登陆

PHP 注册功能实现 创建数据库表结构(MySQL示例): CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, usern…

php 购物车实现session

php 购物车实现session

PHP 购物车实现(基于 Session) 在 PHP 中,使用 Session 实现购物车功能是一种常见方法。Session 可以跨页面存储用户数据,适合临时保存购物车信息。 初始化 Sessio…

php session实现购物车

php session实现购物车

PHP Session 实现购物车 初始化 Session 确保在使用 Session 前调用 session_start() 函数。通常放在 PHP 文件的最顶部。 <?php sessio…

php实现扫码登录

php实现扫码登录

PHP 实现扫码登录的方案 扫码登录通常基于以下流程:用户访问网页生成二维码(含唯一标识),手机扫码后确认登录,服务器验证并完成登录状态同步。以下是具体实现方法: 生成二维码与唯一标识 使用 PHP…