当前位置:首页 > PHP

php实现链表操作

2026-01-30 08:16:32PHP

PHP实现链表基础结构

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

class ListNode {
    public $data;
    public $next;

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

链表类封装

创建链表类实现基本操作,初始化时包含头节点指针:

class LinkedList {
    private $head;

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

插入节点操作

头部插入法时间复杂度为O(1):

public function insertAtBeginning($data) {
    $newNode = new ListNode($data);
    $newNode->next = $this->head;
    $this->head = $newNode;
}

尾部插入需要遍历链表,时间复杂度O(n):

public function insertAtEnd($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($key) {
    if ($this->head === null) return;

    // 处理头节点删除
    if ($this->head->data === $key) {
        $this->head = $this->head->next;
        return;
    }

    $current = $this->head;
    while ($current->next !== null && $current->next->data !== $key) {
        $current = $current->next;
    }

    if ($current->next !== null) {
        $current->next = $current->next->next;
    }
}

查找与遍历

线性查找实现:

public function search($key) {
    $current = $this->head;
    while ($current !== null) {
        if ($current->data === $key) {
            return true;
        }
        $current = $current->next;
    }
    return false;
}

打印链表内容:

public function display() {
    $current = $this->head;
    while ($current !== null) {
        echo $current->data . " -> ";
        $current = $current->next;
    }
    echo "NULL\n";
}

双向链表实现

双向链表节点增加prev指针:

class DoublyListNode {
    public $data;
    public $next;
    public $prev;

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

双向链表插入需要维护前后节点指针:

public function insertAfter($prevNode, $data) {
    if ($prevNode === null) return;

    $newNode = new DoublyListNode($data);
    $newNode->next = $prevNode->next;
    $prevNode->next = $newNode;
    $newNode->prev = $prevNode;

    if ($newNode->next !== null) {
        $newNode->next->prev = $newNode;
    }
}

环形链表检测

使用快慢指针判断环形链表:

public function hasCycle() {
    if ($this->head === null) return false;

    $slow = $this->head;
    $fast = $this->head->next;

    while ($fast !== null && $fast->next !== null) {
        if ($slow === $fast) return true;
        $slow = $slow->next;
        $fast = $fast->next->next;
    }
    return false;
}

链表反转操作

迭代法反转链表:

public function reverse() {
    $prev = null;
    $current = $this->head;

    while ($current !== null) {
        $next = $current->next;
        $current->next = $prev;
        $prev = $current;
        $current = $next;
    }

    $this->head = $prev;
}

链表排序实现

归并排序适合链表排序:

php实现链表操作

public function sortList($head) {
    if ($head === null || $head->next === null) {
        return $head;
    }

    // 快慢指针找中点
    $slow = $head;
    $fast = $head->next;
    while ($fast !== null && $fast->next !== null) {
        $slow = $slow->next;
        $fast = $fast->next->next;
    }

    $mid = $slow->next;
    $slow->next = null;

    $left = $this->sortList($head);
    $right = $this->sortList($mid);

    return $this->merge($left, $right);
}

private function merge($left, $right) {
    $dummy = new ListNode(0);
    $current = $dummy;

    while ($left !== null && $right !== null) {
        if ($left->data < $right->data) {
            $current->next = $left;
            $left = $left->next;
        } else {
            $current->next = $right;
            $right = $right->next;
        }
        $current = $current->next;
    }

    $current->next = ($left !== null) ? $left : $right;

    return $dummy->next;
}

标签: 链表操作
分享给朋友:

相关文章

js 实现链表

js 实现链表

链表的基本概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和指向下一个节点的指针。与数组不同,链表在内存中是非连续存储的,插入和删除操作效率较高。 链表的实现 在 JavaScrip…

vue操作权限实现

vue操作权限实现

Vue 权限控制实现方案 Vue 中实现权限控制通常涉及前端路由、按钮/组件级权限、接口权限等多个层面。以下是常见的实现方法: 路由权限控制 通过动态路由实现,根据用户权限过滤可访问的路由: 定…

react如何操作节点数据

react如何操作节点数据

操作节点数据的常见方法 在React中操作DOM节点数据通常通过ref实现,以下是几种典型场景的解决方案: 使用useRef创建引用 通过useRef钩子创建引用并绑定到DOM元素,后续可直接操作节…

php链表实现

php链表实现

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

js实现数据库操作

js实现数据库操作

JavaScript 实现数据库操作的方法 JavaScript 可以通过多种方式操作数据库,包括浏览器端的本地存储、服务器端的数据库连接以及使用 ORM 框架等。以下是几种常见的实现方法: 浏览器…

js 链表实现

js 链表实现

链表基础概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据部分和指向下一个节点的指针。与数组不同,链表的内存分配不要求连续,插入和删除操作效率更高。 单向链表实现 以下是单向链表的基本…