#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define true 1
#define false 0
typedef struct Node
{
int data;
struct Node *pNext;
}NODE, *PNODE;
typedef struct Stack
{
PNODE pTop;
PNODE pBottom;
}STACK, *PSTACK;
void init(PSTACK pS);
void push(PSTACK pS, int val);
void traverse(PSTACK pS);
int pop(PSTACK pS , int *val);
void clear(PSTACK pS);
int empty(PSTACK pS);
int main(void)
{
STACK S ;
int val;
int i;
init(&S);
push(&S,1);
push(&S,2);
push(&S,3);
push(&S,4);
push(&S,5);
push(&S,6);
traverse(&S);
if(pop(&S ,&val))
{
printf("遍历成功,出栈元素为%d\n",val);
}
else
{
printf("出栈失败!\n");
}
traverse(&S);
clear(&S);
traverse(&S);
return 0 ;
}
void init(PSTACK pS)
{
pS -> pTop = (PNODE)malloc(sizeof(NODE));
if(NULL == pS -> pTop)
{
printf("动态内存分配失败!");
exit(-1);
}
else
{
pS -> pBottom = pS -> pTop;
pS -> pTop -> pNext = NULL;
}
return ;
}
void push(PSTACK pS , int val)
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));
pNew -> data = val;
pNew -> pNext = pS -> pTop;
pS -> pTop = pNew;
return ;
}
void traverse(PSTACK pS)
{
PNODE p = pS -> pTop;
printf("栈内元素为:");
while(p != pS -> pBottom)
{
printf("%d\t", p -> data);
p = p -> pNext;
}
printf("\n");
return ;
}
int empty(PSTACK pS)
{
if(pS -> pTop == pS -> pBottom)
{
return true;
}
else
return false;
}
int pop(PSTACK pS , int *val)
{
if(empty(pS))
{
return false;
}
else
{
PNODE r = pS -> pTop;
*val = r -> data;
pS -> pTop = r -> pNext;
free(r);
r = NULL;
}
}
void clear(PSTACK pS)
{
if(empty(pS))
{
return;
}
else
{
PNODE p = pS -> pTop;
PNODE q = NULL;
while(p != pS -> pBottom)
{
q = p -> pNext;
free(p);
p = q ;
}
pS -> pTop = pS -> pBottom;
return;
}
}