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的更多相关文章

  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. 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) -- ...

  5. LintCode-Implement Queue by Stacks

    As the title described, you should only use two stacks to implement a queue's actions. The queue sho ...

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

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

  7. Implement Queue by Two Stacks

    As the title described, you should only use two stacks to implement a queue's actions. The queue sho ...

  8. 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 ...

  9. Coursera Algorithms week2 栈和队列 练习测验: Queue with two stacks

    题目原文: Implement a queue with two stacks so that each queue operations takes a constant amortized num ...

随机推荐

  1. Symfony命令大全

    执行命令: $ php bin/console 查看一下命令 Symfony version 3.1.5 - app/dev/debug Usage: command [options] [argum ...

  2. java 缓存ehcache的使用(使用方式一)

    实体要序列化 resource文件夹下建立 ehcache.xml <?xml version="1.0" encoding="UTF-8"?> & ...

  3. 梅特卡夫法则(Metcalfe's law)

    如果一个网络中有n个人,那么网络对于每个人的价值与网络中其他人的数量成正比,于是网络对于所有人的总价值与n*(n-1)成正比.

  4. 前端自动化部署之gulp

    1.首先需要安装node+npm(这里不再叙述,网上教程一大堆) 2.gulp全局安装:npm install -g gulp 3.cd进入到你的项目目录,这里使用demo文件夹为我的示例项目 4.在 ...

  5. <poj - 3268> Silver Cow Party 牛のpart 最短路径问题

    本题链接 : http://poj.org/problem?id=3268 题目大意:牛们要去聚会,输入N = 顶点数(牛场):M = 边(路)的数目: X = 终点 (聚会点).问题:求来回时间的最 ...

  6. swift 2 选择头像图片

    一句话选择单个头像图片 新建ImagePickerViewController类: /* let imagePicker = ImagePickerViewController() imagePick ...

  7. iOS 加载本地 HTML 文件 CSS 样式图片无效果

    在开发的过程中,有时候需要加载一些 HTML 页面,对于不太复杂的界面,基本上都可以放到本地用 UIWebview 来加载,但是在开发过程中会碰到 UIWebview 加载出来的 HTML 页面没有图 ...

  8. code style

    http://www.jianshu.com/p/0a984f999592# https://github.com/drakeet/LayoutFormatter https://github.com ...

  9. 安装Redis无错流程

    1.参考文章<安装3.0.3版本配置文章参考>http://www.iyunv.com/thread-89612-1-1.html 2.安装tcl组件包(安装Redis需要tcl支持) 下 ...

  10. IIS下访问网络驱动器(网络位置)

    System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = "cmd.ex ...