news 2026/6/7 12:37:05

PHP数据结构链表与栈队列实现

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
PHP数据结构链表与栈队列实现

PHP数据结构链表与栈队列实现

PHP的数组功能很强大,但有些场景自己实现数据结构能加深理解。今天用PHP实现链表、栈和队列。

链表的实现。

```php
class ListNode
{
public function __construct(
public mixed $data = null,
public ?ListNode $next = null
) {}
}

class LinkedList
{
private ?ListNode $head = null;
private int $size = 0;

public function addFirst(mixed $data): void
{
$this->head = new ListNode($data, $this->head);
$this->size++;
}

public function addLast(mixed $data): void
{
$node = new ListNode($data);
if ($this->head === null) {
$this->head = $node;
} else {
$current = $this->head;
while ($current->next !== null) $current = $current->next;
$current->next = $node;
}
$this->size++;
}

public function remove(mixed $data): bool
{
if ($this->head === null) return false;
if ($this->head->data === $data) {
$this->head = $this->head->next;
$this->size--;
return true;
}
$current = $this->head;
while ($current->next !== null) {
if ($current->next->data === $data) {
$current->next = $current->next->next;
$this->size--;
return true;
}
$current = $current->next;
}
return false;
}

public function toArray(): array
{
$result = [];
$current = $this->head;
while ($current !== null) {
$result[] = $current->data;
$current = $current->next;
}
return $result;
}
}

$list = new LinkedList();
$list->addLast(10);
$list->addLast(20);
$list->addFirst(5);
$list->remove(10);
print_r($list->toArray());
?>
>

栈的实现。后进先出。

```php
class Stack
{
private array $items = [];

public function push(mixed $item): void
{
$this->items[] = $item;
}

public function pop(): mixed
{
if ($this->isEmpty()) throw new UnderflowException("栈为空");
return array_pop($this->items);
}

public function peek(): mixed
{
if ($this->isEmpty()) throw new UnderflowException("栈为空");
return $this->items[count($this->items) - 1];
}

public function isEmpty(): bool
{
return empty($this->items);
}

public function size(): int
{
return count($this->items);
}
}

$stack = new Stack();
$stack->push(10);
$stack->push(20);
$stack->push(30);
echo $stack->pop() . "\n";
echo $stack->peek() . "\n";
echo $stack->size() . "\n";
?>
>

队列的实现。先进先出。

```php
class Queue
{
private array $items = [];

public function enqueue(mixed $item): void
{
$this->items[] = $item;
}

public function dequeue(): mixed
{
if ($this->isEmpty()) throw new UnderflowException("队列为空");
return array_shift($this->items);
}

public function front(): mixed
{
if ($this->isEmpty()) throw new UnderflowException("队列为空");
return $this->items[0];
}

public function isEmpty(): bool
{
return empty($this->items);
}

public function size(): int
{
return count($this->items);
}
}

$queue = new Queue();
$queue->enqueue("A");
$queue->enqueue("B");
$queue->enqueue("C");
echo $queue->dequeue() . "\n";
echo $queue->front() . "\n";
echo $queue->size() . "\n";
?>
>

PHP的SplStack和SplQueue是内置的栈和队列实现,性能更好。但自己实现一遍能更清楚地理解数据结构的原理。数组配合array_push和array_pop可以模拟栈,array_push和array_shift可以模拟队列。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/7 12:36:13

小米模式如何颠覆传统硬件:供应链、参数化与生态的降维打击

1. 从一场微博“口水战”说起:消费电子圈的“鲶鱼效应”这几天科技圈和硬件发烧友圈子里挺热闹的,起因是传闻小米要进军机械键盘市场,定价可能在百元级别。紧接着,机械键盘领域的“祖师爷”、德国品牌CHERRY的官方微博就发了一条颇…

作者头像 李华
网站建设 2026/6/7 12:35:47

从镍氢到锂电池:老式剃须刀动力改造全流程与避坑指南

1. 项目缘起:从“智商税”到“技术宅”的倔强作为一名在消费电子行业摸爬滚打了十几年的硬件工程师,我手边总有些“老伙计”。这台飞利浦剃须刀就是其中之一,当年花了近五百大洋购入,算是笔不小的开销。三年前我手痒拆过一次&…

作者头像 李华
网站建设 2026/6/7 12:34:41

LinkSwift网盘直链下载助手:一键获取九大网盘真实下载地址的完整指南

LinkSwift网盘直链下载助手:一键获取九大网盘真实下载地址的完整指南 【免费下载链接】Online-disk-direct-link-download-assistant 一个基于 JavaScript 的网盘文件下载地址获取工具。基于【网盘直链下载助手】修改 ,支持 百度网盘 / 阿里云盘 / 中国移…

作者头像 李华