栈的顺序存储实现

栈的顺序存储结构通常由一个以为数组和一个记录栈顶元素位置的变量组成。

#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)--]);
}
发布了28 篇原创文章 · 获赞 3 · 访问量 1894

猜你喜欢

转载自blog.csdn.net/qq_44045101/article/details/104066205