Home  >  Article  >  Backend Development  >  PHP data structure: the charm of linked lists, exploring dynamic data organization

PHP data structure: the charm of linked lists, exploring dynamic data organization

WBOY
WBOYOriginal
2024-06-04 12:53:57517browse

A linked list is a data structure that uses a series of nodes with data and pointers to organize elements. It is particularly suitable for processing large data sets and frequent insertion/deletion operations. Its basic components include nodes (data and pointers to the next node) and head nodes (pointing to the first node in the linked list). Common linked list operations include: addition (tail insertion), deletion (specific value) and traversal.

PHP data structure: the charm of linked lists, exploring dynamic data organization

PHP Data Structure: The Charm of Linked List

Introduction

Linked list is a linear data structure, elements Organized as a sequence of nodes, each node contains data and a pointer to the next node. Unlike an array, the elements of a linked list do not need to be stored contiguously in memory, which makes it ideal for processing large data sets and situations where insertion and deletion operations are frequent.

Concept

The basic component of a linked list is a node. Each node consists of the following parts:

  • Data: stores actual values
  • Pointer (next): points to the next node

The linked lists are connected to each other through the head node. The head node is a special node that points to the first node in the linked list.

Operation

The following are some common operations implemented in linked lists:

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

class LinkedList {
    private $head;

    // 添加新节点到尾部
    public function append($data) {
        $new_node = new Node();
        $new_node->data = $data;

        if ($this->head === null) {
            $this->head = $new_node;
        } else {
            $current_node = $this->head;
            while ($current_node->next !== null) {
                $current_node = $current_node->next;
            }
            $current_node->next = $new_node;
        }
    }

    // 从链表中删除特定值
    public function delete($data) {
        if ($this->head === null) {
            return;
        }

        if ($this->head->data === $data) {
            $this->head = $this->head->next;
            return;
        }

        $current_node = $this->head;
        while ($current_node->next !== null) {
            if ($current_node->next->data === $data) {
                $current_node->next = $current_node->next->next;
                return;
            }
            $current_node = $current_node->next;
        }
    }

    // 遍历链表并打印数据
    public function traverse() {
        $current_node = $this->head;
        while ($current_node !== null) {
            echo $current_node->data . " ";
            $current_node = $current_node->next;
        }
    }
}

Practical case

Create a linked list and perform some operations:

$list = new LinkedList();

$list->append(10);
$list->append(20);
$list->append(30);

echo "链表:";
$list->traverse();
echo PHP_EOL;

$list->delete(20);

echo "删除 20 后:" ;
$list->traverse();
echo PHP_EOL;

Output:

链表:10 20 30
删除 20 后:10 30

The above is the detailed content of PHP data structure: the charm of linked lists, exploring dynamic data organization. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn