一、栈的基本操作
![图片](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91cGxvYWRlci5zaGltby5pbS9mL0RoQWJYRFR4NEJaRjJLVDIucG5nIXRodW1ibmFpbA?x-oss-process=image/format,png)
二、栈的顺序存储结构
![图片](https://i-blog.csdnimg.cn/blog_migrate/04b41f147da4431612ffc02408c0750c.png)
#define MaxSize 50
#define ElemType int
typedef struct
{
ElemType data[MaxSize];
int top;
}SqStack;
2.1 栈的初始化
void InitStack(SqStack *&s){
s = (SqStack*)malloc(sizeof(SqStack));
s->top=-1;
}
2.2 判断栈空
bool IsEmpty(SqStack *&s){
return s->top == -1;
}
2.3 栈的进栈
bool Push(SqStack *&s, ElemType e){
if(s->top == -1)
return false;
s->data[++s->top] = e;
return true;
}
2.4 栈的出栈
bool Pop(SqStack *&s, ElemType &e){
if(s->top == -1)
return false;
e = s->data[s->top--];
return true;
}
2.5 读出栈顶元素
bool GetTop(SqStack *&s, ElemType &e){
if(s->top==-1)
return false;
e = s->data[s->top];
return true;
}
2.6 共享栈
![图片](https://i-blog.csdnimg.cn/blog_migrate/7d20751f551240b7cf1f482d377e825d.png)
三、栈的链式存储结构
![图片](https://i-blog.csdnimg.cn/blog_migrate/a06cfeb25fd8a91c6d863ddb67bd6657.png)
typedef struct LinkNode
{
ElemType data;
struct LinkNode *next;
}LinkStack;