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 ...
随机推荐
- ios开发之简单实现loading动画效果
最近有朋友问我类似微信语音播放的喇叭动画和界面图片加载loading界面是怎样实现的,是不是就是一个gif图片呢!我的回答当然是否定了,当然不排除也有人用gif图片啊!下面我就来罗列三种实现loadi ...
- JDK各版本新增的主要特性
JDK1.5新特性: 1.自动装箱与拆箱: 2.枚举 3.静态导入,如:import staticjava.lang.System.out 4.可变参数(Varargs) 5.内省(Introspec ...
- Java中的native关键字与JNI
一.先说一下大致的意思: jdk提供的类库源代码中有一些方法没有实现,这些方法前有native关键字,如object类中的 : native Object clone() throws CloneNo ...
- js实现多行文本超出一定字数显示省略号功能
最近项目中遇到了一个关于超出一定字数用省略号显示的问题,其实这种形式很常见,公司简介.产品介绍啊里面都可能会用到,一行文字显示省略号很容易,多行就得想点办法了.在经过查阅.整理之后,我也算是实现了这个 ...
- Elastarchsearch安装搭建(一)
Elasticsearch是一个实时分布式搜索和分析引擎.一个基于Apache Lucene(TM)的开源搜索引擎.无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进.性能最好的.功能最全 ...
- Cocoapod安装使用和常见问题(转载)
1.cocoapod的按照,cocoapod是运行在ruby环境下的,在ruby环境的 ,像cocoapod这样的开源项目时放在放在rubygems服务器上面的,但国内访问https://ruby ...
- UltimateDefrag磁盘碎片整理软件 v3.0.100.19汉化版
软件名称:UltimateDefrag磁盘碎片整理软件 v3.0.100.19汉化版软件类别:汉化软件运行环境:Windows软件语言:简体中文授权方式:免费版软件大小:3.25 MB软件等级:整理时 ...
- HTML4基础
form 表单 首先,讨论“控件”(下面很多都是新控件, ...
- mongodb (一)
#mongodb安装(3.4.0) #下载安装包,解压 mkdir /data/mongodb cd /data/mongodb mkdir log conf data bin vim conf/mo ...
- 关于IT实例教程
W3c:http://www.w3school.com.cn/ 易百:http://www.yiibai.com/ 菜鸟教程:http://www.runoob.com/