php队列与堆栈的区别,在PHP中使用队列和堆栈

这篇文章的内容是受Vaidehi Joshi的“排队或不排队” 以及Base CS系列视频堆栈和队列的启发

在Vaidehi的帖子中,她解释了队列如何工作并显示了一些使队列入队和出队的“函数/方法”,这是一个简单直接的示例,向您展示了它在PHP中的工作方式(我假设您已经了解了CS视频)。

PHP带有一个称为的标准库SplQueue(Spl表示标准PHP库)SplQueue继承自SplDoublyLinkedList。因此,SplQueue支持方法push()和对象pop()。但是请注意,如果在对象上使用push()和pop()方法SplQueue,则其行为类似于堆栈而不是队列

Manu Manjunath (PHP官方文档的原始解释)

简短的长篇小说,SplDoublyLinkedList意味着同一个图书馆可同时为queue和使用stacks。

堆栈

如上所述,如果在对象上使用push()和pop(),SplQueue则行为将是堆栈而不是队列,请参见:$stack = new SplQueue();

$stack->push(1);

$stack->push(2);

$stack->push(3);

$stack->pop(); // remove the last one

print_r($stack);

/*

OUTPUT

SplQueue Object

(

[flags:SplDoublyLinkedList:private] => 4

[dllist:SplDoublyLinkedList:private] => Array

(

[0] => 1

[1] => 2

)

)

*/

请注意,弹出了3个而不是 1。

s列

方法使用SplQueue的队列是:enqueue()和dequeue(),请参阅:$queue = new SplQueue();

$queue->enqueue('A');

$queue->enqueue('B');

$queue->enqueue('C');

$queue->dequeue(); // remove the first one

print_r($q);

/*

OUTPUT

SplQueue Object

(

[flags:SplDoublyLinkedList:private] => 4

[dllist:SplDoublyLinkedList:private] => Array

(

[0] => B

[1] => C

)

)

*/

Queue和Stack辅助方法:$queue->enqueue('dev'); // or $stack->push('dev');

$queue->enqueue('.to'); // or $stack->push('.to');

$queue->count(); // (int) 2

$queue->isEmpty(); // false

$queue->valid(); // false because right now the pointer is in the last position

// move the cursor to the first position, if you run valid() again after this it will be true

$queue->rewind();

$queue->current(); // "dev", current() just work if you rewind() before call it

$queue->unshift(3); // add the value to the beginning

$queue->current(); // "dev", because we need to move the cursor back to the first position

$queue->rewind();

$queue->current(); // 3

$queue->add(2, 'random-text'); // Add/insert a new value at the specified index

print_r($queue); // Show only 3, "dev" and "to" because we don't rewind() yet.

$queue->rewind(); // move the cursor back to the first position

print_r($queue);

/*

OUTPUT

SplQueue Object

(

[flags:SplDoublyLinkedList:private] => 4

[dllist:SplDoublyLinkedList:private] => Array

(

[0] => 3

[1] => dev

[2] => random-text

[3] => .to

)

)

*/

遍历队列和堆栈:$queue = new SplQueue();

$queue->enqueue('dev');

$queue->enqueue('.to');

$queue->unshift('https://');

$queue->rewind(); // always rewind the queue/stack, so PHP can start from the beginning.

while($queue->valid()){

echo $queue->current()."\n"; // Show the first one

$queue->next(); // move the cursor to the next element

}

/*

OUTPUT

https://

dev

.to

*/

您可以使用很多方法,但我无法在此处显示所有方法,您可以在官方文档中查看有关SplQueue

那么,您现在正在使用哪种编程语言?是否具有内置的队列和堆栈?如果是这样,我该如何使用?如果不是,您该怎么做?写一篇文章,向我们展示如何使用您喜欢的编程语言处理队列和堆栈!?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值