#include<iostream>
#include<stack>
using namespace std;
//两个栈实现一队列 栈先进后出
template<typename T>
class Queue
{
public:
Queue()
{}
void Push(const T& x)
{
stack<T>* tmp=NULL;
if (!s1.empty())//
{
tmp = &s1;
}
else
{
tmp = &s2;
}
tmp->push(x);
}
void Pop()
{
if (s1.empty() && s2.empty())
{
cout << "NULL" << endl;
return;
}
stack<T>* input=NULL;//输入(存储要处理的数据)
stack<T>* output=NULL;//输出(数据所在的地方)
if (!s1.empty())//s1非空
{
input = &s2;
output = &s1;
}
else
{
input = &s1;
output = &s2;
}
while (!output->empty())
{
input->push(output->top());
output->pop();
}
input->pop();
while (!input->empty())
{
output->push(input->top());
input->pop();
}
//swap(s1, s2);
}
private:
stack<T> s1;
stack<T> s2;
};
两个栈实现一个队列
最新推荐文章于 2023-03-22 22:40:14 发布