当前位置: 首页 > news >正文

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可以模拟队列。

http://www.jsqmd.com/news/968066/

相关文章:

  • 2026宿州黄金回收白银回收铂金回收怎么变现?实地探访 5 家本地老牌回收店铺 - 中安检金银铂钻回收
  • WarcraftHelper终极指南:3步解锁魔兽争霸300帧+宽屏完美体验
  • 湘潭黄金回收白银回收铂金回收哪家靠谱?2026 实地测评 5 家高人气实体门店 - 信誉隆金银铂奢回收
  • 宜宾黄金回收白银回收铂金回收哪家靠谱?2026 实地测评 5 家高人气实体门店 - 信誉隆金银铂奢回收
  • 2026芜湖黄金回收白银回收铂金回收怎么变现?实地探访 5 家本地老牌回收店铺 - 中安检金银铂钻回收
  • WinForms中DataGridView单元格自由合并与双级表头实现方案
  • 告别PetaLinux编译卡死:手把手教你配置本地sstate-cache与替换故障软件包(以glog/bind为例)
  • 2026张家界黄金回收白银回收铂金回收怎么变现?实地探访 5 家本地老牌回收店铺 - 中安检金银铂钻回收
  • MTKClient刷机教程:三步完成联发科设备救砖与数据恢复
  • 3分钟掌握全国高铁数据:Parse12306完全指南
  • ARM裸机开发与RTOS移植实战:从启动流程到uCOS-II移植详解
  • STM32F103C8T6驱动蜂鸣器/喇叭演奏《晴天》的可运行工程(含OLED显示与完整HAL/标准库支持)
  • 2026年6月GEO服务商哪家好?为什么GEO要找头部公司合作?技术、效果、合规三维度深度选型与评测 - 互联网科技品牌测评
  • 铜川黄金回收白银回收铂金回收去哪卖?5 家实地探访靠谱门店汇总 2026 - 中业金奢再生回收中心
  • MATLAB GUI里两个实用时间控件:实时系统时钟显示 + 5秒倒计时功能演示
  • 2026应届生AI智能降重工具盘点:省时省力+高分适配哪家强? - 降AI小能手
  • 2026宜春黄金回收白银回收铂金回收怎么变现?实地探访 5 家本地老牌回收店铺 - 中安检金银铂钻回收
  • 2026 泰州漏水维修攻略|苏易修缮推荐:卫生间 / 阳台 / 外墙 / 屋顶 / 地下室漏水|靠谱防水门店推荐 - 苏易修缮
  • 八大网盘直链下载助手完整指南:一键获取真实下载地址的终极解决方案
  • 2026最新伊犁黄金回收白银回收铂金回收攻略,实地甄选五家优质实体店 - 诚金汇钻回收公司
  • 2026武威黄金回收白银回收铂金回收怎么变现?实地探访 5 家本地老牌回收店铺 - 中安检金银铂钻回收
  • Winform项目快速套用Material风格的中文化UI组件包,含深色模式与字体渲染优化
  • VxWorks平台无硬件MDIO控制器时GPIO模拟SMI总线的驱动代码包
  • Balena Etcher 终极指南:三步完成系统镜像烧录的完整教程
  • AMD锐龙SDT调试工具深度解析:底层硬件参数调优实战指南
  • CSDN AI数字营销单次购买真相曝光,5大限制条款被忽略!资深运营总监亲测37次后的紧急预警
  • 兴安盟黄金回收白银回收铂金回收哪家靠谱?2026 实地测评 5 家高人气实体门店 - 信誉隆金银铂奢回收
  • 3分钟快速激活:Beyond Compare 5永久授权密钥完整指南
  • 别再只会用mc ls了!MinIO Client (mc) 这5个隐藏功能,帮你把对象存储玩出花
  • 湛江黄金回收白银回收铂金回收哪家靠谱?2026 实地测评 5 家高人气实体门店 - 信誉隆金银铂奢回收