首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

数据结构 栈兑现(练手题)传说中的数据结构

2013-01-26 
数据结构栈实现(练手题)传说中的数据结构传说中的数据结构 Time Limit: 1000MSMemory limit: 65536K 题目

数据结构 栈实现(练手题)传说中的数据结构

传说中的数据结构

Time Limit: 1000MS    Memory limit: 65536K
题目描述输入包含多组测试数据.
每组数据的第一行为一个整数 T(1 <= T <= 1000 ),接下来 T 行为对栈的操作。

输出

如果操作是top,那么输出最后面的数,如果栈中没有数的话,那就输出“empty”(不含引号)。
如果操作是pop且栈是空的,那么输出 “error”(不含引号)。
在每组测试数据的最后多加一次换行。

示例输入
8push 1push 2push 3push 4toppoptoppop3push 1poptop
示例输出
43empty

 

 

#include<cstdio>
#include<stack>
using namespace std;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        stack<int> S;
        while(n--)
        {
            char c[101];
            scanf("%s",c);
            if(c[0]=='p'&&c[1]=='u')
            {
                int t;
                scanf("%d",&t);
                S.push(t);
            }
            else if(c[0]=='p'&&c[1]=='o')
                {
                    if(S.empty())
                    printf("error\n");
                    else
                    S.pop();
                }
            else if(c[0]=='t')
            {
                int qq;
                if(S.empty())
                    printf("empty\n");
                else
                {
                    qq=S.top();
                    printf("%d\n",qq);
                }
            }
        }
        printf("\n");
    }
    return 0;
}


/**************************************
 Problem id : SDUT OJ A
 User name : ACboy
 Result  : Accepted
 Take Memory : 1092K
 Take Time : 0MS
 Submit Time : 2013-01-23 10:35:20 
**************************************/

热点排行