/* 数据结构 栈的实现 Stack.h
* author : star
*/
//防止重复定义
#ifndef STACK_H
#define STACK_H
#include "defs.h"
//初始化大小为n的栈
int InitSqstack(SQSTACK *S, int n)
{
S->data = (ElemType *)malloc(n * sizeof(ElemType));
if(S->data == NULL)
return 0;
S->size = n;
S->top = -1;
return 1;
}
//销毁栈
void DestroySqstack(SQSTACK *S)
{
free(S->data);
S->data = NULL;
S->top = -1;
S->size = 0;
}
//判断栈空、栈满
int IsSqstackEmpty(SQSTACK S)
{
return S.top == -1;
}
int IsSqstackFull(SQSTACK S)
{
return S.top == S.size-1;
}
//入栈
int PushS(SQSTACK *S, ElemType e)
{
if(IsSqstackFull(*S))
return 0;
++S->top;
S->data[S->top] = e;
return 1;
}
//出栈
int PopS(SQSTACK *S, ElemType *e)
{
if(IsSqstackEmpty(*S))
return 0;
*e = S->data[S->top];
--S->top;
return 1;
}
//初始化链式栈
int InitLinkstack(LINKSTACK *S)
{
*S = (LINKSTACK)malloc(sizeof(LSNODE));
if(*S == NULL)
return 0;
(*S)->next = NULL;
return 1;
}
//销毁链式栈
int DestroyLinkstack(LINKSTACK *S)
{
LSNODEPTR q, p;
p = *S;
while(p)
{
q = p;
p = p->next;
free(q);
}
*S = NULL;
}
//判断链栈空、满
IsLinkstackEmpty(LINKSTACK S)
{
return S->next == NULL;
}
IsLinkstackFull(LINKSTACK S)
{
return S->next != NULL;
}
//入链栈
int PushL(LINKSTACK S, ElemType e)
{
LSNODEPTR p;
p = (LSNODEPTR)malloc(sizeof(LSNODE));
if(p == NULL)
return 0;
p->data = e;
p->next = S->next;
S->next = p;
return 1;
}
//出链栈
int PopL(LINKSTACK S, ElemType *e)
{
LSNODEPTR p;
p = S->next;
if(p == NULL)
return 0;
S->next = p->next;
*e = p->data;
free(p);
return 1;
}
#endif