两个栈实现一个队列
#include <stdio.h>
#include <stack>
using namespace std;
template<typename T>
class Queue
{
public:
bool empty()
{
return s1.empty() && s2.empty();
}
void put(const T &e)
{
s1.push(e);
}
T remove()
{ // 未考虑为空的情况
if (s2.empty())
{
while (!s1.empty())
{
T t = s1.top();
s1.pop();
s2.push(t);
}
}
T t = s2.top();
s2.pop();
return t;
}
private:
stack<T> s1, s2;
};
int main()
{
Queue<int> q;
q.put(1);
q.put(2);
q.put(3);
while (!q.empty())
printf("%d ", q.remove());
getchar();
return 0;
}