实验4 顺序栈
作者:互联网
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<conio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
#define NO -1
#define endl '\n'
#define MAXSIZE 100
#define SIZEMENT 10
using namespace std;
typedef int SElemType;
typedef int Status;
int here=0;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S);
Status DestoryStack(SqStack &S);
Status ClearStack(SqStack &S);
Status StackEmpty(SqStack S);
int StackLength(SqStack S);
Status GetTop(SqStack S,SElemType &e);
Status Push(SqStack &S,SElemType &e);
Status Pop(SqStack &S,SElemType &e);
Status Display(SqStack S);
int main(){
SqStack S;//该栈用于基础操作
SqStack S1;//该栈用于进制转换
SElemType e;
int flag=1;
int s;
cout<<"********************************"<<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;
while(flag){
cin>>s;
switch(s){
case 1:{
InitStack(S);
cout<<"成功初始化栈!"<<endl;
here=1;
break;
}
case 2:{
if(here==1){
DestoryStack(S);
cout<<"销毁栈成功!"<<endl;
here=0;
}
else cout<<"请先初始化栈!"<<endl;
break;
}
case 3: {
if(here==1){
ClearStack(S);
cout<<"清空栈成功!"<<endl;
}
else cout<<"请先初始化栈!"<<endl;
break;
}
case 4:{
if(here==1){
if(StackEmpty(S)==1){
cout<<"栈为空"<<endl;
}else{
cout<<"栈不为空"<<endl;
}
}
else cout<<"请先初始化栈!"<<endl;
break;
}
case 5:{
if(here==1){
cout<<"当前栈的长度为:"<<StackLength(S)<<endl;
}
else cout<<"请先初始化栈!"<<endl;
break;
}
case 6:{
if(here==1){
SElemType thetop;
if(GetTop(S,thetop)==0)cout<<"栈顶无元素"<<endl;
else cout<<"栈顶元素为"<<thetop<<endl;
}
else cout<<"请先初始化栈!"<<endl;
break;
}
case 7:{
if(here==1){
cout<<"请输入要进栈的元素"<<endl;
SElemType thepush;
cin>>thepush;
if(Push(S,thepush)==1){
cout<<"进栈成功" <<endl;
}
else cout<<"请先初始化栈!"<<endl;
break;
}
case 8:{
if(here==1){
SElemType thepop;
if(Pop(S,thepop)==0){
cout<<"栈空"<<endl;
}else{
cout<<"出栈成功,出栈的元素为"<<thepop<<endl;
}
}
else cout<<"请先初始化栈!"<<endl;
break;
}
case 9:{
if(here==1){
cout<<"栈中元素为:";
if(Display(S)==1){
cout<<endl;
}
else cout<<"栈为空"<<endl;
}
else cout<<"请先初始化栈!"<<endl;
break;
}
case 10:{
if(here==1||here==0){
InitStack(S1);
int n,b;
cout<<"请输入要转换的十进制数:";
cin>>n;
cout<<"请输入转换的进制:";
cin>>b;
while(n){
int i=n%b;
Push(S1,i);
n=n/b;
}
cout<<"转换结果为:";
int pop;
while(!StackEmpty(S1)){
Pop(S1,pop);
cout<<pop;
}cout<<endl;
}
break;
}
default:exit(1);
}
}
}
}
Status InitStack(SqStack &S){
S.base=(int *)malloc(MAXSIZE*sizeof(SElemType));
if(!S.base)exit(-1);
S.top=S.base;
S.stacksize=MAXSIZE;
return OK;
}
Status DestoryStack(SqStack &S){
if(S.base){
free(S.base);
S.stacksize=0;
S.base=S.top=NULL;
return OK;
}
}
Status ClearStack(SqStack &S){
if(S.base){
S.top=S.base;
return OK;
}
}
Status StackEmpty(SqStack S){
if(S.top==S.base)return TRUE;
return FALSE;
}
int StackLength(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,SIZEMENT*sizeof(SElemType));
}
*S.top++=e;
return OK;
}
Status Pop(SqStack &S,SElemType &e){
if(S.top==S.base)return ERROR;
e=*--S.top;
return OK;
}
Status Display(SqStack S){
if(S.base==S.top)return ERROR;
for(int i=1;S.top-i!=S.base;i++){
cout<<*(S.top-i)<<" ";
}
cout<<*(S.base);
return OK;
}
标签:Status,顺序,cout,SqStack,int,SElemType,实验,define 来源: https://blog.csdn.net/qq_53117087/article/details/120919339