数据结构复盘——顺序栈

#include 
#include 

typedef struct SNode *PtrToSNode;

//定义
struct SNode{
	int *data;
	int top;
	int MAXSIZE;
};
typedef PtrToSNode Stack;

//初始化
Stack CreateStack(int MAXSIZE){
	Stack S;
	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("表已满!");
		return false;
	}
	else{
		S->data[++(S->top)]=x;
		return true;
	}
}

//判空
bool IsEmpty(Stack S){
	return (S->top==-1);
}

//出栈
int pop(Stack S){
	if(IsEmpty(S)){
		printf("表已空!");
		return -1;
	}else{
		return (S->data[(S->top)--]);
	}
}

//主函数
void main(){
	Stack S=CreateStack(10);
	int n,x,p;

	printf("请输入入栈个数:");
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&x);
		push(S,x);
	}

	while(!IsEmpty(S)){
		p=pop(S);
		printf("%d ",p);
	}
	printf("\n");
}

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