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

删除节点操作

根据值删除节点需要处理头节点特殊情况:

php实现链表操作

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指针:

php实现链表操作

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

链表排序实现

归并排序适合链表排序:

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

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

相关文章

php如何实现链表

php如何实现链表

实现链表的基本结构 在PHP中,链表可以通过类和对象来实现。每个节点是一个对象,包含数据和指向下一个节点的引用。 class ListNode { public $data; pub…

js实现无序链表排序

js实现无序链表排序

实现无序链表排序的方法 在JavaScript中,可以使用多种方法对无序链表进行排序。以下是几种常见的实现方式,包括冒泡排序、归并排序和快速排序。 冒泡排序实现链表排序 冒泡排序是一种简单的排序算法…

js实现数据库操作

js实现数据库操作

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

js实现单向链表

js实现单向链表

单向链表的基本概念 单向链表是一种线性数据结构,由多个节点组成,每个节点包含数据和指向下一个节点的指针。与数组不同,链表中的元素在内存中不是连续存储的,而是通过指针链接起来。 实现单向链表的步骤 定…

js如何实现链表

js如何实现链表

实现链表的基础结构 在JavaScript中,链表可以通过对象(节点)和引用来实现。每个节点包含两个部分:存储的数据(value)和指向下一个节点的指针(next)。头节点(head)是链表的起点。…