办法扭转队列只用了两个临时队列,仅此而已? [英] A way to reverse queue using only two temporary queues and nothing more?

查看:136
本文介绍了办法扭转队列只用了两个临时队列,仅此而已?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法扭转项目中只使用了两个临时队列队列顺序(没有其他变量,如柜台)?只有标准的队列操作可用:ENQUEUE(E),DEQUEUE(),空()?

Is there a way to reverse items' order in queue using only two temporary queues (and no other variables, such as counters)? Only standard queue operation are available: ENQUEUE(e), DEQUEUE(), EMPTY()?

在任何语言或伪code解决方案是值得欢迎的。

Solutions in any language or pseudocode are welcome.

推荐答案

您可以:

  • 使用两个队列模拟堆栈
  • 按所有原始队列中的元素来这个堆栈中。
  • 现在,从栈中弹出的每个元素,并把它添加到原始队列。

这篇关于办法扭转队列只用了两个临时队列,仅此而已?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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