typedef struct Node{
int data;
struct Node *pNext;
}NODE_T, *PNODE_T;
typedef struct Stack{
PNODE_T pTop;
PNODE_T pBottom;
}STACK_T, *PSTACK_T;
void Init_Stack(PSTACK_T pStack);
bool Push_Stack(PSTACK_T pStack, int val);
bool Pop_Stack(PSTACK_T pStack, int *pVal);
void Traverse_Stack(PSTACK_T pStack);
void Clear_Stack(PSTACK_T pStack);
int main(void)
{
STACK_T stack;
int val;
Init_Stack(&stack);
Push_Stack(&stack, 53);
Push_Stack(&stack, 158);
Push_Stack(&stack, -34);
Push_Stack(&stack, -12);
Push_Stack(&stack, 8);
Push_Stack(&stack, 68);
Traverse_Stack(&stack);
Clear_Stack(&stack);
Traverse_Stack(&stack);
//if(Pop_Stack(&stack, &val)){
//printf("Pop Stack Success!,val = %d\n", val);
//}
//if(Pop_Stack(&stack, &val)){
//printf("Pop Stack Success!,val = %d\n", val);
//}
//if(Pop_Stack(&stack, &val)){
//printf("Pop Stack Success!,val = %d\n", val);
//}
//Traverse_Stack(&stack);
return 0;
}
void Init_Stack(PSTACK_T pStack)
{
pStack->pTop = (PNODE_T)MALLOC(NODE_T);
if(NULL == pStack->pTop){
printf("Stack Create Fail!\n");
}else{
pStack->pBottom = pStack->pTop;
pStack->pBottom->pNext = NULL;
pStack->pBottom->data = 0;
}
}
bool Push_Stack(PSTACK_T pStack, int val)
{
PNODE_T pNew = (PNODE_T)MALLOC(NODE_T);
if(NULL == pNew){
printf("Push Stack Fail!\n");
return false;
}else{
pNew->data = val;
pNew->pNext = pStack->pTop;
pStack->pTop = pNew;
return true;
}
}
bool Is_Empty(PSTACK_T pStack)
{
if(pStack->pTop == pStack->pBottom)
return true;
else
return false;
}
bool Pop_Stack(PSTACK_T pStack, int *pVal)
{
if( Is_Empty(pStack) ){
printf("Pop Stack Fail!\n");
return false;
}else{
(*pVal) = pStack->pTop->data;
pStack->pTop = pStack->pTop->pNext;
return true;
}
}
void Traverse_Stack(PSTACK_T pStack)
{
if( Is_Empty(pStack) ){
printf("stack is empty!\n");
}else{
PNODE_T p = pStack->pTop;
while(p != pStack->pBottom)
{
printf("%xH: %d;\n", p,p->data);
p = p->pNext;
}
}
return;
}
void Clear_Stack(PSTACK_T pStack)
{
if( Is_Empty(pStack) ){
printf("stack is empty!\n");
}else{
PNODE_T p = pStack->pTop;
while(p != pStack->pBottom)
{
FREE(p);
p = p->pNext;
}
pStack->pTop = pStack->pBottom;
}
return;
}