#include <iostream>
/* Author lanxiaofang
* Date 20200925
* Topic SqStack
*/
// 顺序栈的类型定义
#define MAXLEN 100
typedef int DataType;
typedef struct{
DataType data[MAXLEN];
int top;
}SeqStack;
// 初始化栈
void InitStack(SeqStack &S){
S.top = -1;
printf("--InitStack--初始化栈成功~\n");
}
// 判断栈空
int EmptyStack(SeqStack S){
if(S.top == -1){
printf("--EmptyStack--栈空情况~\n");
return 1;
}
return 0;
}
// 判断栈满
int FullStack(SeqStack S){
if(S.top == MAXLEN - 1){
printf("--FullStack--已经栈满~\n");
return 1;
}
return 0;
}
// 进栈操作
int Push(SeqStack &S, DataType x){
if(FullStack(S)){
printf("--Push--栈满,不能进栈\n");
return 0;
}else{
S.top++;
S.data[S.top] = x;
printf("--Push-- %c 已入栈\n",x);
return 1;
}
}
//出栈操作
int Pop(SeqStack &S, DataType &x){
if(EmptyStack(S)){
printf("--Pop--栈空,不能出栈");
return 0;
}else{
x = S.data[S.top];
S.top--;
printf("--Pop-- %c 已出栈\n",x);
return 1;
}
}
//取栈顶元素
int GetTop(SeqStack S, DataType &x){
if(EmptyStack(S)){
printf("--GetTop--栈空,取栈顶元素失败!");
return 0;
}else{
x = S.data[S.top];
printf("--GetTop-- 栈顶元素是 %c \n",x);
return 1;
}
}
int main(int argc, char** argv) {
SeqStack S;
DataType x;
InitStack(S); // 初始化栈
EmptyStack(S); //判断栈空
Push(S,'L'); // 入栈
Push(S,'X');
Push(S,'F');
Push(S,'9');
Push(S,'8');
EmptyStack(S); //判断栈空
GetTop(S,x); // 取栈顶元素
while(!EmptyStack(S)){
Pop(S,x);
}
return 0;
}
数据结构作业--顺序栈的基本操作
猜你喜欢
转载自blog.csdn.net/c_lanxiaofang/article/details/108792270
今日推荐
周排行