#include <stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct Node {
int date;
struct Node *pNext;
}NODE,*PNODE;
typedef struct Stack {
PNODE Top;
PNODE Bottom;
}STACK,*PSTACK;
void init(PSTACK ps)
{
ps->Top =(PNODE)malloc(sizeof(NODE));
if(NULL==ps->Top)
{
printf("mallc err");
exit(-1);
}else
{
ps->Bottom=ps->Top;
ps->Bottom->pNext=NULL;
}
}
void push(PSTACK ps,int val)
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if(NULL==pNew)
{
printf("malloc err");
exit(-1);
}
pNew->date=val;
pNew->pNext=ps->Top;
ps->Top=pNew;
}
void traverse(PSTACK ps)
{
PNODE p =ps->Top;
while(p!=ps->Bottom)
{
printf("%d ",p->date);
p=p->pNext;
}
return ;
}
int empty(PSTACK ps)
{
if(ps->Top==ps->Bottom)
{
return 1;
}else
{
return 0;
}
}
int pop(PSTACK ps,int *val)
{
if(empty(ps))
{
printf("empty");
return 0;
}else
{
PNODE p=ps->Top;
*val = p->date;
ps->Top=p->pNext;
free(p);
p=NULL;
return 1;
}
}
void clear(PSTACK ps)
{
if(empty(ps))
{
printf("is empty");
return;
}else
{
PNODE p = ps->Top;
PNODE q = NULL;
while (p!=ps->Bottom)
{
q=p->pNext;
free(p);
p=q;
}
ps->Top=ps->Bottom;
}
}
int main(void)
{
int val=0;
STACK s ;
init(&s);
push(&s,1);
push(&s,2);
push(&s,3);
push(&s,4);
push(&s,5);
traverse(&s);
return 0;
}