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

链表排序实现

归并排序适合链表排序:

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实现链表操作

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

相关文章

react 如何操作cookie

react 如何操作cookie

安装依赖 在React项目中操作cookie通常需要第三方库的支持,推荐使用js-cookie。通过npm或yarn安装: npm install js-cookie # 或 yarn add j…

php 链表实现

php 链表实现

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

php 实现链表

php 实现链表

PHP 实现链表的方法 链表是一种线性数据结构,由节点组成,每个节点包含数据和指向下一个节点的指针。PHP 中可以通过类和对象来实现链表。 定义链表节点类 创建一个 ListNode 类,用于表示链…

vue实现操作权限

vue实现操作权限

Vue 实现操作权限的方法 使用自定义指令 在 Vue 中可以通过自定义指令来实现操作权限控制。创建一个指令,检查用户是否有权限执行某个操作。 // 注册全局指令 Vue.directive('p…

vue实现操作执行功能

vue实现操作执行功能

Vue 实现操作执行功能的方法 使用 methods 定义方法 在 Vue 组件的 methods 选项中定义需要执行的操作方法。这些方法可以绑定到模板中的事件或直接调用。 export defa…

vue项目操作如何实现

vue项目操作如何实现

Vue 项目基本操作实现 创建 Vue 项目 使用 Vue CLI 快速创建项目: npm install -g @vue/cli vue create project-name cd projec…