C++:利用两个栈实现队列操作
来源:互联网 发布:手机如何开淘宝店铺 编辑:程序博客网 时间:2024/05/21 12:10
#include<iostream>
#include<stack>
using namespace std;
class queue
{
private:
stack<int> s1,s2;
public:
void qpush(int a)
{
s1.push(a);
}
int qsize()
{
return s1.size();
}
bool qempty()
{
return s1.empty();
}
void stos(stack<int> &s1,stack<int> &s2)
{
while(!s2.empty())
{
s1.push(s2.top());
s2.pop();
}
}
int qpop()
{
this->stos(s2,s1);
int b=s2.top();
s2.pop();
this->stos(s1,s2);
return b;
}
};
int main()
{
queue qq;
qq.qpush(1);
qq.qpush(2);
qq.qpush(3);
qq.qpush(4);
cout<<qq.qsize()<<endl;
cout<<qq.qpop()<<endl;
cout<<qq.qpop()<<endl;
cout<<qq.qpop()<<endl;
cout<<qq.qpop()<<endl;
return 0;
}
#include<stack>
using namespace std;
class queue
{
private:
stack<int> s1,s2;
public:
void qpush(int a)
{
s1.push(a);
}
int qsize()
{
return s1.size();
}
bool qempty()
{
return s1.empty();
}
void stos(stack<int> &s1,stack<int> &s2)
{
while(!s2.empty())
{
s1.push(s2.top());
s2.pop();
}
}
int qpop()
{
this->stos(s2,s1);
int b=s2.top();
s2.pop();
this->stos(s1,s2);
return b;
}
};
int main()
{
queue qq;
qq.qpush(1);
qq.qpush(2);
qq.qpush(3);
qq.qpush(4);
cout<<qq.qsize()<<endl;
cout<<qq.qpop()<<endl;
cout<<qq.qpop()<<endl;
cout<<qq.qpop()<<endl;
cout<<qq.qpop()<<endl;
return 0;
}
0 0
- C++:利用两个栈实现队列操作
- C++:利用两个队列实现栈的操作
- 利用两个栈实现队列的--->基本操作
- 利用两个栈实现一个队列
- 利用两个栈实现的队列
- 利用两个栈实现队列的功能
- 利用两个栈实现一个队列
- 利用两个队列实现一个栈
- Java:如何利用两个栈实现队列
- 两个栈实现一个队列(C/C++)
- 《算法》第一章——利用两个栈实现一个队列的push和pop操作
- 用两个栈实现队列操作
- (java)两个队列实现栈的操作
- 用两个栈实现队列的操作
- 用两个栈实现队列的操作
- 利用栈实现队列(C语言实现)
- 【C++】两个队列实现一个栈
- 【C++】两个栈实现一个队列
- Java计算两日期之间的天数
- iOS 之awakeFromNib 和 viewDidLoad的区别
- MTK_android_如何预置APK(整合MTK回复)
- Cocos2d-x教程(4)-创建动画效果,TexturePacker工具
- Linux命令之basename使用
- C++:利用两个栈实现队列操作
- Web设计原则
- Android 正则表达式开发中常用方法整理
- hdu 5040 Instrusive 2014 ACM/ICPC Asia Regional Beijing Online bfs
- struts2返回JSON配置
- struts线程安全
- STL源码解析1—六大组件关系
- Cocos2d-x教程(5)-播放音乐与音效
- 用户级线程和内核级线程