用两个栈实现一个队列的功能
来源:互联网 发布:电商跟淘宝有区别吗 编辑:程序博客网 时间:2024/05/22 17:29
#include <iostream>
#include <stack>
using namespace std;
template<class T>
struct MyQueue
{
void push(T &t)
{
s1.push(t);
}
T front()
{
if(s2.empty())
{
if(s1.size() == 0) throw;
while(!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}
return s2.top();
}
void pop()
{
if(s2.empty())
{
while(!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}
if(!s2.empty())
s2.pop();
}
stack<T> s1;
stack<T> s2;
};
int main()
{
MyQueue<int> mq;
int i;
for( i = 0; i < 10; i++ )
{
mq.push(i);
}
for( i = 0; i < 10; i++ )
{
cout<<mq.front()<<endl;
mq.pop();
}
return 0;
}
#include <stack>
using namespace std;
template<class T>
struct MyQueue
{
void push(T &t)
{
s1.push(t);
}
T front()
{
if(s2.empty())
{
if(s1.size() == 0) throw;
while(!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}
return s2.top();
}
void pop()
{
if(s2.empty())
{
while(!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}
if(!s2.empty())
s2.pop();
}
stack<T> s1;
stack<T> s2;
};
int main()
{
MyQueue<int> mq;
int i;
for( i = 0; i < 10; i++ )
{
mq.push(i);
}
for( i = 0; i < 10; i++ )
{
cout<<mq.front()<<endl;
mq.pop();
}
return 0;
}
- 用两个栈实现一个队列的功能&&用两个队列实现一个栈的功能
- 用两个栈实现一个队列,用两个队列实现一个栈的功能
- 两个队列实现一个栈的功能
- 两个栈实现一个队列的功能
- 两个栈实现一个队列的功能
- 两个栈实现一个队列的功能
- 用两个栈实现一个队列功能
- 用两个栈实现一个队列功能
- 用两个栈实现一个队列功能
- 用两个栈实现一个队列的功能
- 用两个栈实现一个队列的功能
- 用两个栈实现一个队列的功能...
- 用两个栈实现一个队列的功能
- 用两个栈实现一个队列的功能
- 用两个栈实现一个队列的功能
- 用两个栈实现一个队列的功能
- 用两个栈实现一个队列的功能
- 用两个栈实现一个队列的功能
- [ios开发]判断网络连接是否正常
- 遍历用户提供文件夹下的所有文件夹路径
- 金山一员工在办公室猝死 死因正在调查中
- 设计模式C++描述----01.单例(Singleton)模式
- RGB颜色查询对照表
- 用两个栈实现一个队列的功能
- 菜鸟 Bundler 在 Windows 下的安装、配置和使用 方法以及cygwin的安装和卸载方法汇总
- C++:探讨i++和++i
- 全新Windows 8开发者初学包和虚拟实验室发布
- C/C++中*、&用法
- SQL处理语句步骤
- HDU 2160母猪的故事
- 能够匹配任意长度的回文字的shell脚本 match_palindrome.sh
- ViewPager介绍和使用说明