php pop/push/shift/unshift,哪些用于队列,哪些用于堆栈 [英] php pop/push/shift/unshift, which to use for queues and which for stacks

查看:42
本文介绍了php pop/push/shift/unshift,哪些用于队列,哪些用于堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 PHP 中,有两种方法可以将数组用作堆栈 (LIFO)
以及将它们用作队列 (FIFO) 的两种方法.

In PHP there are two ways to use an array as a stack (LIFO)
and two ways to use them as a queue (FIFO).

可以使用 push & 实现一个 stack弹出,
但是同样可以用 unshift &shift.

One could implement a stack with push & pop,
but the same can be done with unshift & shift.

类似地,可以使用 push & 实现 队列shift,
但是同样可以用 unshift &pop.

Similarly one could implement a queue with push & shift,
but the same can be done with unshift & pop.

演示:

echo "stack push & pop approach:\n";
$s = []; array_push($s, 'first'); array_push($s, 'second'); array_push($s, 'third');
echo     array_pop($s) . '-' .    array_pop($s) . '-' .     array_pop($s) .  "\n";
echo "stack unshift & shift approach:\n";
$s = []; array_unshift($s, 'first'); array_unshift($s, 'second'); array_unshift($s, 'third');
echo     array_shift($s) . '-' .     array_shift($s) . '-' .      array_shift($s) .  "\n";
echo "queue push & shift approach:\n";
$q = []; array_push($q, 'first'); array_push($q, 'second'); array_push($q, 'third');
echo     array_shift($q) . '-' .  array_shift($q) . '-' .   array_shift($q) .  "\n";
echo "queue unshift & pop approach:\n";
$q = []; array_unshift($q, 'first'); array_unshift($q, 'second'); array_unshift($q, 'third');
echo     array_pop($q) . '-' .       array_pop($q) . '-' .        array_pop($q) .       "\n";

输出:

stack push & pop approach:
third-second-first
stack unshift & shift approach:
third-second-first
queue push & shift approach:
first-second-third
queue unshift & pop approach:
first-second-third

那么使用哪些函数集?!

So which sets of functions to use?!

推荐答案

简短回答

对于 stack 使用 push &pop(添加到末尾,从末尾取出).

Short answer

For stacks use push & pop (add to end, take from end).

对于队列使用push &shift(添加到结尾,从头开始).

For queues use push & shift (add to end, take from beginning).

在考虑截至今天 2016-12-29 的这些函数的 PHP 文档时,我们发现:

When considering the PHP documentation as of today 2016-12-29 for these functions we find this:

对于array_push():
描述 提到array_push() 将数组视为堆栈"
示例使用$stack".

For array_push():
the description mentions "array_push() treats array as a stack"
and the example uses "$stack".

对于array_pop():
描述没有提到堆栈或队列
示例使用$stack".

For array_pop():
the description mentions nothing on stacks or queues
and the example uses "$stack".

对于array_shift():
描述没有提到堆栈或队列(但提到需要重新索引)
示例使用$stack".

For array_shift():
the description mentions nothing on stacks or queues (but mentions required reindexing)
and the example uses "$stack".

对于array_unshift():
描述没有提到堆栈或队列(但提到需要重新索引)
示例使用$queue".

For array_unshift():
the description mentions nothing on stacks or queues (but mentions required reindexing)
and the example uses "$queue".

所以这建议使用 push &pop 用于堆栈(基于描述中的提及)
并建议使用 unshift &pop 用于队列(仅基于示例中提到的队列).

So this suggests using push & pop for stacks (based on mention in description)
and suggests using unshift & pop for queues (based on only mention of queue in examples).

这似乎有点单薄...这里提交了改进文档的建议:https://bugs.php.net/bug.php?id=73839

This seems a bit thin... A suggestion to improve the documentation has been submitted here: https://bugs.php.net/bug.php?id=73839

运行:

echo "\nstack push & pop approach     :  ";
$a=[];$i=0;$t=microtime(true); while($i<100000) { array_push($a, 'dummy'); array_pop($a); ++$i; }; echo (microtime(true) - $t);
echo "\nstack unshift & shift approach:  ";
$a=[];$i=0;$t=microtime(true); while($i<100000) { array_unshift($a, 'dummy'); array_shift($a); ++$i; }; echo (microtime(true) - $t);
echo "\nqueue push & shift approach   :  ";
$a=[];$i=0;$t=microtime(true); while($i<100000) { array_push($a, 'dummy'); array_shift($a); ++$i; }; echo (microtime(true) - $t);
echo "\nqueue unshift & pop approach  :  ";
$a=[];$i=0;$t=microtime(true); while($i<100000) { array_unshift($a, 'dummy'); array_pop($a); ++$i; }; echo (microtime(true) - $t);

返回:

stack push & pop approach     :  0.011210918426514
stack unshift & shift approach:  0.015399217605591
queue push & shift approach   :  0.011627912521362
queue unshift & pop approach  :  0.015273094177246

对于 stacks 这建议使用 push &pop:自然术语,匹配文档中的提及并且性能更好(考虑到同时使用 unshiftshift 重新索引,这是有道理的).

For stacks this suggests to use push & pop: natural terminology, matching the mention in the documentation and also performing better (which makes sense considering the reindexing with both unshift & shift).

对于队列,这建议使用push &shift,尽管文档中有提到.

For queues this suggests to use push & shift, despite the mention in the documentation.

这篇关于php pop/push/shift/unshift,哪些用于队列,哪些用于堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆