stack

/*

* 

* stack

* chenbdchenbd@gmail.com

*

*/

#include <stdio.h>

#include <memory.h>



#define	STACK_BUF_LEN		(5)



#define	STACK_OK		(0x00)

#define	STACK_NG		(0xff)



//#define	STACK_INVALID_IDX	(-1)



#ifdef _DEBUG

#define STACK_DEBUG(x)	x

#else

#define	STACK_DEBUG(x)

#endif



typedef struct

{

	int i_top;

	int	i_buf[STACK_BUF_LEN];

}S_STACK_t;



S_STACK_t st_STACK;



int	stack_init(S_STACK_t* stack)

{

	int i_ret = STACK_NG;



	if (NULL != stack)

	{

		stack->i_top =0;

		memset(stack->i_buf, 0x00, sizeof(stack->i_buf));

		i_ret = STACK_OK;

		STACK_DEBUG(printf("stack: i_top = %08x/n",stack->i_top));

	}



	return i_ret;

}



int stack_push(S_STACK_t* stack, int i_val)

{

	int i_ret = STACK_NG;



	if (NULL != stack)

	{

		if ((STACK_BUF_LEN -1) != stack->i_top)

		{

			stack->i_buf[stack->i_top++] = i_val;

			i_ret = STACK_OK;

			STACK_DEBUG(printf("stack: i_top = %08x/n",stack->i_top));

		}

		else

		{

			STACK_DEBUG(printf("stack:stack is full!!!/n"));

		}

	}



	return i_ret;

}



int stack_pop(S_STACK_t* stack, int* i_out)

{

	int i_ret = STACK_NG;



	if (NULL != stack)

	{

		if (0 <= (stack->i_top - 1))

		{

			if (NULL != i_out)

			{

				*i_out = stack->i_buf[--stack->i_top];

				i_ret = STACK_OK;

				STACK_DEBUG(printf("stack: i_top = %08x/n",stack->i_top));

			}

		}

		else

		{

			STACK_DEBUG(printf("stack:stack is empty!!!/n"));

		}

	}



	return i_ret;

}



int stack_empty(S_STACK_t* stack)

{

	int i_ret = STACK_NG;



	if (NULL != stack)

	{

		if (0 == stack->i_top)

		{

			i_ret = STACK_OK;

			STACK_DEBUG(printf("stack:stack is empty!!!/n"));

		}

	}



	return i_ret;

}



int stack_full(S_STACK_t* stack)

{

	int i_ret = STACK_NG;



	if (NULL != stack)

	{

		if (STACK_BUF_LEN == stack->i_top)

		{

			i_ret = STACK_OK;

			STACK_DEBUG(printf("stack:stack is full!!!/n"));

		}

	}



	return i_ret;

}



int main (void)

{

	int i_tmp;



	stack_init(&st_STACK);

	stack_push(&st_STACK, 1);

	stack_push(&st_STACK, 2);

	stack_push(&st_STACK, 3);

	stack_push(&st_STACK, 4);

	stack_push(&st_STACK, 5);

	stack_push(&st_STACK, 6);

	stack_full(&st_STACK);

	stack_pop(&st_STACK, &i_tmp);

	stack_pop(&st_STACK, &i_tmp);

	stack_pop(&st_STACK, &i_tmp);

	stack_pop(&st_STACK, &i_tmp);

	stack_pop(&st_STACK, &i_tmp);

	stack_pop(&st_STACK, &i_tmp);

	stack_empty(&st_STACK);

	return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值