php如何实现链表
PHP 实现链表的方法
基础链表节点类
链表由节点构成,每个节点包含数据和指向下一个节点的指针。以下是一个简单的节点类实现:

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;
}
// 在链表头部插入节点
public function insertAtHead($data) {
$newNode = new ListNode($data);
$newNode->next = $this->head;
$this->head = $newNode;
}
// 在链表尾部插入节点
public function insertAtTail($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($data) {
if ($this->head === null) return;
if ($this->head->data === $data) {
$this->head = $this->head->next;
return;
}
$current = $this->head;
while ($current->next !== null && $current->next->data !== $data) {
$current = $current->next;
}
if ($current->next !== null) {
$current->next = $current->next->next;
}
}
// 查找节点
public function search($data) {
$current = $this->head;
while ($current !== null) {
if ($current->data === $data) {
return true;
}
$current = $current->next;
}
return false;
}
// 打印链表
public function printList() {
$current = $this->head;
while ($current !== null) {
echo $current->data . " -> ";
$current = $current->next;
}
echo "NULL\n";
}
}
双向链表实现
双向链表节点包含指向前一个和后一个节点的指针:
class DoublyListNode {
public $data;
public $prev;
public $next;
public function __construct($data) {
$this->data = $data;
$this->prev = null;
$this->next = null;
}
}
class DoublyLinkedList {
private $head;
private $tail;
public function __construct() {
$this->head = null;
$this->tail = null;
}
// 在头部插入节点
public function insertAtHead($data) {
$newNode = new DoublyListNode($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 DoublyListNode($data);
if ($this->tail === null) {
$this->head = $newNode;
$this->tail = $newNode;
} else {
$newNode->prev = $this->tail;
$this->tail->next = $newNode;
$this->tail = $newNode;
}
}
// 删除节点
public function deleteNode($data) {
$current = $this->head;
while ($current !== null) {
if ($current->data === $data) {
if ($current->prev !== null) {
$current->prev->next = $current->next;
} else {
$this->head = $current->next;
}
if ($current->next !== null) {
$current->next->prev = $current->prev;
} else {
$this->tail = $current->prev;
}
return;
}
$current = $current->next;
}
}
// 打印链表
public function printList() {
$current = $this->head;
while ($current !== null) {
echo $current->data . " <-> ";
$current = $current->next;
}
echo "NULL\n";
}
}
使用示例
// 单链表使用示例
$list = new LinkedList();
$list->insertAtHead(3);
$list->insertAtHead(2);
$list->insertAtHead(1);
$list->insertAtTail(4);
$list->printList(); // 输出: 1 -> 2 -> 3 -> 4 -> NULL
// 双向链表使用示例
$dlist = new DoublyLinkedList();
$dlist->insertAtHead(2);
$dlist->insertAtHead(1);
$dlist->insertAtTail(3);
$dlist->printList(); // 输出: 1 <-> 2 <-> 3 <-> NULL
以上代码展示了PHP中实现单链表和双向链表的基本方法,包括节点的插入、删除和遍历操作。






