学习目标:栈的顺序存储实现
学习内容:
1、进栈(顺序存储)
#define MaxSize 10
typef struct{ //结构体里面有着数组和top指针
ElemType data[MaxSize];
int top;
}Stack;
//新元素入栈,先判断栈是否已满
bool Push(Stack &s, ElemType x){
if(s.top == MaxSize-1){
return false;
}
s.top++; //top初始化值为-1
s.data[s.top] = x;
return true;
}
2、 出栈(顺序存储)
#define MaxSize 10
typef struct{ //结构体里面有着数组和top指针
ElemType data[MaxSize];
int top;
}Stack;
//出栈操作
bool Pop(Stack &s, ElemType &x){
if(s.top==-1){
return false; //栈空,报错
}
x = s.data[s.top]; //出栈元素赋给x
s.top--; //top减一
return true;
}
3、 读栈顶元素(顺序存储)
#define MaxSize 10
typef struct{ //结构体里面有着数组和top
ElemType data[MaxSize];
int top;
}Stack;
//读栈顶元素
bool GetTop(Stack &s, ElemType &x){
if(s.top == -1)
return false;
x = s.data[s.top];
return true;
}
学习时间:
1、 2021.6.6 15:30-16:20
学习产出:
1、 数据结构视频3.1.1、3.1.2