定义
typedef int ElemType;
typedef struct linknode{
ElemType data;
struct linknode* next;
}LinkStack;
初始化
void InitStack(LinkStack*& s)
{
s = (LinkStack*)malloc(sizeof(LinkStack));
s->next = NULL;
}
销毁
void DestoyStack(LinkStack*& s)
{
LinkStack* pre=s, * p=s->next;
while (p != NULL)
{
free(pre);
pre = p;
p = p->next;
}
free(pre);
}
判空
bool StackEmpty(LinkStack* s)
{
return (s->next==NULL);
}
进栈
void Push(LinkStack*& s, ElemType e)
{
LinkStack* p;
p = (LinkStack*)malloc(sizeof(LinkStack));
p->data = e;
p->next = s->next;
s->next = p;
}
出栈
bool Pop(LinkStack*& s, ElemType& e)
{
if (s->next == NULL)
return false;
LinkStack* p = s->next;
e = p->data;
s->next = p->next;
free(p);
return true;
}
读取栈顶
bool GetTop(LinkStack* s, ElemType& e)
{
if (s->next == NULL)
return false;
e = s->next->data;
return true;
}