码迷,mamicode.com
首页 > 编程语言 > 详细

C语言顺序栈完整实现

时间:2016-12-07 20:49:51      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:顺序栈   pop   push   注意   include   node   class   bsp   ret   

#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语言顺序栈完整实现

标签:顺序栈   pop   push   注意   include   node   class   bsp   ret   

原文地址:http://www.cnblogs.com/zangkuo/p/6142400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!