/*
*
* 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;
}
stack
最新推荐文章于 2024-09-07 12:34:03 发布