数据结构》严奶奶版本—栈(1) 顺序栈 完整源码
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/be21b4e0fb5b3b6205946adc0337a8c0.png#pic_center)
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
#define STACK_INIT_SIZE 100
#define STACK_ADD_SIZE 10
typedef int SElemType;
typedef struct sqstack{
SElemType* top;
SElemType* base;
int max_size;
}Sqstack;
bool init_stack(Sqstack &sq);
bool judge_full(Sqstack sq);
bool groth_stack(Sqstack &sq);
bool judge_empty(Sqstack sq);
bool push(Sqstack &sq,SElemType data);
bool pop(Sqstack &sq,SElemType &data);
bool get_top(Sqstack sq,SElemType &data);
bool print_stack(Sqstack sq);
void conversion();
string judge_match();
void hanoi(int n,char x,char y,char z);
Sqstack sq;
int count = 0;
int main()
{
cout<<"顺序栈操作\n";
init_stack(sq);
int i,count;
SElemType data;
cout<<"请输入入栈元素个数:";
cin>>count;
for(i = 0;i < count; i++)
{
cout<<"请输入第"<<1+i<<"个元素:";
cin>>data;
push(sq,data);
}
print_stack(sq);
cout<<"出栈的栈顶元素:";
pop(sq,data);
cout<<data<<endl;
print_stack(sq);
cout<<"输出栈顶元素:";
get_top(sq,data);
cout<<data<<endl;
conversion();
cout<<judge_match()<<endl;
cout<<"输入汉诺塔的层数:";
cin>>count;
hanoi(count,'A','B','C');
return 0;
}
bool init_stack(Sqstack &sq)
{
sq.base = new SElemType[STACK_INIT_SIZE];
if(!sq.base)
{
cout<<"初始化栈失败"<<endl;
return false;
}
sq.top = sq.base;
sq.max_size = STACK_INIT_SIZE;
}
bool judge_full(Sqstack sq)
{
if(sq.top - sq.base >= sq.max_size)
return true;
return false;
}
bool groth_stack(Sqstack &sq)
{
sq.base = (SElemType *)realloc(sq.base,(sq.max_size + STACK_ADD_SIZE) * sizeof(SElemType));
if(!sq.base)
{
cout<<"扩容失败"<<endl;
return false;
}
sq.top = sq.base + sq.max_size;
sq.max_size += STACK_ADD_SIZE;
return true;
}
bool judge_empty(Sqstack sq)
{
if(sq.base == sq.top)
return true;
return false;
}
bool print_stack(Sqstack sq)
{
if(judge_empty(sq))
{
cout<<"当前栈为空"<<endl;
return false;
}
cout<<"栈中的元素为:";
int i = sq.top -sq.base,j;
for(j = 1; j <= i ; j++)
cout<<*(sq.top - j)<<" ";
cout<<endl;
}
bool push(Sqstack &sq,SElemType data)
{
if(judge_full(sq))
{
cout<<"栈满,自动扩容"<<endl;
if(!groth_stack(sq))
return false;
}
*(sq.top) = data;
sq.top++;
return true;
}
bool pop(Sqstack &sq,SElemType &data)
{
if(judge_empty(sq))
{
cout<<"当前栈为空"<<endl;
return false;
}
data = *(--sq.top);
return true;
}
bool get_top(Sqstack sq,SElemType &data)
{
if(judge_empty(sq))
{
cout<<"当前栈为空"<<endl;
return true;
}
data = *(sq.top-1);
return true;
}
void conversion()
{
Sqstack sq;
init_stack(sq);
int num;
cout<<"请输入要转换的十进制数:";
cin>>num;
while(num)
{
push(sq,num%8);
num = num / 8;
}
SElemType data;
cout<<"\n转换后的八进制数为:";
while(!judge_empty(sq))
{
pop(sq,data);
cout<<data;
}
cout<<endl;
}
string judge_match()
{
string e;
Sqstack sq;
init_stack(sq);
cout<<"请输入进行括号匹配的表达式:";
cin>>e;
int i = 0;
int temp;
while(i < e.length())
{
switch(e[i])
{
case '(' : push(sq,e[i]); i++; break;
case '[' : push(sq,e[i]); i++; break;
case ')' :
{
if( get_top(sq,temp) )
{
if(temp == '(')
{
pop(sq,temp);
i++;
}
else
return "括号不匹配";
}
else
return "括号不匹配";
}break;
case ']' :
{
if( get_top(sq,temp) )
{
if(temp == '[')
{
pop(sq,temp);
i++;
}
else
return "括号不匹配";
}
else
return "括号不匹配";
}break;
default: i++;break;
}
}
return "括号匹配";
}
void hanoi(int n,char x,char y,char z)
{
if(n == 1)
{
printf("第%d次移动,将%d从%c移至%c\n",++count,n,x,z);
}
else
{
hanoi(n-1,x,z,y);
printf("第%d次移动,将%d从%c移至%c\n",++count,n,x,z);
hanoi(n-1,y,x,z);
}
}