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可以模拟队列。
PHP数据结构链表与栈队列实现
张小明
前端开发工程师
用ConstraintLayout和LinearLayout实战:手把手教你用margin和padding实现精准的UI间距控制
深度解析Android布局间距:ConstraintLayout与LinearLayout的margin/padding实战指南在Android开发中,精确控制UI元素间距是构建高质量界面的关键技能。许多开发者虽然熟悉margin和padding的基本概念,但在实际项目中面对复杂布局嵌套时&#x…
小米模式如何颠覆传统硬件:供应链、参数化与生态的降维打击
1. 从一场微博“口水战”说起:消费电子圈的“鲶鱼效应”这几天科技圈和硬件发烧友圈子里挺热闹的,起因是传闻小米要进军机械键盘市场,定价可能在百元级别。紧接着,机械键盘领域的“祖师爷”、德国品牌CHERRY的官方微博就发了一条颇…
从镍氢到锂电池:老式剃须刀动力改造全流程与避坑指南
1. 项目缘起:从“智商税”到“技术宅”的倔强作为一名在消费电子行业摸爬滚打了十几年的硬件工程师,我手边总有些“老伙计”。这台飞利浦剃须刀就是其中之一,当年花了近五百大洋购入,算是笔不小的开销。三年前我手痒拆过一次&…
CUDA GPU上cuBLAS矩阵乘与向量乘实测工具:支持float/double、多尺寸、自动计时验证
本文还有配套的精品资源,点击获取 简介:一套开箱即用的CUDA性能测试工具,专注测量cuBLAS库中GEMM(矩阵乘)和GEMV(矩阵-向量乘)在真实GPU上的运行时间与吞吐量。用C和CUDA编写,含完…
LinkSwift网盘直链下载助手:一键获取九大网盘真实下载地址的完整指南
LinkSwift网盘直链下载助手:一键获取九大网盘真实下载地址的完整指南 【免费下载链接】Online-disk-direct-link-download-assistant 一个基于 JavaScript 的网盘文件下载地址获取工具。基于【网盘直链下载助手】修改 ,支持 百度网盘 / 阿里云盘 / 中国移…
别再为数据发愁了!用Python的ydata-synthetic库,5分钟搞定时间序列数据生成
用Python生成逼真时间序列数据:TimeGAN实战指南当你在开发一个预测电力消耗的模型时,发现历史数据只有三个月;或者构建股票价格预测系统时,高频交易数据获取成本令人望而却步——这些场景下,合成数据生成技术能成为你的…