栈的顺序存储结构
#define maxsize 100
typedef struct{
Elemtype data[maxsize];
int top;
}sqstack;
int InitStack(sqstack *s)
{
//创建一个空栈由指针s指向
if((s=(sqstack *)malloc(sizeof(sqstack)))==NULL)
return ERROR;
s->top=-1;
return OK;
}
int StackEmpty(sqstack *s)
{
return (s->t==-1?true:false);
}
int StackFull(sqstack *s)
{
//判栈为满栈时返回值为真
return (s->top==maxsize-1?true:false);
}
//入栈
int Push(sqstack *s,Elemtype e)
{
if(StackFull(s))return error;
s->top++;
s->data[s->top]=e;
return OK;
}
//出栈
int Pop(sqstack *s,Elemtype *e)
{
if(StackEmpty(s))return ERROR;
*e=s->data[s->top];
s->top--;
return OK;
}
//取栈顶元素
int GetTop(sqstack *s,Elemtype *e)
{
if(StackEmpty(s))return ERROR;
*e=s->data[s->top];
return OK;
}
栈的链式存储结构typedef struct node
{
Elemtype data;
struct node* next;
} StackNode,*LinkStack;
LinkStack top;//top为栈顶指针
LinkStack InitStack()
{
return NULL;
}
int StackEmpty(LinkStack top)
{
return(top==NULL?true:false);
}
//进栈
void Push(LinkStack top,Elemtype e)
{
StackNode *s;
s=(StackNode *)malloc(sizeof(StackNode));
s->data=e;
s->next=top;
top=s;
}
//出栈
int Pop(LinkStack top,Elemtype *e)
{
StackNode *p;
if(StackEmpty(top))return error;
*e=top->data;
p=top;
top=top->next;
free(p);
return OK;
}