简单的模拟栈和队列

Description

ACboy was kidnapped!!
he miss his mother very much and is very scare now.You can’t image how dark the room he was put into is, so poor 😦.
As a smart ACMer, you want to get ACboy out of the monster’s labyrinth.But when you arrive at the gate of the maze, the monste say :" I have heard that you are very clever, but if can’t solve my problems, you will die with ACboy."
The problems of the monster is shown on the wall:
Each problem’s first line is a integer N(the number of commands), and a word “FIFO” or “FILO”.(you are very happy because you know “FIFO” stands for “First In First Out”, and “FILO” means “First In Last Out”).
and the following N lines, each line is “IN M” or “OUT”, (M represent a integer).
and the answer of a problem is a passowrd of a door, so if you want to rescue ACboy, answer the problem carefully!

Input

The input contains multiple test cases.
The first line has one integer,represent the number oftest cases.
And the input of each subproblem are described above.

Output

For each command “OUT”, you should output a integer depend on the word is “FIFO” or “FILO”, or a word “None” if you don’t have any integer.

Sample Input

4
4 FIFO
IN 1
IN 2
OUT
OUT
4 FILO
IN 1
IN 2
OUT
OUT
5 FIFO
IN 1
IN 2
OUT
OUT
OUT
5 FILO
IN 1
IN 2
OUT
IN 3
OUT

Sample Output

1
2
2
1
1
2
None
2
3

描述

阿博伊被绑架了!!
他非常想念他的母亲, 现在很害怕。你不能把他放在的房间有多暗, 这么可怜的:(。
作为一个聪明的阿克默, 你想让 Acboy 走出怪物的迷宫。但是,当你到达迷宫的大门,蒙斯特说:“我听说你很聪明,但如果你不能解决我的问题,你会死与C男孩。
怪物的问题显示在墙上:每个
问题的第一行是一个整数N(命令的数量),和一个单词"FIFO"或"FILO”。(你很高兴,因为你知道"FIFO"代表"先出",而"FILO"的意思是"第一个在后出")。
和下面的N行,每行是"IN M"或"OUT",(M表示整数)。
问题的回答是一扇门, 所以如果你想拯救 Acboy, 请仔细回答问题!

输入

输入包含多个测试用例。
第一行有一个整数,表示测试案例数。
上面描述了每个子问题的投入。

输出

对于每个命令"OUT",应输出一个整数,该单词依赖于单词是"FIFO"或"FILO",或者如果您没有任何整数,则输出一个单词"无"。

示例输入

4
4 FIFO
IN 1
IN 2
OUT
OUT
4 FILO
IN 1
IN 2
OUT
OUT
5 FIFO
IN 1
IN 2
OUT
OUT
OUT
5 FILO
IN 1
IN 2
OUT
IN 3
OUT

样品输出

1
2
2
1
1
2
None
2
3

题意
这是一题简单的栈和队列的运用题目,FIFO代表按队列的定义输入输出,FILO代表按栈的定义输入输出,如果栈或队列中没有元素,则输出 None。
首先输入的一个整数,表示下面数据的组数。每组数据开头是一个数字和一个字符串,分别表示命令数量和数据类型。
主要思路
首先需要判断是栈还是队列,即是FIFO还是FILO,只需判断字符串的第三位是F还是L。其次判断是输入还是输出(IN/OUT)同理,看第一位是I还是O。这需要注意的一点是,输出(OUT)前如果栈或队列为空,则需要输出None。

代码如下:

#include"iostream"
#include"stack"
#include"queue"
using namespace std;
void main()
{
	stack <int>a;
    queue <int>b;
    int n, m,i,j;
    char c[5],d[4];
    cin >> n ;
    for (i = 1;i <= n;i++)
    {
        cin >> m>> c;
        if (c[2]=='F')
        {
            while (m--)
            {
                cin >> d ;
                if (d[0] == 'I') { cin>>j;b.push(j); }
                else 
                { 
                    if (b.empty()) cout << "None" << endl;
                    else { cout << b.front()<<endl ;b.pop();}
                }
            }
            while (!b.empty()) b.pop();
        }
        else
        {
            while (m--)
            {
                cin >> d ;
                if (d[0]=='I') { cin >> j;a.push(j); }
                else
                {
                    if (a.empty()) cout << "None" << endl;
                    else { cout << a.top() << endl;a.pop();  }
                }
            }
            while (!a.empty()) a.pop();
        }
    }
}

易错点:
在每组数据输出结束后,应把栈/队列清空,即while (!a.empty()) a.pop();这步操作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值