【数据结构】栈的顺序存储(整型栈、字符栈)
- 一、栈的结构定义
- 二、字符栈的初始化、入栈、出栈、判断是否栈为空、获取栈顶元素、获取栈的当前元素个数等操作
- 三、整型栈的初始化、入栈、出栈、判断是否栈为空、获取栈顶元素、获取栈的当前元素个数等操作
一、栈的结构定义
MaxSize: 顺序栈的长度
s.top: 指针,指向栈顶元素。当栈为空时,指向-1。
s.data[MaxSize]: 顺序栈,用数组表示
#define MaxSize 50
typedef struct SqStack{char data[MaxSize]; // 数据 int top; // 指针
}SqStack;
栈空 | s.top = -1 |
栈满 | s.top = MaxSize - 1 |
栈顶元素 | s.data[s.top] |
栈的当前元素个数 | s.top + 1(因为元素从索引0开始,所以元素个数是:索引值+1) |
二、字符栈的初始化、入栈、出栈、判断是否栈为空、获取栈顶元素、获取栈的当前元素个数等操作
#include <stdio.h>
using namespace std;#define MaxSize 50
typedef struct SqStack{char data[MaxSize]; // 数据 int top; // 指针
}SqStack;//InitStack(&s):初始化一个空栈 S。
void InitStack(SqStack &s){s.top = -1;
}//StackEmpty(s):判断一个栈是否为空,若栈s为空则返回 true,否则返回 false。
bool StackEmpty(SqStack s){if(s.top == -1) return true;return false;
}//Push(&s,x): 入栈,若栈s未满,则将x加入使之成为新栈顶。
bool Push(SqStack &s, char x){if(s.top == MaxSize - 1) return false;s.data[++s.top] = x;return true;
}//Pop(&S,&x):出栈,若栈s非空,则弹出栈顶元素,并用x返回。
bool Pop(SqStack &s, char &x){if(s.top == -1) return false;x = s.data[s.top--];return true;
}//GetTop(s,&x):读栈顶元素,但不出栈,若栈s非空,则用x返回顶元素。
bool GetTop(SqStack s, char &x){if(s.top == -1) return false;x = s.data[s.top];return true;
}//Destroystack(&s):销毁栈,并释放栈s占用的存储空间(“&”表示引用调用)。//getNum(s): 获取栈的当前元素个数
int GetNum(SqStack s){if(s.top == -1) return 0;return s.top + 1;
}
int main() {SqStack s;InitStack(s); bool flag = true;int choose;while(flag){printf("\n=======================\n");printf("1.入栈\n");printf("2.出栈\n");printf("3.判断是否栈为空\n");printf("4.读栈顶元素\n");printf("5.获取栈的当前元素个数\n");printf("6.退出\n");printf("=======================\n");printf("请选择:");scanf("%d", &choose);switch(choose) {case 1:char x;printf("输入要新入栈的字符:");scanf(" %c", &x); // 注意,此时%c前面需要加个空格,使得scanf跳过任何空白字符(包括换行符),然后读取下一个非空白字符。 if(Push(s, x)) printf("成功把%c入栈!\n", x);else printf("入栈失败!\n");break;case 2:char x2;if(Pop(s, x2)) printf("%c出栈成功!\n", x2);else printf("出栈失败!\n");break;case 3:if(StackEmpty(s)) printf("栈为空!\n");else printf("栈暂时不为空!\n");break;case 4:char x3;if(GetTop(s, x3)) printf("当前栈的栈顶元素是:%c\n", x3);else printf("出错!\n");break;case 5:printf("栈的当前元素个数是:%d", GetNum(s));break;case 6:flag = false;break; default:printf("非法输入!\n");break; } }
}
三、整型栈的初始化、入栈、出栈、判断是否栈为空、获取栈顶元素、获取栈的当前元素个数等操作
很简单,只是把data数组及参数x的数据类型从char改为int类型就好了。
#include <bits/stdc++.h>
using namespace std;/*
栈空: top = -1
栈满: top = MaxSize - 1
栈顶元素: SqStack[top]
*/
#define MaxSize 50
typedef struct SqStack{int data[MaxSize]; // 数据 int top; // 指针
}SqStack;//InitStack(&s):初始化一个空栈 S。
void InitStack(SqStack &s){s.top = -1;
}//StackEmpty(s):判断一个栈是否为空,若栈s为空则返回 true,否则返回 false。
bool StackEmpty(SqStack s){if(s.top == -1) return true;return false;
}//Push(&s,x): 入栈,若栈s未满,则将x加入使之成为新栈顶。
bool Push(SqStack &s, int x){if(s.top == MaxSize - 1) return false;s.data[++s.top] = x;return true;
}//Pop(&S,&x):出栈,若栈s非空,则弹出栈顶元素,并用x返回。
bool Pop(SqStack &s, int &x){if(s.top == -1) return false;x = s.data[s.top--];return true;
}//GetTop(s,&x):读栈顶元素,但不出栈,若栈s非空,则用x返回顶元素。
bool GetTop(SqStack s, int &x){if(s.top == -1) return false;x = s.data[s.top];return true;
}//Destroystack(&s):销毁栈,并释放栈s占用的存储空间(“&”表示引用调用)。//getNum(s): 获取栈的当前元素个数
int GetNum(SqStack s){if(s.top == -1) return 0;return s.top + 1;
}
int main() {SqStack s;InitStack(s); bool flag = true;int choose;while(flag){printf("\n=======================\n");printf("1.入栈\n");printf("2.出栈\n");printf("3.判断是否栈为空\n");printf("4.读栈顶元素\n");printf("5.获取栈的当前元素个数\n");printf("6.退出\n");printf("=======================\n");printf("请选择:");scanf("%d", &choose);switch(choose) {case 1:int x;printf("输入要新入栈的字符:");scanf("%d", &x);if(Push(s, x)) printf("成功把%d入栈!\n", x);else printf("入栈失败!\n");break;case 2:int x2;if(Pop(s, x2)) printf("%d出栈成功!\n", x2);else printf("出栈失败!\n");break;case 3:if(StackEmpty(s)) printf("栈为空!\n");else printf("栈暂时不为空!\n");break;case 4:int x3;if(GetTop(s, x3)) printf("当前栈的栈顶元素是:%d\n", x3);else printf("出错!\n");break;case 5:printf("栈的当前元素个数是:%d", GetNum(s));break;case 6:flag = false;break; default:printf("非法输入!\n");break; } }
}