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 toppeek/pop from topsize, 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).
 class Queue {
stack<int> s1;
stack<int> s2;
public:
// Push element x to the back of queue.
void push(int x) {
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
s1.push(x);
while(!s2.empty()){
s1.push(s2.top());
s2.pop();
}
} // Removes the element from in front of queue.
void pop(void) {
s1.pop();
} // Get the front element.
int peek(void) {
return s1.top();
} // Return whether the queue is empty.
bool empty(void) {
return s1.empty();
}
};

Implement Queue using Stacks的更多相关文章

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

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

  2. 【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( ...

  3. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  4. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

  5. Lintcode: Implement Queue by Stacks 解题报告

    Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...

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

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

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

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

  8. [LeetCode] Implement Queue using Stacks 用栈来实现队列

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

  9. Leetcode Implement Queue using Stacks

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

随机推荐

  1. Computer Network and Internet(1)

    计算机网路相关的教材很少,TCP/IP,HTTP 协议非常多,很难找到一个合适的材料去学习. <计算机网络>自上而下方法是这个方面的经典之作. 1.what is internet? 1. ...

  2. AngularJS 指令

    AngularJS 指令 AngularJS 指令是扩展的 HTML 属性,带有前缀 ng-. ng-app 指令 ng-app 指令定义了 AngularJS 应用程序的 根元素. ng-app 指 ...

  3. Spring整合Junit4

    1.加入相应依赖包 junit4-4.7.jar 以及spring相关jar包 2.在测试代码的源码包中如 src/test/java 新建一个抽象类如下 import org.junit.runne ...

  4. HashSet vs TreeSet vs LinkedHashSet

    使用Set集合的主要原因是因为Set集合里面没有重复的元素.Set集合有三个常见的实现类:HashSet,TreeSet,LinkedHashSet.什么时候,选择哪一个使用非常重要.简单的说,如果你 ...

  5. SQL Server(九)——事务

    事务: 保障流程的完整执行,就像银行取钱,先在你账上扣钱,然后存入别人的账上:但是从你账上扣完钱了,突然网断了,对方没有收到钱,那么此时你的钱也没了,别人的钱也没加上,事务为了防止此类情况的出现. 事 ...

  6. 【API】获取优酷视频信息接口

    序:        自己的网站中需要接入一个视频模块,虚拟主机的空间小所以只能引用第三方的链接.感觉国内优酷好不错,所以查了一下优酷的接口. 0x00:        先去优酷API开放中心申请一个开 ...

  7. SQL Server 2008 R2——VC++ ADO 操作 重复利用_ParameterPtr

    ==================================声明================================== 本文原创,转载在正文中显要的注明作者和出处,并保证文章的完 ...

  8. expr

    将变量的值当作数值处理而不是字符串,shell支持6种算术运算符:加+, 减-,乘\*,除/, 取余%, 括号\( \),每种算术运算符的两边都要有空格 $echo "Res = $(exp ...

  9. SpringMVC总结帖

    SpringMVC是基于MVC设计理念的一款优秀的Web框架,是目前最流行的MVC框架之一,SpringMVC通过一套注解,让POPJ成为处理请求的控制器,而无需实现任何接口,然后使用实现接口的控制器 ...

  10. 北理工c语言期末考试

    1 给定一个区间,输出其中前半部分数字之和等于后半部分数字之和的数,没有则输出No output.(15分) 题目内容: 给定一个区间,输出其中前半部分数字之和等于后半部分数字之和的数,没有则输出No ...