这道题主要是考察栈的进栈和出栈操作,需要注意的是输出是要注意输出的格式,看清题目要求。
代码如下:
#include <malloc.h>
#include <iostream>
#define maxsize 100
using namespace std;
typedef struct{/*栈的定义*/
int data[maxsize];
int top;
}Seqstack;
Seqstack *Initstack(){/*初始化栈*/
Seqstack *s;
s=new Seqstack;
s->top=-1;
return s;
}
Seqstack *Pushstack(Seqstack *s,int e){/*进栈函数*/
if(s->top==maxsize-1)
cout<<"OVERFLOW"<<endl;
s->top++;
s->data[s->top]=e;
return s;
}
int main(){
int m,n,t,d;
char h;
cin>>t;
while(t--){
Seqstack *s;
s=Initstack();
cin>>m>>n;
while(n--){
cin>>h;
if(h=='P'){
cin>>d;
if(s->top==m-1){
if(n==0)
cout<<"F";
else
cout<<"F"<<endl;
}
else
s=Pushstack(s,d);
}
else if(h=='A'){
if(s->top==-1){
if(n==0)
cout<<"E";
else
cout<<"E"<<endl;
}
else{
if(n==0)
cout<<s->data[s->top];
else
cout<<s->data[s->top]<<endl;
}
}
else if(h=='O'){
if(s->top==-1){
if(n==0)
cout<<"E";
else
cout<<"E"<<endl;
}
else
{
if(n==0)
cout<<s->data[s->top];
else
cout<<s->data[s->top]<<endl;
s->top--;
}
}
}
if(t>=1){
cout<<endl;
cout<<endl;
}
}
return 0;
}