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 ...
随机推荐
- eclipse安装插件的4种方式
Eclipse插件的安装方法大体有以下三种:[9] 第一种:直接复制法 假设Eclipse的安装目录在C:\eclipse,解压下载的eclipse 插件或者安装eclipse 插件到指定目录AA(如 ...
- 【Time系列一】datetime的妙用
今天在弄个自动关机小脚本的时候,遇到了时间转换的问题,也难怪,以前没学过, 不能怪我不会哦! 首先,先学会打印出当前时间的几种方式 参考开源社区: http://my.oschina.net/u/1 ...
- rte_mempool内存管理
DPDK以两种方式对外提供内存管理方法,一个是rte_mempool,主要用于网卡数据包的收发:一个是rte_malloc,主要为应用程序提供内存使用接口.本文讨论rte_mempool.rte_me ...
- ios 显示其他app的购买页面
using UnityEngine; using System.Collections; using System.Runtime.InteropServices ; public class IOS ...
- delphi 帮助文档(中英对译)下载地址
文档下载地址:http://download.csdn.net/detail/yangzhimars/5014350
- 推荐几个好的 Maven 常用仓库网址
注意,以下内容转载自:推荐几个好的 Maven 常用仓库网址 Maven 确确实实是个好东西,用来管理项目显得很方便,但是如果是通过 Maven 来远程下载 JAR 包的话,我宿舍的带宽是4兆的,4个 ...
- JAVA的字符编码及问题
web开发时,字符编码及有时候也会是一个麻烦的问题,没有经验的话,肯定不知道怎么解决,有一定的经验的话,那还是比较简单的.以下,是我学习过程中总结出来的几种字符编码级问题和其解决的方法 1.文档乱码, ...
- Android实现图片宽度100%ImageView宽度且高度按比例自动伸缩
在ListView中为了实现图片宽度100%适应ImageView容器宽度,让高度自动按比例伸缩功能,查了很多资料,搞了一下午都没找出个现成的办法,不过貌似有个结论了,就是: Android自身不能实 ...
- Counting Stars
Counting Stars 题目链接:http://acm.xidian.edu.cn/problem.php?id=1177 离线+一维树状数组 一眼扫过去:平面区间求和,1e6的数据范围,这要h ...
- spring注解支持
Spring基于注解实现Bean定义支持如下三种注解: Spring自带的@Component注解及扩展@Repository.@Service.@Controller JSR-250 1.1版本中中 ...