以下是一个使用PHP实现链表结构(LLS)的示例,包括创建链表、添加节点、删除节点、遍历链表等基本操作。

1. 创建链表

我们需要定义一个节点类和一个链表类。

```php

class Node {

public $data;

public $next;

public function __construct($data) {

$this->data = $data;

$this->next = null;

}

}

class LinkedList {

private $head;

public function __construct() {

$this->head = null;

}

// 添加节点到链表尾部

public function append($data) {

$newNode = new Node($data);

if ($this->head === null) {

$this->head = $newNode;

} else {

$current = $this->head;

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

$current = $current->next;

}

$current->next = $newNode;

}

}

// 遍历链表

public function traverse() {

$current = $this->head;

while ($current !== null) {

echo $current->data . ' ';

$current = $current->next;

}

echo "