用两个栈实现队列[数据结构]

#include <iostream>
#include <stack>

using namespace std;
void StackToQueue(void);

int main()
{
	StackToQueue();
	system("pause");
	return 0;
}
//实现stack to queue
void StackToQueue()
{
	stack<int> temp1;
	stack<int> temp2;
	int temp;
	while (1)
	{
		while (1)
		{
			int data_in;
			cin>>data_in;
			if (!data_in)
			{
				break;
			}
			temp1.push(data_in);
		}
		while (1)
		{
			if (temp2.size()==0)
			{
				while (temp1.size())
				{
					temp=temp1.top();
					temp1.pop();
					temp2.push(temp);
				}
			}
			else
			{
				break;
			}
		}
		cout<<temp2.top();
		temp2.pop();
	}
}

点赞