栈的顺栈栈的顺序存储结构及其基本运算实现的顺序存储结构及其基本运算实现序存储结构及其基本运算实现
栈是一种只能在一端进行插入和删除操作的线性表,主要特点:后进先出。
1.初始化栈InitStack(SqStack *&s)
void InitStack(SqStack *&s)
{
s=(SqStack *)malloc(sizeof(SqStack));
s->top=-1;//栈顶指针置为-1
}
2.销毁栈 DestroyStack(s)
void DestroyStack(SqStack *&s)
{
free(s);
}
3.判断栈是否为空StackEmpty(s)
bool StackEmpty(SqStack *s)
{
return (s->top==-1);
}
4.进栈Push(s,e)
bool Push(SqStack *&s,ElemType &e)
{
if(s->top==MaxSize-1)
return false;
s->top++;
s->data[s->top]=e;
return true;
}
5.出栈 Pop(s,e)
bool Pop(SqStack *&s,ElemType &e)
{
if(s->top==-1)
return false;
e=s->data[s->top];
s->top--;
return true;
}
6.取出栈顶元素GetTop(s,e)
bool GetTop(SqStack *s,ElemType &e)
{
if(s->top==-1)
return false;
e=s->data[s->top];
return true;
}