#include<stdio.h>
#include<algorithm>
using namespace std;
typedef struct DataNode{
int elem;
struct DataNode *next;
}DataNode;
typedef struct {
int count;
DataNode *next;
}HeadNode,*hnode;
int InitStack(hnode *S)
{
hnode p=(HeadNode *)malloc(sizeof(HeadNode));
if(p==NULL)
{
return -1;
}
p->count=0;
p->next = NULL;
(*S)=p;
return 0;
}
int Push(hnode *S,int elem)
{
DataNode *s=(DataNode *)malloc(sizeof(DataNode));
if(s==NULL)
{
return -1;
}
s->elem = elem;
s->next = (*S)->next;
(*S)->next=s;
(*S)->count+=1;
return 0;
}
int Pop(hnode *S,int *elem)
{
if((*S)==NULL)//栈不存在
{
return -1;
}
DataNode *s=(*S)->next;
if(s==NULL)//空栈
{
return -1;
}
*elem = s->elem;
(*S)->next = s->next;
free(s);
(*S)->count-=1;
return 0;
}
int ClearStack(hnode *S)
{
DataNode *p;
if(p==NULL)
{
return -1;
}
while((*S)->count==0)
{
p=(*S)->next;
(*S)->next=p->next;
free(p);
(*S)->count-=1;
}
return 0;
}
int Deatory(hnode *S)
{
DataNode *p;
while((*S)->next != NULL)
{
p=(*S)->next;
(*S)->next = p->next;
free(p);
}
free(*S);
(*S)=NULL;
return 0;
}
int Stacklength(hnode S)
{
return S->count;
}
int Getelem(hnode S,int *elem)
{
DataNode *s=S->next;
if(s==NULL)
{
return -1;
}
*elem = s->elem;
return 0;
}
int Traverse(hnode S)
{
DataNode *s=S->next;
for(int i=0;i<S->count;i++)
{
printf("%d",s->elem);
s=s->next;
}
return 0;
}