Implement the following operations of a queue using stacks.

  • push(x) -- Push element x to the back of queue.
  • pop() -- Removes the element from in front of queue.
  • peek() -- Get the front element.
  • empty() -- Return whether the queue is empty.

Notes:

  • You must use only standard operations of a stack -- which means only
    push to top, peek/pop from top, size, and
    is empty
    operations are valid.
  • Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
  • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

题目要求通过栈来模拟队列的行为。与此类似的还有通过队列模拟栈(http://blog.csdn.net/sunao2002002/article/details/46482673),此题是算法导论第十章的一道题。算法例如以下:

堆栈a和b。a用作入队,b出队

(1)判队满:假设a满且b不为空,则队满

(2)判队空:假设a和b都为空,则队空

(3)入队:首先判队满。

若队不满:(1)栈a若不满,则直接压入栈a

(2)若a满,则将a中的全部元素弹出到栈b中,然后再将元素入栈a

(4)出队:(1)若b空就将a中的全部元素弹出到栈b中。然后出栈

(2)b不空就直接从b中弹出元素

代码例如以下:

class Queue {
public:
// Push element x to the back of queue.
stack<int> in;
stack<int> out;
void push(int x) {
in.push(x);
} void move(){
while(!in.empty())
{
int x = in.top();
in.pop();
out.push(x);
}
}
// Removes the element from in front of queue.
void pop(void) {
if (out.empty())
{
move();
}
if (!out.empty())
{
out.pop();
}
} // Get the front element.
int peek(void) {
if (out.empty())
{
move();
}
if (!out.empty())
{
return out.top();
} } // Return whether the queue is empty.
bool empty(void) {
return in.empty() && out.empty(); }
};

LeetCode 232: Implement Queue using Stacks的更多相关文章

  1. LeetCode 232:Implement Queue using Stacks

     Implement the following operations of a queue using stacks. push(x) -- Push element x to the back ...

  2. LeetCode OJ:Implement Queue using Stacks(栈实现队列)

    比较典型的一个题目,easy,不过可以有许多实现方式. 这里用的方式是每次pop完成之后再将stack2中的内容立即倒回stack1中.但是其他的实现也可以不是这样,可以是需要push的时候检查再,如 ...

  3. 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues

    232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...

  4. 【LeetCode OJ 232】Implement Queue using Stacks

    题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operatio ...

  5. LeetCode(232) Implement Queue using Stacks

    题目 Implement the following operations of a queue using stacks. push(x) – Push element x to the back ...

  6. LeetCode算法题-Implement Queue Using Stacks(Java实现)

    这是悦乐书的第195次更新,第201篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第57题(顺位题号是232).使用栈实现队列的以下操作. push(x) - 将元素x推 ...

  7. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  8. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

  9. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

随机推荐

  1. 【codeforces 794A】Bank Robbery

    [题目链接]:http://codeforces.com/contest/794/problem/A [题意] 每个位置上可能有物品(>=1)或是没物品 你一开始在某一个位置b; 然后你最左可以 ...

  2. LaTeX 基本的公式符号命令

    本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50240237 下面列出一些基本的LaT ...

  3. aliyun Ubuntu 14.04 64bit OpenJDK Tomcat7 install

    my work environment: aliyun Ubuntu 14.04 64位 first phase:apt-get update    (it is very important,oth ...

  4. 叫号系统排队系统挂号系统实现(JAVA队列)

    关于队列,使用的地方很的多. 现实中有许多的样例. 比方医院的挂号系统,银行里的叫号系统,食堂里的排队打饭等等.市场上又这种排队取号的设备.他们的功能基本例如以下: 1.系统可联网联机统一发号.2.系 ...

  5. [windows+cocos2dx]CCSprite精灵类

    序言 回想cocos2dx,之前在mac+Xcode平台学习了一遍cocos2dx,一年时间不接触cocos了.一直在搞Unity3d.如今还是就之前所学温故温故,但不再用Xcode来写.用经常使用的 ...

  6. C 语言运算符优先级(记忆口诀)

    优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] 左到右   () 圆括号 (表达式)/函数名(形參表)   . 成员选择(对象) 对象.成员名   -& ...

  7. c语言中类型转换与赋值运算符、算术运算符、关系运算符、逻辑运算符。原码、反码、补码。小解。

    类型转换 自动转换 小范围的类型能够自动转换成大范围的类型.short->int->long->float->double 强制类型转换 (类型名)变量或数值 #include ...

  8. 前后端分离跨域 关于前后端分离开发环境下的跨域访问问题(angular proxy=>nginx )

    前后端分离后遇到了跨域访问的问题: angular1中使用proxy很麻烦,最后还是失败结束:最后总结3种方法如下: 本人使用的第一种方法,只是开发环境下使用很方便! 1:禁掉谷歌的安全策略(Turn ...

  9. rails数据库操作rake db一点心得

    问题描述,对于很多的新手rails lover来说,搞定db是件头疼的事情,当建立了一个model,测试了半天发现我草列名写错了,再过一会儿发现association里面竟然没有xxx_id,这下子s ...

  10. asp.net mvc5 文件下载上传

    下载:是通过点击a标签直接下载的方式,没有其他任何要求,在服务器上存在实体文件,不需要请求后台控制层 前段js: <a id="NF-DownLoad" authorize= ...