当前位置: 首页 > 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.rkmt.cn/news/1479818.html

相关文章:

  • 5分钟解锁Adobe全家桶:开源破解工具GenP 3.0终极指南
  • 宜宾黄金回收白银回收铂金回收哪家靠谱?2026 实地测评 5 家高人气实体门店 - 信誉隆金银铂奢回收
  • WinForms中DataGridView单元格自由合并与双级表头实现方案
  • 2026宿州黄金回收白银回收铂金回收怎么变现?实地探访 5 家本地老牌回收店铺 - 中安检金银铂钻回收
  • WarcraftHelper终极指南:3步解锁魔兽争霸300帧+宽屏完美体验
  • 3分钟掌握全国高铁数据:Parse12306完全指南
  • STM32F103C8T6驱动蜂鸣器/喇叭演奏《晴天》的可运行工程(含OLED显示与完整HAL/标准库支持)
  • MATLAB GUI里两个实用时间控件:实时系统时钟显示 + 5秒倒计时功能演示
  • 2026年6月GEO服务商哪家好?为什么GEO要找头部公司合作?技术、效果、合规三维度深度选型与评测 - 互联网科技品牌测评
  • 2026宜春黄金回收白银回收铂金回收怎么变现?实地探访 5 家本地老牌回收店铺 - 中安检金银铂钻回收
  • 2026 泰州漏水维修攻略|苏易修缮推荐:卫生间 / 阳台 / 外墙 / 屋顶 / 地下室漏水|靠谱防水门店推荐 - 苏易修缮
  • 2026最新伊犁黄金回收白银回收铂金回收攻略,实地甄选五家优质实体店 - 诚金汇钻回收公司
  • 2026武威黄金回收白银回收铂金回收怎么变现?实地探访 5 家本地老牌回收店铺 - 中安检金银铂钻回收
  • AMD锐龙SDT调试工具深度解析:底层硬件参数调优实战指南
  • 2026渭南黄金回收白银回收铂金回收怎么变现?实地探访 5 家本地老牌回收店铺 - 中安检金银铂钻回收
  • MEMS传感器原理全解析:从电容、压阻到热学与陀螺仪
  • 2026最新长沙黄金回收白银回收铂金回收攻略,实地甄选五家优质实体店 - 诚金汇钻回收公司
  • 巴法云MQTT接入避坑指南:用Python paho-mqtt库时,别忘了处理这几个隐藏的断开重连问题
  • 如何快速实现PC游戏本地多人分屏:Nucleus Co-Op的完整指南
  • GEO地理空间公司是什么?GEO优化公司是什么?两者谁是主流?2026年权威GEO公司TOP5推荐排行榜 - 互联网科技品牌测评
  • 微软Majorana 2量子芯片重磅发布:量子比特稳定时间达20秒,2029年规模化目标提前到来
  • 2026兴安盟黄金回收白银回收铂金回收怎么变现?实地探访 5 家本地老牌回收店铺 - 中安检金银铂钻回收
  • Turnitin查重降到27%?聊聊学术会议投稿前你该知道的查重那些事儿
  • Office 2007激活弹窗终极解决方案:注册表与配置文件修改原理详解
  • 2026西安黄金回收白银回收铂金回收怎么变现?实地探访 5 家本地老牌回收店铺 - 中安检金银铂钻回收
  • PyTorch轻量VAE实现:MNIST图像重建与随机数字生成
  • 遂宁黄金回收白银回收铂金回收去哪卖?5 家实地探访靠谱门店汇总 2026 - 中业金奢再生回收中心
  • CSS 性能诊断与选择器层级优化实战:浏览器渲染链路深度剖析
  • Allegro DRC错误代码解析:从编码逻辑到高效排查的PCB设计指南
  • 保姆级教程:用Docker 2.0.0镜像5分钟搞定RocketMQ Dashboard部署与初体验