/*********************************************
基本的栈操作,注意栈空时pop输出error的情况也要把接下来的读完
**********************************************/
2:栈的基本操作
- 总时间限制:
- 1000ms
- 内存限制:
- 1000kB
- 描述
-
栈是一种重要的数据结构,它具有push k和pop操作。push k是将数字k加入到栈中,pop则是从栈中取一个数出来。
- 输入
-
第一行为m,表示有m组测试输入,m<100。
每组第一行为n,表示下列有n行push k或pop操作。(n<150)
接下来n行,每行是push k或者pop,其中k是一个整数。
(输入保证同时在栈中的数不会超过100个) - 输出
- 对每组测试数据输出一行。该行内容在正常情况下,是栈中从左到右存储的数字,数字直接以一个空格分隔,如果栈空,则不作输出;但若操作过程中出现栈已空仍然收到pop,则输出error。
- 样例输入
-
- 2
- 4
- push 1
- push 3
- pop
- push 5
- 1
- pop
- 样例输出
-
- 1 5
- error
- # include<iostream>
- using namespace std;
- int main(void)
- {
- int b[155],top,tmp;
- int i,m,n,a;
- char s[5];
- cin>>m;
- while(m--)
- {
- tmp=0;
- top=-1;
- cin>>n;
- while(n--)
- {
- cin>>s;
- if(s[1]=='o')
- {
- if(top==-1)
- {
- tmp=1;
- }
- top--;
- }
- else
- {
- cin>>a;
- b[++top]=a;
- }
- }
- if(tmp)
- {
- cout<<"error"<<endl;
- continue;
- }
- if(top!=-1)
- {
- for(i=0; i<=top; i++)
- cout<<b[i]<<" ";
- cout<<endl;
- }
- }
- return 0;
- }