实验题目:
1.初始化栈,2.销毁栈,3.清空栈,4.栈判空,5.求栈长度,6.获取栈顶元素,7.插入一个 元素,8.删除一个元素,9输出所有元素,10进制转换。
要求:自定义的函数中不允许出现提示语和输出语句。
实验注意事项:
1、要保证程序是可控制的,如当未进行初始化的时候无法进行其他的操作,在实现这个功能的时候,我定义了一个isInit作为初始化的标记,初始值为false,当执行完初始化操作的时候,将改标志变为true。
2、函数里面要考虑用户输入的非法性,当用户输入非法数据的时候要保证程序可以控制。
3、入栈之前先判满,出栈之前先判空。
顺序栈:顺序栈是指利用顺序存储结构实现的栈。采用地址连续的存储空间(数组)依次存储栈中数据元素,入栈和出栈运算都是在栈顶进行,而栈底位置是固定不变的。
特点:
·先进后出
·栈满会出现“上溢”,栈空会出现“下溢”
#include <bits/stdc++.h>
using namespace std;
//定义声明区
#define OK 1
#define ERROR 0
typedef int Status;
typedef int SElemType;
bool isInit = false;
//栈的表示
# define STACK_INIT_SIZE 100 //存储空间的初始分配量
# define STACKINCREMENT 10 //存储空间的分配增量
typedef struct{
SElemType * base;
SElemType * top; //栈顶指针,总是指向栈顶元素的下一个位置
int stacksize; //已分配的存储空间
} SqStack;
//函数声明区
void menu(); //功能菜单
Status InitStack (SqStack &S); //初始化栈
Status DestoryStack (SqStack &S); //销毁栈
Status ClearStack(SqStack &S); //清空栈
Status StackEmpty(SqStack S); //栈判空
Status GetLength(SqStack S); //求栈的长度
Status GetTop(SqStack S, SElemType &e); //获取栈顶元素
Status Push(SqStack &S, SElemType e); //插入一个元素
Status Pop(SqStack &S, SElemType &e); //删除一个元素
Status OutPut(SqStack S, SElemType *arr, int arr_length); //输出所有的元素
Status Convert(int num, int x, string &result); //进制转换
//主函数
int main(){
SqStack S;
int option = 0;
SElemType e;
int count = 0;
int arr_length;
string result = "";
int num = 0;
int x = 0;
SElemType arr[1000];
menu();
while (option >= 0){
cout << "请输入你的选择:" << endl;
cin >> option;
if (isInit == false && option != 1 && option != 10 && option >= 0){
cout << "栈未进行初始化,请先进行初始化操作" << endl;
continue;
}
switch (option){
case 1:
if (InitStack(S)){
cout << "初始化成功" << endl;
} else {
cout << "初始化失败,内存空间不足" << endl;
}
break;
case 2:
DestoryStack(S);
cout << "销毁成功" << endl;
case 3:
ClearStack(S);
cout << "清空成功" << endl;
break;
case 4:
if (StackEmpty(S)){
cout << "该栈为空栈" << endl;
} else {
cout << "该栈不是空栈" << endl;
}
break;
case 5:
cout << "栈的长度为:" << GetLength(S) << endl;
break;
case 6:
if (GetTop(S, e)){
cout << "栈顶元素为:" << e << endl;
} else {
cout << "该栈为空,无法获取栈顶元素" << endl;
}
break;
case 7:
cout << "请输入你要插入的元素的个数:" << endl;
cin >> count;
cout << "请输入你要插入的元素:" << endl;
for (int i = 0; i < count; ++i){
cin >> e;
Push(S,e);
}
cout << "插入成功" << endl;
break;
case 8:
if (Pop(S, e)){
cout << "成功删除栈顶元素:" << e << endl;
} else {
cout << "该栈为空,删除失败" << endl;
}
break;
case 9:
arr_length = GetLength(S);
OutPut(S, arr, arr_length);
cout << "栈的元素有:" << endl;
for (int i = 0; i < arr_length; ++i){
cout << arr[i] << " " ;
}
cout << endl;
break;
case 10:
cout << "请输入要进行转化的数字:" << endl;
cin >> num;
cout << "请输入你要转换的对应进制:" << endl;
cin >> x;
if (Convert(num, x, result)){
cout << "进制转换的结果是:" << result << endl;
} else {
cout << "转化失败,请检查你的进制是否合法" << endl;
}
break;
default:
cout << "欢迎您下次使用" << endl;
break;
}
}
}
void menu(){
cout << "~~~~~~Spraing※boy~~~~~~" << endl;
cout << "** 1.初始化栈 **" << endl;
cout << "** 2.销毁栈 **" << endl;
cout << "** 3.清空栈 **" << endl;
cout << "** 4.栈判空 **" << endl;
cout << "** 5.求栈长度 **" << endl;
cout << "** 6.获取栈顶元素 **" << endl;
cout << "** 7.插入一个元素 **" << endl;
cout << "** 8.删除一个元素 **" << endl;
cout << "** 9.输出所有元素 **" << endl;
cout << "** 10.进制转化 **" << endl;
cout << "** -> 退出输入负数 **" << endl;
cout << "************************" << endl;
}
//初始化栈
Status InitStack (SqStack &S){
//构造一个空栈S
S.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if (!S.base) exit (OVERFLOW); //存储分配失败
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
isInit = true; //已进行初始化
return OK;
}
//销毁栈
Status DestoryStack (SqStack &S){
S.top = NULL;
S.stacksize = 0;
free(S.base);
isInit = false;
return OK;
}
//清空栈
Status ClearStack(SqStack &S){
if (S.base){
S.base = S.top;
}
return OK;
}
//栈判空
Status StackEmpty(SqStack S){
if (S.base == S.top){
return OK;
} else {
return ERROR;
}
}
//求栈的长度
Status GetLength(SqStack S){
return (S.top - S.base);
}
//获取栈顶元素
Status GetTop(SqStack S, SElemType &e){
if (S.base == S.top){
return ERROR;
}
e = *(S.top-1);
return OK;
}
//插入栈顶元素
Status Push(SqStack &S, SElemType e){
if (S.top - S.base >= S.stacksize){ //表示栈满
S.base = (SElemType *) realloc (S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType));
if (!S.base) exit (OVERFLOW); //存储分配失败
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top = e;
S.top++;
return OK;
}
//删除一个元素
Status Pop(SqStack &S, SElemType &e){
if (S.top == S.base){
return ERROR;
}
S.top--;
e = *S.top;
return OK;
}
//输出所有元素
Status OutPut(SqStack S, SElemType *arr, int arr_length){
int i = 0;
if (S.base == S.top){
return ERROR;
}
while (S.top > S.base){
S.top--;
arr[i] = *S.top;
i++;
}
return OK;
}
//进制转化
Status Convert(int num, int x, string &result){
result = "";
int rem;
string s = "0123456789ABCDEF";
SqStack SS;
InitStack(SS);
if (x < 2 || x > 16){
return ERROR;
}
while (num != 0){
rem = num % x;
Push(SS, rem);
num = (num - rem) / x;
}
int length = GetLength(SS);
for (int i = 0; i < length; ++i){
Pop(SS, rem);
result += s[rem];
}
return OK;
}