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. wpf xmal基础

    1.名称空间的引用 比如想使用System.Windows.Controls名称空间 首先需要把改名称空间所在的程序集presentationFramework.dll引用到项目里 然后在根元素的起始 ...

  2. JSON文件处理

    牛X的JSON解析JSON字符串显示字典键值 public void ResolveJson() { //定义的JSON字符串,注意JSON的格式 string str = @” { “”Name”” ...

  3. UltraEdit-32文本编辑器软件 23.20.0.28 中文版

    软件名称: UltraEdit-32文本编辑器软件软件语言: 简体中文授权方式: 共享软件运行环境: Win 32位/64位软件大小: 21.5MB图片预览: 软件简介:UltraEdit 是一个功能 ...

  4. 关于UI_USER_INTERFACE_IDIOM() & UIDevice.model

    使用 UI_USER_INTERFACE_IDIOM() 进行区分 (ios 3.2 >=) 无法区分iphone和ipod if (UI_USER_INTERFACE_IDIOM() == U ...

  5. many bugs report when test bcm bt/wifi chip

    after change aerial  , many bugs diappear .

  6. 关于指针要注意的地方还有尝试在codeblocks上建立项目

    1.字符串: char a[]="house"; char *b="house"; a[2]='r';可以   b[2]='r'不可以,因为这个指针变量指的是字 ...

  7. es6--(二)变量的解构赋值

    1.数组的解构赋值 //数组解析 let [a,b,c] = [1,2,3]; //a=1;b=2;c=3 //嵌套数组 let [a,[b,c]] = [1,[2,3]];//a=1;b=2;c=3 ...

  8. C++实现中缀表达式转前、后缀

    #include<iostream> #include<string> #include<stack> using namespace std; bool isIn ...

  9. UITableViewCell上面添加UIWebView

    首先创建一个webView对象和 private var webViewcell : UIWebView? 在viewDidLoad里面设置创建webViewcell,设置属性并用kvo监听webVi ...

  10. 本地缓存FMDB的使用(iOS)

    一.简单说明 1.什么是FMDB FMDB是iOS平台的SQLite数据库框架 FMDB以OC的方式封装了SQLite的C语言API 2.FMDB的优点 使用起来更加面向对象,省去了很多麻烦.冗余的C ...