以下是一个简单的PHP实例,演示如何声明一个链表以及一些基本操作,如添加节点、遍历链表等。

```php

实例php声明链表,PHP实例:声明链表及基本操作  第1张

// 定义链表节点类

class ListNode {

public $value;

public $next;

public function __construct($value) {

$this->value = $value;

$this->next = null;

}

}

// 定义链表类

class LinkedList {

private $head;

public function __construct() {

$this->head = null;

}

// 添加节点到链表尾部

public function append($value) {

$newNode = new ListNode($value);

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;

$results = [];

while ($current !== null) {

$results[] = $current->value;

$current = $current->next;

}

return $results;

}

}

// 创建链表实例

$linkedlist = new LinkedList();

// 向链表中添加节点

$linkedlist->append(1);

$linkedlist->append(2);

$linkedlist->append(3);

// 遍历链表并打印结果

$results = $linkedlist->traverse();

echo "