栈(Stack)顺序存储C语言实现
//栈--线性表实现
#define ERROR -1
typedef int Position;
typedef int ElemType;
struct SNode{
ElemType *Data;
Position Top;
int MaxSize;
};
typedef struct SNode *Stack;
Stack CreateStack(int MaxSize){
Stack S = (Stack)malloc(sizeof(struct SNode));
S->Data = (ElemType *)malloc(MaxSize * sizeof(ElemType));
S->Top = -1;
S->MaxSize = MaxSize;
return S;
}
void DestroyStack(Stack S){
if(S == NULL){
return;
}
if(S->Data != NULL){
free(S->Data);
}
free(S);
}
int IsEmpty(Stack S){
return (S->Top == -1);
}
int IsFull(Stack S){
return (S->Top + 1 == S->MaxSize );
}
int Push( Stack S, ElemType data){
if(IsFull(S)){
return ERROR;
}
S->Data[++(S->Top)] = data;
return 1;
}
ElemType Pop( Stack S){
if(IsEmpty()){
return ERROR;
}
return (S->Data[S->Top--]);
}