定义
typedef int Position;
struct SNode {
ElementType *Data;
Position Top;
int MaxSize;
};
typedef struct SNode *Stack;
建立堆栈
Stack CreateStack( int MaxSize ) {
Stack S = (Stack)malloc(sizeof(struct SNode));
S->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
S->Top = -1;
S->MaxSize = MaxSize;
return S;
}
判定堆栈是否空
bool IsEmpty( Stack S ) {
return (S->Top == -1);
}
判定堆栈是否满
bool IsFull( Stack S ) {
return (S->Top == S->MaxSize-1);
}
弹出
ElementType Pop( Stack S ) {
if ( IsEmpty(S) ) {
printf("堆栈空");
return ERROR;
} else
return ( S->Data[(S->Top)--] );
}
压入
bool Push( Stack S, ElementType X ) {
if ( IsFull(S) ) {
printf("堆栈满");
return false;
} else {
S->Data[++(S->Top)] = X;
return true;
}
}