#include <cstdlib>
#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(int argc, char *argv[])
{
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();
}
system("PAUSE");
return EXIT_SUCCESS;
}
c++ 两个栈实现队列
最新推荐文章于 2025-03-27 13:19:14 发布