C++两个队列实现一个栈
C++两个队列实现一个栈
/*
* source.cpp
*
* Created on: 2015年6月21日
* Author: codekiller
*/ #include "iostream"
#include "queue"
#include <exception>
#include "stdexcept"
using namespace std; #define should_not_reach_here template <typename T> class QStack{ public:
QStack(void){};
~QStack(void){};
void push(const T& element);
T pop();
private:
queue<T> queue1;
queue<T> queue2; }; template <typename T> void QStack<T>::push(const T& element){ if(!queue2.empty())
queue2.push(element);
else if(!queue1.empty())
queue1.push(element);
else if(queue1.empty()&&queue2.empty())
queue1.push(element); } template <typename T> T QStack<T>::pop(){
if(queue1.size()==0&&queue2.size()==0)
{ throw underflow_error(string("QStack UnderFlow"));
//return 0;
}
if(queue1.size()==0){
while(queue2.size()>1){
queue1.push(queue2.front());
queue2.pop();
}
T & dataout=queue2.front();
queue2.pop();
return dataout;
}else if(queue2.size()==0){
while(queue1.size()>1){
queue2.push(queue1.front());
queue1.pop();
}
T & dataout=queue1.front();
queue1.pop();
return dataout;
} should_not_reach_here
} int main(){ QStack<int> sstack;
sstack.push(1);
cout<<sstack.pop()<<endl;
sstack.push(100);
try{
cout<<sstack.pop()<<endl;
cout<<sstack.pop()<<endl;
cout<<sstack.pop()<<endl;
}
catch(underflow_error &err){
cerr<<err.what()<<endl;
} }
C++两个队列实现一个栈的更多相关文章
- 《剑指Offer》附加题_用两个队列实现一个栈_C++版
在<剑指Offer>中,在栈和队列习题中,作者留下来一道题目供读者自己实现,即"用两个队列实现一个栈". 在计算机数据结构中,栈的特点是后进先出,即最后被压入(push ...
- 剑指offer编程题Java实现——面试题7相关题用两个队列实现一个栈
剑指offer面试题7相关题目:用两个队列实现一个栈 解题思路:根据栈的先入后出和队列的先入先出的特点1.在push的时候,把元素向非空的队列内添加2.在pop的时候,把不为空的队列中的size()- ...
- java两个栈实现一个队列&&两个队列实现一个栈
栈:先进后出 队列:先进先出 两个栈实现一个队列: 思路:先将数据存到第一个栈里,再将第一个栈里的元素全部出栈到第二个栈,第二个栈出栈,即可达到先进先出 源码: class Queue<E&g ...
- python两个队列实现一个栈和两个栈实现一个队列
1.两个栈实现一个队列 两个栈stack1和stack2, push的时候直接push进stack1,pop时需要判断stack1和stack2中的情况.如果stack2不为空的话,直接从stack2 ...
- 【校招面试 之 剑指offer】第9-2题 用两个队列实现一个栈
#include<iostream> #include<queue> using namespace std; // 对于出栈解决的思路是:将queue1的元素除了最后一个外全 ...
- 两个队列实现一个栈,剑指offer P59
public class StackByQueue { private LinkedList<String> queue1; private LinkedList<String> ...
- 两队列模拟一个栈,python实现
python实现两个队列模拟一个栈: class Queue(object): def __init__(self): self.stack1=[] self.stack2=[] def enqueu ...
- 两个栈实现队列+两个队列实现栈----java
两个栈实现队列+两个队列实现栈----java 一.两个栈实现一个队列 思路:所有元素进stack1,然后所有出s ...
- 【剑指offer】两个队列实现堆栈
转载请注明出处:http://blog.csdn.net/ns_code/article/details/25076689 题目:用两个队列模拟一个栈,即用两个队列的出队和入队操作.来实现栈的 ...
随机推荐
- bug的生命周期
一 Bug重现环境 这个应该是我们重现bug的一个前提,没有这个前提,可能会无法重现问题,或根本无从下手. 操作系统 这个是一般软件运行的一大前提,基本上所有的软件都依赖于操作系统之上的,对于一个软 ...
- NN and the Optical Illusion-光学幻觉 CodeForce1100C 几何
题目链接:NN and the Optical Illusion 题目原文 NN is an experienced internet user and that means he spends a ...
- JavaScript学习记录
js整理笔记 1.数据类型 2.基本语法 3.js运算符 4.条件语句 5.类型转换 6.函数 7.预编译 8.作用域 9.闭包 10.对象创建方法 11.this 12.dom操作 13.事件 14 ...
- Centos安装PhantomJS
1.下载PhantomJS [root@liuge ~]# wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-l ...
- F#周报2019年第39期
新闻 宣告F# 4.7 宣告.NET Core 3.0 .NET Core 3.0中ASP.NET Core与Blazor的更新 .NET Conf 2019里提到的ML.NET与模型构建器 参与.N ...
- C语言入门-指针
终于到了精髓的地方了,这确实有点懵,总感觉这太麻烦了,而且写着也不爽,还是怀念py或者java,但也没办法,还是要继续学下去. 一.运算符& scanf("%d" , &a ...
- 【SQL server基础】object_id()函数
在SQLServer数据库中,如果查询数据库中是否存在指定名称的索引或者外键约束等,经常会用到object_id('name','type')方法,做笔记如下: ? 语法:object_id('obj ...
- egret引擎中使用tiled运行在微信小游戏中
egret的官方文档,对tiled的介绍不是很细致,很多东西都需要摸索.现在把踩的坑记录下来.作为一个备忘 引用tiledmap的库 在GitHub上下载egret的tiledmap支持库:https ...
- 常用css总结
个人博客: https://chenjiahao.xyz 1.让网站快速变灰 html { filter: grayscale(100%);//IE浏览器 -webkit-filter: graysc ...
- jQuery.noConflict()解决imgBox.js依赖jquery版本问题
jQuery提供两种点击图片放大效果出处 在使用imgbox.js是出现的jquery版本不兼容问题,之后了解到jQuery.noConflict()的用法 jQuery.noConflict()的存 ...