顺序栈的实现 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;
}


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值