#include "stdafx.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
/*此代码并未考虑边界和错误处理,只体现思路*/
typedef unsigned char BOOL;
#define TRUE 1
#define FALSE 0
typedef struct _stack
{
int* stack_bottom_ptr;
int* stack_top_ptr;
int stacksize;
}_stack;
_stack* StackInit(int stackSize)
{
_stack* myStack = (_stack*)malloc(sizeof(_stack));
myStack->stacksize = stackSize;
myStack->stack_bottom_ptr = (int*)malloc(sizeof(int)*stackSize);
myStack->stack_top_ptr = myStack->stack_bottom_ptr;
return myStack;
}
static BOOL Stack_push(_stack* myStack,int dat)
{
if( myStack->stack_top_ptr - myStack->stack_bottom_ptr >= myStack->stacksize) //myStack->stack_top_ptr - myStack->stack_bottom_ptr/sizeof(int) 此处为何不除以sizeof(int)? 单步调试是发现不除以就已经为1,2,3...
{
printf("stack flow! \r\n");
return FALSE;
}
*myStack->stack_top_ptr = dat;
myStack->stack_top_ptr ++;
printf(" Stack_push = %d! \r\n",dat);
return TRUE;
}
static int Stack_pop(_stack* myStack)
{
if(myStack->stack_top_ptr == myStack->stack_bottom_ptr)
{
printf("stack empty! \r\n");
return FALSE;
}
myStack->stack_top_ptr --;
printf(" Stack_pop = %d! \r\n",*myStack->stack_top_ptr);
return *myStack->stack_top_ptr;
}
int main()
{
int ret = 0;
_stack* t_stack = StackInit(3);
Stack_push(t_stack,10);
Stack_push(t_stack,20);
Stack_push(t_stack,30);
Stack_push(t_stack,40); //printf("stack flow! \r\n");
ret = Stack_pop(t_stack);
ret = Stack_pop(t_stack);
ret = Stack_pop(t_stack);
ret = Stack_pop(t_stack); //printf("stack empty! \r\n");
return 0;
}