源代码
typedef struct
{
ElemType stack[MaxStackSize];
int top;
}SequenceStack;
void StackInitiate(SequenceStack *S)
{
S->top = 0; /*定义初始栈顶的下标值*/
}
int StackNotEmpty(SequenceStack S)
{
if(S.top <= 0)
return 0; /*空栈返回0*/
else
return 1; /*非空栈返回1*/
}
int StackPush(SequenceStack *S, ElemType x)
{
if(S->top >= MaxStackSize)
{
printf("堆栈已满无法插入! \n");
return 0;
}
else
{
S->stack[S->top] = x;
S->top ++;
return 1; /*入栈成功则返回1,否则返回0 */
}
}
int StackPop(SequenceStack *S, ElemType *d)
{
if(S->top <= 0)
{
printf("堆栈已空无数据元素出栈! \n");
return 0;
}
else
{
S->top --;
*d = S->stack[S->top]; /*将栈顶元素值赋予参数d*/
return 1; /*出栈成功返回1,否则返回0*/
}
}
int StackTop(SequenceStack S, ElemType *d)
{
if(S.top <= 0)
{
printf("堆栈已空! \n");
return 0;
}
else
{
*d = S.stack[S.top - 1]; /*当前栈顶数据元素值赋予参数d*/
return 1; /*成功取出返回1,否则返回0*/
}
}