完整代码:
#include<iostream>
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define MAXSIZE 1000
using
namespace
std;
typedef
struct
{
int
data[MAXSIZE];
int
top;
}SqStack;
void
InitStack(SqStack*&s){
s=(SqStack*)
malloc
(
sizeof
(SqStack));
s->top=-1;
}
bool
StackEmpty(SqStack * s){
return
(s->top==-1);
}
bool
Push(SqStack *& s,
int
e){
if
(s->top==MAXSIZE-1)
return
false
;
s->top++;
s->data[s->top]=e;
return
true
;
}
bool
Pop(SqStack *& s){
int
e;
if
(s->top==-1)
return
false
;
e=s->data[s->top];
s->top--;
return
true
;
}
int
GetTop(SqStack*s,
int
&e){
if
(s->top==-1)
return
false
;
e=s->data[s->top];
return
e;
}
void
DestroyStack(SqStack*&s){
free
(s);
}
int
main (){
SqStack *stack;
int
i,k,n,num[1000];
int
e;
cin>>n;
for
(i=0;i<n;i++)
cin>>num[i]; //将要输入的整数存储在数组里不容易出错
InitStack(stack);
for
(k=0;k<n;k++){
if
(num[k]>0)
Push(stack,num[k]);
else
if
(num[k]==-1){
if
(StackEmpty(stack)==
true
){
cout<<
"ERROR"
;
return
0; //这一步骤可以避免在输出ERROR后又接着输出EMPTY
}
else
Pop(stack);
}
}
if
(StackEmpty(stack)==
true
)
cout<<
"EMPTY"
;
else
cout<<GetTop(stack,e)<<endl;
DestroyStack(stack);
return
0;
}