用数组实现栈的功能的C语言代码?

来源:百度知道 编辑:UC知道 时间:2024/05/13 13:56:55

栈在处理数组上面真的很方便,这是栈的基础方法函数
//顺序栈的实现stack.cpp
#include "stack.h"
Status SqStack::InitStack(SqStack **S)
{ (*S)=(SqStack *) malloc(sizeof(SqStack));
(*S)->base=(SElemType *)malloc(STACKSIZE *sizeof(SElemType));
if(!(*S)->base) exit(OVERFLOW);
(*S)->top=(*S)->base;
(*S)->stacksize=0;
return 1;}

Status SqStack::DestroyStack()
{free(base);return 1;}

Status SqStack::ClearStack()
{stacksize=0;return 1;}

bool SqStack::StackEmpty()
{ if(stacksize==0) return true;
else return false;
}
int SqStack::StackLength()
{ return stacksize;}
SElemType SqStack::GetTop()
{ if(top==base)
{cerr<<"空栈!\n";exit(1);}
return *(top-1);
}
Status SqStack::Push(SElemType e)
{ *(top++)=e;stacksize++;
return 1;
}
SElemType SqStack::Pop(SElemType *e)
{ if(top==base)
{cerr<<"