use 2 stacks to simulate a queue
class Stack{
private:
int cur = ;
int elem[];
public:
void push(int n);
int pop();
int peek();
int size();
}; void Stack::push(int n){
elem[cur] = n;
cur++;
} int Stack::pop(){
cur--;
return elem[cur + ];
} int Stack::peek(){
return elem[cur - ];
} int Stack::size(){
return cur;
} class Queue{
private:
Stack s1, s2;
public:
void enqueue(int n);
int dequeue();
int peek();
int size();
}; void Queue::enqueue(int n){
if(s1.size() == ){
while(s2.size()){
s1.push(s2.pop());
}
} s1.push(n);
} int Queue::dequeue(){
if(s2.size() == ){
while(s1.size()){
s2.push(s1.pop());
}
}
return s2.pop();
} int Queue::peek(){
if(s2.size() == ){
while(s1.size()){
s2.push(s1.pop());
}
} return s2.peek();
} int Queue::size(){
return s1.size()? s1.size() : s2.size();
}
use 2 stacks to simulate a queue的更多相关文章
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【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( ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- Implement Queue by Two Stacks & Implement Stack using Queues
Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...
- LintCode-Implement Queue by Stacks
As the title described, you should only use two stacks to implement a queue's actions. The queue sho ...
- Lintcode: Implement Queue by Stacks 解题报告
Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...
- Implement Queue by Two Stacks
As the title described, you should only use two stacks to implement a queue's actions. The queue sho ...
- 40. Implement Queue by Two Stacks【medium】
As the title described, you should only use two stacks to implement a queue's actions. The queue sho ...
- Coursera Algorithms week2 栈和队列 练习测验: Queue with two stacks
题目原文: Implement a queue with two stacks so that each queue operations takes a constant amortized num ...
随机推荐
- POJ 2484 A Funny Game
博弈. $n>=3$,后手赢,否则先手赢. #pragma comment(linker, "/STACK:1024000000,1024000000") #include& ...
- 安卓activity之间值共享解决办法,tabhost之间共享父类值,字符串类型的转换,获取每一个listview的item
1.tabhost父类值共享的解决办法 dianzhanliebiao.java是传值页面,zhuyemian.java放的是tabhost,dianzhangaikuang.java是tabhost ...
- Chapter 17_4 终结器
Lua中的垃圾回收主要是针对Lua对象,但是也可以做一些额外的资源管理工作. 可以为表设定垃圾收集的元方法(对于完全用户数据,则需要使用C API),该元方法称为 终结器. Lua用"__g ...
- 基于AFN的多张图片上传
不废话,直接上代码 NSString *urlString = [NSString stringWithFormat:@"http://192.168.1.166:8080/Discipli ...
- eclipse 导入tomcat7源码
导入tomcat的源码其实说简单也不简单,说不简单也简单,主要还是环境问题,中间花费了我很多时间,网上找了很多都没什么用,参考一些文章,然后自己慢慢摸索出来的. 环境:(1)jdk:jdk1.6.0_ ...
- spring Bean的三种注入方式
1.构造函数注入: 构造函数的注入方式分为很多种 (1)普通构造函数,空参数的构造函数 <bean id="exampleBean" class="examples ...
- mob.com Android studio 配置环境
- lumen框架
1,获取配置 app(); 说明:app()返回的是全局唯一的application对象,该对象的原型是Laravel\Lumen\Application 加载配置文件(config/app.php) ...
- keyboard添加down按钮
self.textView.inputAccessoryView = [self addToolbar]; - (UIToolbar *)addToolbar { UIToolbar *toolbar ...
- .net导出不规则Excel
using Hamp.App.BLL; using Hamp.App.Model; using Hamp.App.Model.QualityManagement; using System; usin ...