顺序栈的实现 C语言版(一)

/*
	顺序栈的实现以及相关操作	C语言版
	作者:S_hmily
	日期:2011年8月31日
	编译环境:VC++6.0
	栈空  top == bottom   bottom始终等于0
	栈满  top == MaxSize
	为了操作的方便,设置下标从1开始,下标为0的元素不使用
*/
/**********************************************************/
#include <stdio.h>
/**********************************************************/
#define	MaxSize	10
#define OK      1   
#define ERROR   0   
#define TRUE    1   
#define FALSE   0 
typedef int		ElemType;
typedef	int		Status;

typedef struct {
	ElemType	data[MaxSize+1];
	int			top;					//栈顶指针
	int			bottom;					//栈底指针
}Stack,  *pStack;
/**********************************************************/
//初始化顺序栈
Status InitStack(pStack S)
{
	S->top = S->bottom = 0;
	return OK;
}
/**********************************************************/
//压栈操作
Status Push_Stack(pStack S, ElemType e)
{
	if (MaxSize == S->top)
		return ERROR;
	S->data[++S->top] = e;
	++S->data[0];
	return OK;
}
/**********************************************************/
//输出顺序栈中所有元素
void DispStack(pStack S)
{
	int i=1;
	while (i<=S->top)
	{
		printf("%d ", S->data[i]);
		++i;
	}
}
/**********************************************************/
//出栈
Status Pop_Stack(pStack S, ElemType *e)
{
	if (S->bottom == S->top)
		return ERROR;
	*e = S->data[S->top--];
	return OK;
}
/**********************************************************/
int main(void)
{
	Stack S;
	int e;
	InitStack(&S);
	
	if (!Pop_Stack(&S, &e))
		printf("栈空");
	else
		printf("\n\n%d", e); 
	return 0;
}


 

你可能感兴趣的:(顺序栈的实现 C语言版(一))