# include <iostream>
# define MAX_SIZE 20
# define OK 1
# define ERROR 0
# define TRUE 1
# define FALSE 0
typedef int Status;
typedef int ElemType;
typedef struct SqStack
{
ElemType data[ MAX_SIZE] ;
int top;
} ;
Status InitStack ( SqStack* * s)
{
* s = ( SqStack* ) malloc ( sizeof ( SqStack) ) ;
if ( ( * s) == NULL ) return ERROR;
( * s) -> top = 0 ;
return OK;
}
Status DestoryStack ( SqStack* s)
{
if ( s != NULL )
{
free ( s) ;
}
return OK;
}
Status ClearStack ( SqStack* s)
{
s-> top = 0 ;
return OK;
}
Status StackEmpty ( SqStack* s)
{
if ( s-> top > 0 )
return ERROR;
return TRUE;
}
Status Push ( SqStack* s, ElemType e)
{
if ( s == NULL ) return ERROR;
if ( ( s-> top) >= MAX_SIZE) return ERROR;
s-> data[ s-> top] = e;
s-> top++ ;
return OK;
}
Status Pop ( SqStack* s)
{
if ( s == NULL ) return ERROR;
if ( s-> top == 0 ) return ERROR;
s-> top-- ;
}
int StackLength ( SqStack* s)
{
if ( s == NULL ) return ERROR;
return s-> top;
}
Status StackShow ( SqStack* s)
{
if ( s == NULL ) return ERROR;
for ( int i = 0 ; i < s-> top; i++ )
{
printf ( "%d-->" , s-> data[ i] ) ;
}
printf ( "***全部显示完成!\n" ) ;
return OK;
}
int main ( )
{
SqStack* s;
InitStack ( & s) ;
if ( s == NULL )
{
printf ( "内存不足!" ) ;
return - 1 ;
}
for ( int i = 0 ; i < 10 ; i++ )
{
Push ( s, i) ;
}
StackShow ( s) ;
printf ( "删除5个元素:\n" ) ;
Pop ( s) ;
Pop ( s) ;
Pop ( s) ;
Pop ( s) ;
Pop ( s) ;
StackShow ( s) ;
printf ( "是否为空:%d(1:是 0 :否)\n" , StackEmpty ( s) ) ;
ClearStack ( s) ;
printf ( "清空后:\n" ) ;
StackShow ( s) ;
printf ( "是否为空:%d(1:是 0 :否)\n" , StackEmpty ( s) ) ;
DestoryStack ( s) ;
s = NULL ;
return 0 ;
}