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;
}
0 0
原创粉丝点击