数据结构中的堆栈实现---顺序表(附完整代码)

栈的顺序存储实现,完整运行代码如下,运行结果如下:

#include
#include
typedef int Position;
typedef struct SNode * PtrToSNode;
struct SNode{
	int * Data;
	Position Top;
	int MaxSize;
};
typedef PtrToSNode Stack;


//顺序栈的创建
Stack CreateStack(int MaxSize){
	Stack S = (Stack)malloc(sizeof(struct SNode));
	S->Data = (int *)malloc(MaxSize * sizeof(int));
	S->Top =-1;
	S->MaxSize=MaxSize;
	return S;
} 

//顺序栈的入栈操作
bool IsFull(Stack S){
	return (S->Top==S->MaxSize-1);
}
bool Push(Stack S,int X){
	if(IsFull(S)){
		printf("堆栈满\n");
	}
	else{
		S->Data[++(S->Top)]=X;
		printf("入栈成功\n");
	}
}

//顺序栈的出栈操作
bool IsEmpty(Stack S){
	return(S->Top==-1);
} 
int Pop(Stack S){
	if(IsEmpty(S)){
		printf("堆栈空\n");
	}
	else{
		return (S->Data[(S->Top)--]);
	}
}
int main(){
	//初始化堆栈 
	Stack S = CreateStack(10);
	int i,n,z,p;
	//入栈 
	printf("入栈个数为: ");
	scanf("%d",&n);
	printf("输入入栈元素: ");
	for(i=0;i

运行结果:

数据结构中的堆栈实现---顺序表(附完整代码)_第1张图片

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(数据结构,堆栈)