作者:埋葬曾经的一切 | 来源:互联网 | 2022-10-08 03:29
代码如下所示:
代码如下:
// StackToQueue.cpp : 定义控制台应用程序的入口点。
//用两个标准容器stack,实现一个队列
#include "stdafx.h"
#include
#include
using namespace std;
template
class StackToQueue
{
public:
StackToQueue()
{
stack1;
stack2;
}
void push(T e)
{
while (!stack2.empty())
{
T temp;
temp = stack2.top();
stack2.pop();
stack1.push(temp);
}
stack2.push(e);
while (!stack1.empty())
{
T temp;
temp = stack1.top();
stack1.pop();
stack2.push(temp);
}
}
void pop()
{
stack2.pop();
}
T front()
{
if (!empty())
{
return stack2.top();
}
else
{
return NULL;
}
}
bool empty()
{
return stack2.empty();
}
size_t size()
{
return stack2.size();
}
private:
stack stack1, stack2;
};
int _tmain(int argc, _TCHAR* argv[])
{
StackToQueue queue;
int i(0);
cout <<"Enter several integer number,and press ctrl+z to the end." < while (cin >> i)
{
queue.push(i);
}
cout <<"The front element is: " < cout <<"The size now is: " < if (!queue.empty())
{
cout <<"Pop one element now." < queue.pop();
}
cout <<"The front element is: " < cout <<"The size now is: " < return 0;
}