首页 > 代码库 > C语言顺序栈完整实现

C语言顺序栈完整实现

#include <stdio.h>
#include <stdlib.h>
const int MAXSIZE = 100;//注意 
#define ElementType int

typedef struct SNode *Stack;
struct SNode{
	ElementType Data[MAXSIZE];
	int Last;
};
Stack CreateStack(int MAXSIZE);
int IsFull(Stack S);
void Push(Stack S,ElementType item);
int IsEmpty(Stack S);
ElementType Pop(Stack S);


int main(){
	Stack S = CreateStack(MAXSIZE);
	for(int i=0;i<11;i++)
		Push(S,i);
	while(!IsEmpty(S)){
		printf("%d\n",Pop(S));
	}
	return 0;
}
Stack CreateStack(int MAXSIZE){
	Stack S = (Stack)malloc(sizeof(SNode));
	S->Last = -1;
	return S;
}
int IsFull(Stack S){
	return S->Last+1 ==MAXSIZE;
}
int IsEmpty(Stack S){
	return S->Last < 0;
}
void Push(Stack S, ElementType item){
	if(IsFull(S)){
		printf("栈满\n");
		return;
	}else{
		S->Data[++S->Last]= item;
		return;
	}
}
ElementType Pop(Stack S){
	if(IsEmpty(S)){
		printf("栈为空\n");
		return NULL;
	}else{
		return S->Data[S->Last--];
	}
}

  

 

C语言顺序栈完整实现