php双向链怎么用

worktile 其他 96

回复

共3条回复 我来回复
  • worktile的头像
    worktile
    Worktile官方账号
    评论

    双向链表是一种常见的数据结构,它在计算机科学中有广泛的应用。它可以在使用中灵活地插入和删除节点,同时具有正向和反向遍历的能力。在PHP中,我们可以使用类来实现双向链表。

    首先,我们需要定义一个节点类,该类包含三个属性:前一个节点的引用(prev)、后一个节点的引用(next)和节点的值(value)。
    “`php
    class Node {
    public $prev;
    public $next;
    public $value;

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

    然后,我们再定义一个双向链表类,该类可以用来操作节点。在双向链表类中,我们需要记录链表的头节点和尾节点。
    “`php
    class DoublyLinkedList {
    private $head;
    private $tail;
    private $length;

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

    public function append($value) {
    $newNode = new Node($value);

    if ($this->length === 0) {
    $this->head = $newNode;
    $this->tail = $newNode;
    } else {
    $newNode->prev = $this->tail;
    $this->tail->next = $newNode;
    $this->tail = $newNode;
    }

    $this->length++;
    }

    // 在指定位置插入节点
    public function insert($position, $value) {
    if ($position < 0 || $position > $this->length) {
    return false;
    }

    $newNode = new Node($value);

    if ($position === 0) {
    if ($this->head === null) {
    $this->head = $newNode;
    $this->tail = $newNode;
    } else {
    $newNode->next = $this->head;
    $this->head->prev = $newNode;
    $this->head = $newNode;
    }
    } else if ($position === $this->length) {
    $newNode->prev = $this->tail;
    $this->tail->next = $newNode;
    $this->tail = $newNode;
    } else {
    $current = $this->head;
    $index = 0;

    while ($index < $position) { $current = $current->next;
    $index++;
    }

    $newNode->prev = $current->prev;
    $current->prev->next = $newNode;
    $newNode->next = $current;
    $current->prev = $newNode;
    }

    $this->length++;
    return true;
    }

    // 删除指定位置的节点
    public function remove($position) {
    if ($position < 0 || $position >= $this->length) {
    return null;
    }

    $current = $this->head;

    if ($position === 0) {
    if ($this->length === 1) {
    $this->head = null;
    $this->tail = null;
    } else {
    $this->head = $current->next;
    $this->head->prev = null;
    }
    } else if ($position === $this->length – 1) {
    $current = $this->tail;
    $this->tail = $current->prev;
    $this->tail->next = null;
    } else {
    $index = 0;

    while ($index < $position) { $current = $current->next;
    $index++;
    }

    $current->prev->next = $current->next;
    $current->next->prev = $current->prev;
    }

    $this->length–;
    return $current->value;
    }

    // 正向遍历链表
    public function forwardTraverse() {
    $current = $this->head;

    while ($current !== null) {
    echo $current->value . ‘ ‘;
    $current = $current->next;
    }

    echo “\n”;
    }

    // 反向遍历链表
    public function backwardTraverse() {
    $current = $this->tail;

    while ($current !== null) {
    echo $current->value . ‘ ‘;
    $current = $current->prev;
    }

    echo “\n”;
    }
    }
    “`

    现在,我们可以使用双向链表类来创建一个双向链表,并进行一些操作。
    “`php
    $dll = new DoublyLinkedList();
    $dll->append(1);
    $dll->append(2);
    $dll->append(3);
    $dll->append(4);

    $dll->forwardTraverse(); // 正向遍历链表,输出:1 2 3 4
    $dll->backwardTraverse(); // 反向遍历链表,输出:4 3 2 1

    $dll->insert(2, 5); // 在位置2插入节点5
    $dll->forwardTraverse(); // 正向遍历链表,输出:1 2 5 3 4

    $dll->remove(3); // 移除位置3的节点
    $dll->forwardTraverse(); // 正向遍历链表,输出:1 2 5 4
    “`

    以上就是使用PHP实现双向链表的一种方法。通过双向链表,我们可以方便地进行节点的插入、删除,并且可以灵活地正向和反向遍历链表。希望对你有所帮助。

    2年前 0条评论
  • fiy的头像
    fiy
    Worktile&PingCode市场小伙伴
    评论

    双向链表(Doubly Linked List)是一种常见的数据结构,与单向链表相比,它在节点中存储了指向前一个节点和后一个节点的引用。这样的设计使得双向链表可以从两个方向遍历,提供了更强的灵活性和功能。下面将介绍如何使用PHP实现双向链表,并讨论双向链表的使用场景和一些常见操作。

    1. 实现双向链表的节点类

    首先,我们需要创建一个双向链表的节点类,该类包含了存储数据和前后指针的属性。在PHP中,可以使用类的属性来实现这些功能。

    “`php
    class Node {
    public $data;
    public $previous;
    public $next;

    function __construct($data) {
    $this->data = $data;
    $this->previous = null;
    $this->next = null;
    }
    }
    “`

    2. 创建双向链表类

    接下来,我们需要创建一个双向链表类,用于管理节点和执行相关操作。该类包含了头节点和尾节点的引用,并提供了一系列的方法来插入、删除和遍历节点。

    “`php
    class DoublyLinkedList {
    public $head;
    public $tail;

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

    // 插入节点到链表头部
    function insertFirst($data) {
    $newNode = new Node($data);
    if ($this->head == null) {
    $this->head = $newNode;
    $this->tail = $newNode;
    } else {
    $newNode->next = $this->head;
    $this->head->previous = $newNode;
    $this->head = $newNode;
    }
    }

    // 插入节点到链表尾部
    function insertLast($data) {
    $newNode = new Node($data);
    if ($this->tail == null) {
    $this->head = $newNode;
    $this->tail = $newNode;
    } else {
    $newNode->previous = $this->tail;
    $this->tail->next = $newNode;
    $this->tail = $newNode;
    }
    }

    // 删除链表头部节点
    function deleteFirst() {
    if ($this->head == null) {
    return;
    }
    $deletedNode = $this->head;
    if ($this->head == $this->tail) {
    $this->head = null;
    $this->tail = null;
    } else {
    $this->head = $this->head->next;
    $this->head->previous = null;
    }
    unset($deletedNode);
    }

    // 删除链表尾部节点
    function deleteLast() {
    if ($this->tail == null) {
    return;
    }
    $deletedNode = $this->tail;
    if ($this->head == $this->tail) {
    $this->head = null;
    $this->tail = null;
    } else {
    $this->tail = $this->tail->previous;
    $this->tail->next = null;
    }
    unset($deletedNode);
    }

    // 遍历链表
    function display() {
    $currentNode = $this->head;
    while ($currentNode != null) {
    echo $currentNode->data . ” “;
    $currentNode = $currentNode->next;
    }
    echo “\n”;
    }
    }
    “`

    3. 使用双向链表

    通过上面的代码,我们已经成功地实现了一个双向链表类。现在我们可以使用这个类来操作双向链表。

    “`php
    $linkedlist = new DoublyLinkedList();

    // 插入节点到链表尾部
    $linkedlist->insertLast(1);
    $linkedlist->insertLast(2);
    $linkedlist->insertLast(3);

    // 删除链表头部节点
    $linkedlist->deleteFirst();

    // 遍历链表
    $linkedlist->display();
    “`

    运行上面的代码,输出结果为”2 3″,说明我们成功地删除了链表的头部节点并遍历了链表。

    4. 双向链表的使用场景

    双向链表在一些特定的场景中非常有用。由于它具有从前到后和从后到前两个方向遍历的能力,因此在需要频繁地访问前一个节点或后一个节点的情况下,双向链表比单向链表更加高效。

    例如,浏览器的前进和后退功能就可以使用双向链表来实现。每次访问一个新的页面时,将页面的URL存储在一个双向链表中,然后可以通过点击前进或后退按钮来遍历并显示对应的页面。

    另一个例子是实现LRU(Least Recently Used)缓存算法,其中双向链表可以记录访问顺序,当缓存空间不足时可以删除最近最少使用的节点。

    5. 常见的双向链表操作

    除了上面提到的插入、删除和遍历操作,双向链表还支持其他常见的操作,如获取链表长度、在指定位置插入节点和删除指定节点等。这些操作的实现与单向链表类似,只是需要考虑更新节点的前后指针。

    综上所述,双向链表是一种非常有用的数据结构,在一些特定场景下可以提供更高效的操作。通过实现双向链表的节点类和链表类,并使用PHP语言来操作,我们可以轻松地使用双向链表来解决一些问题。

    2年前 0条评论
  • 不及物动词的头像
    不及物动词
    这个人很懒,什么都没有留下~
    评论

    双向链表(Doubly Linked List)是一种常用的数据结构,它与单向链表相比,每个节点除了包含指向下一个节点的指针外,还包含指向上一个节点的指针。这样的设计使得双向链表可以实现双向遍历,而不仅仅是单向遍历。在某些情况下,双向链表比单向链表具有更好的性能,特别是在需要频繁插入和删除节点的情况下。

    本文将详细介绍如何使用PHP实现双向链表,并演示双向链表的基本操作流程,包括创建链表、插入节点、删除节点等。文章将按照以下小标题展示内容:

    ## 1. 双向链表的定义和数据结构设计

    首先,我们需要定义一个双向链表类。这个类包含两个属性:头节点和尾节点。头节点用于保存链表的第一个节点,尾节点用于保存链表的最后一个节点。

    “`php
    class DoublyLinkedList {
    private $head;
    private $tail;

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

    // 省略其他方法
    }
    “`

    ## 2. 创建双向链表和节点

    接下来,我们需要实现创建链表和节点的方法。创建链表时,我们只需要将头节点和尾节点初始化为null。创建节点时,我们需要传入数据以及前驱节点和后继节点的引用。

    “`php
    public function createLinkedList() {
    $this->head = null;
    $this->tail = null;
    }

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

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

    ## 3. 在双向链表中插入节点

    插入节点是双向链表中的一个重要操作。插入节点时,我们需要考虑两种情况:插入在链表的头部和插入在链表的尾部。插入在头部时,我们需要将新节点的后继节点指向原头节点,同时更新原头节点的前驱节点指向新节点。插入在尾部时,我们需要将新节点的前驱节点指向原尾节点,同时更新原尾节点的后继节点指向新节点。

    “`php
    public function insertAtHead($data) {
    $newNode = new Node($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 Node($data);

    if($this->tail === null) {
    $this->head = $newNode;
    $this->tail = $newNode;
    } else {
    $newNode->prev = $this->tail;
    $this->tail->next = $newNode;
    $this->tail = $newNode;
    }
    }
    “`

    ## 4. 从双向链表中删除节点

    删除节点也是双向链表中的一个重要操作。删除节点时,我们需要考虑两种情况:删除头节点和删除尾节点。删除头节点时,我们只需要将头节点的后继节点设置为新的头节点即可。删除尾节点时,我们只需要将尾节点的前驱节点设置为新的尾节点即可。

    “`php
    public function deleteAtHead() {
    if($this->head === null) {
    return;
    }

    $this->head = $this->head->next;
    $this->head->prev = null;

    if($this->head === null) {
    $this->tail = null;
    }
    }

    public function deleteAtTail() {
    if($this->tail === null) {
    return;
    }

    $this->tail = $this->tail->prev;
    $this->tail->next = null;

    if($this->tail === null) {
    $this->head = null;
    }
    }
    “`

    ## 5. 遍历双向链表

    遍历双向链表是为了查看链表中的所有节点。我们可以从头节点开始,沿着后继指针依次遍历每个节点,并打印节点的数据。

    “`php
    public function display() {
    $current = $this->head;

    while($current !== null) {
    echo $current->data . ” “;
    $current = $current->next;
    }

    echo “\n”;
    }
    “`

    ## 6. 实例化并测试双向链表

    最后,我们可以实例化双向链表,并测试插入和删除操作。下面是一个简单的示例代码:

    “`php
    $linkedList = new DoublyLinkedList();

    $linkedList->insertAtHead(1);
    $linkedList->insertAtHead(2);
    $linkedList->insertAtTail(3);

    $linkedList->display(); // 输出结果:2 1 3

    $linkedList->deleteAtHead();
    $linkedList->deleteAtTail();

    $linkedList->display(); // 输出结果:1
    “`

    以上就是使用PHP实现双向链表的方法和操作流程。通过双向链表,我们可以实现更高效的节点插入和删除操作,并且可以双向遍历链表。

    2年前 0条评论
注册PingCode 在线客服
站长微信
站长微信
电话联系

400-800-1024

工作日9:30-21:00在线

分享本页
返回顶部