[c]代码库
//置空栈:首先建立栈空间,然后初始化栈顶指针。
SeqStack *Init_SeqStack()
{
SeqStack *s;
s=malloc ( sizeof ( SeqStack ) );
s->top= -1;
return s;
}
//判空栈
int Empty_SeqStack ( SeqStack *s )
{
if ( s->top= = -1 ) return 1;
else return 0;
}
//入栈
int Push_SeqStack ( SeqStack *s, datatype x )
{
if ( s->top= =MAXSIZE-1 ) return 0; /*栈满不能入栈*/
else
{
s->top++;
s->data[s->top]=x;
return 1;
}
}
//出栈
int Pop_SeqStack ( SeqStack *s, datatype *x )
{
if ( Empty_SeqStack ( s ) ) return 0; /*栈空不能出栈*/
else
{
*x=s->data[s->top];
s->top--;
return 1;
} /*栈顶元素存入*x,返回*/
}
//取栈顶元素
datatype Top_SeqStack ( SeqStack *s )
{
if ( Empty_SeqStack ( s ) ) return 0; /*栈空*/
else return ( s->data[s->top] );
}