栈的顺序存储结构通常由一个以为数组和一个记录栈顶元素位置的变量组成。
#define MaxSize //<储存数据元素的最大个数>
typedef struct SNode*Stack;
struct SNode{
ElementType Data[MaxSize];
int Top;
};
//(1)入栈
void Push(Stack PtrS, ElementType item)
{
if(PtrS->Top == MaxSize - 1){
print("堆栈满"); return ;
}else{
PtrS -> Data[++(PtrS -> Top)] = item;
return ;
}
}
//(2)出栈
ElementType Pop(Stack PtrS)
{
if(PtrS -> Top == -1){
printf("堆栈空");
return ERROR;//ERROR是ElementType的特殊值,标志
//错误
}else
return (PtrS -> Data[(Ptrs -> Top)--]);
}