1️⃣栈的概念
- 栈(stack) 是限定仅在表尾进行插入或者删除的线性表。
- 对于栈来说,表尾端称为栈顶(top),表头端称为栈低(bottom)。不含元素的空表称为空栈。
- 因为栈限定在表尾进行插入或者删除,所以栈又被称为后进先出的线性表(LIFO)
2️⃣栈的基本操作
- 判空IsEmpty:判断栈是否为空
- 判满IsFull:判断栈是否装满
- 进栈push:向栈顶压入一个元素
- 退栈pop:从栈顶弹出一个元素

3️⃣用数组实现栈的基本操作

- 首先定义一个结构体存放栈的信息,栈为空时令top= - 1,push时top+1,pop时top-1,我们通过top的值就可以判断栈空和栈满的情况
struct stack
{
int top;
int *bottom;
int Size;
};
int IsFull(struct stack *s)
{
if (s->top > s->size - 2){
return 0;
}else{
return 1;
}
}
int IsEmpty(struct stack *s)
{
if (s->top == -1){
return 0;
}else{
return 1;
}
}
- push操作,把栈结构体的地址传入,x为要入栈的数据,每次top+1
int push(struct stack *s, int x)
{
int ret = IsFull(s);
if (ret == 0){
printf("此栈已满\n");
return 0;
}
int *p = s->bottom;
p[++(s->top)] = x;
}
int pop(struct stack *s)
{
int ret = IsEmpty(s);
if (ret == 0){
printf("此栈已空\n");
return 0;
}
s->top--;
}
#include <stdio.h>
#define MAX_SIZE 8
struct stack
{
int top;
int *bottom;
int size;
};
int IsFull(struct stack *s)
{
if (s->top > s->size - 2)
{
return 0;
}
else