A - ACboy needs your help again! (HDU - 1702)

本文介绍了一种使用队列和栈解决特定问题的方法。通过读取输入命令,选择使用队列(FIFO)或栈(FILO)进行数据操作。对于每种结构,程序根据IN指令添加元素,或根据OUT指令移除并输出顶部或头部元素。

- 题目大意

    给出了先进先出和先进后出的两种结构,分别对应队列和栈,并且每种均给出In和Out两类操作,如果是In,push进后面的数,如果是Out,输出栈顶(队首)。

- 解题思路

    对于给的命令判断,然后来决定是用队列还是栈。

- 代码

#include<iostream>
#include<stack>
#include<queue>

using namespace std;
int main()
{
	int n,x,b;
	char a[5];
	char c[5];
	cin >> n;
	while (n--)
	{
		cin >> x >> a;
		
		if (strcmp(a, "FIFO"))
		{
			stack<int>num;
			while (x--)
			{
				cin >> c;
				if (!strcmp(c, "IN"))
				{
					cin >> b;
					num.push(b);
				}
				else
				{
					if (num.empty())
					{
						cout << "None" << endl;
					}
					else
					{
						cout << num.top() << endl;
						num.pop();
					}
				}
			}
		}

			else
			{
				queue<int>sum;
				while (x--)
				{
				cin >> c;
				if (!strcmp(c, "IN"))
				{
					cin >> b;
					sum.push(b);
				}
				else
				{
					if (sum.empty())
					{
						cout << "None" << endl;
					}
					else
					{
						cout << sum.front() << endl;
						sum.pop();
					}
				}
			}
		}
	}
	return 0;
}

  

转载于:https://www.cnblogs.com/alpacadh/p/8438469.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值