顺序栈及栈的一些基本操作 模板

这是一个用C++编写的栈数据结构实现,包括初始化、销毁、清空、获取栈长、判断栈空、入栈、出栈、查看栈内元素等基本操作,并提供了一个用于数字进制转换的函数。程序通过用户交互,允许选择执行不同的栈操作,方便进行数值的进制转换和栈的管理。
摘要由CSDN通过智能技术生成
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

typedef int SElemType;
typedef int Status;
#define STACK_INIT_SIZE 100 
#define INCREAMENT 10
#define OVERFLOW -1

typedef struct{
	SElemType *base;
	SElemType *top;
	int stacksize; 
}Sqstack;

Status InitStack(Sqstack &S){
	S.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
	if(!S.base) exit(OVERFLOW);
	S.top = S.base;
	S.stacksize = STACK_INIT_SIZE;
	return 1;
}

Status Pan(Sqstack &S){
	if(S.base == S.top) return 1;
	return 0;
}

Status Push(Sqstack &S, SElemType e){
	if(S.top - S.base >= S.stacksize){
		S.base = (SElemType *)realloc(S.base, STACK_INIT_SIZE * sizeof(SElemType));
		if(!S.base) exit(OVERFLOW);
		S.top = S.base + S.stacksize;
		S.stacksize += INCREAMENT;
	}
	*S.top = e;
	S.top ++ ;
	return 1;
}

int SLength(Sqstack S){
	return S.top - S.base;
}

Status GetTop(Sqstack S, SElemType &e){
	if(S.base == S.top) return 0;
	else{
		e = *(S.top - 1);
		return 1;
	}
}

Status Pop(Sqstack &S, SElemType &e){
	if(S.base == S.top) return 0;
	S.top -- ;
	e = *(S.top);
	return 1;
}

void Destroy(Sqstack &S){
	free(S.base);
	S.top = NULL;
	S.base = NULL;
	S.stacksize = 0;
}

void Clear(Sqstack &S){
	S.top = S.base;
}

Status View(Sqstack S){
	if(S.base == S.top) return 0;
	Sqstack q = S;
	while(!Pan(q)){
		q.top -- ;
		cout<<*(q.top)<<" ";
	}
	return 1;
}

void JinZhi(SElemType a, SElemType b){
	Sqstack S;
	InitStack(S);
	int i = 0;
	while(a){
		Push(S, a % b);
		a = a / b;
	}
	while(!Pan(S)){
		int e;
		cout<<Pop(S, e);
	}
	return ;
}

int main()
{
	
	Sqstack S;
	int flag = 0;
	while(1){
		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;  
		int n;
		cin>>n;
		if(n < 0){
			cout<<"感谢使用!"<<endl;
			break;
		}
		else if(n == 1){
			if(InitStack(S)){
				flag = 1;
				cout<<"初始化栈成功!"<<endl;
			}
			else{
				cout<<"初始化栈失败!"<<endl;
			}
		}
		else if(n == 2){
			if(flag == 0){
				cout<<"请先初始化栈"<<endl;
			}
			else if(flag == 2){
				cout<<"栈已销毁,请先初始化栈"<<endl;
			}
			else if(flag == 1){
				Destroy(S);
				flag = 2;
				cout<<"销毁成功"<<endl;
			}
		}
		else if(n == 3){
			if(flag == 0){
				cout<<"请先初始化栈"<<endl;
			}
			else if(flag == 2){
				cout<<"栈已销毁,请先初始化栈"<<endl;
			}
			else if(flag == 1){
				Clear(S);
				cout<<"清空成功!"<<endl;
			}
		}
		else if(n == 4){
			if(flag == 0){
				cout<<"请先初始化栈"<<endl;
			}
			else if(flag == 2){
				cout<<"栈已销毁,请先初始化栈"<<endl;
			}
			else if(flag == 1){
				cout<<"栈的长度为:"<<SLength(S)<<endl;
			}
		} 
		else if(n == 5){
			if(flag == 0){
				cout<<"请先初始化栈"<<endl;
			}
			else if(flag == 2){
				cout<<"栈已销毁,请先初始化栈"<<endl;
			}
			else if(flag == 1){
				if(Pan(S)){
					cout<<"栈为空"<<endl;
				}
				else{
					cout<<"栈不为空"<<endl;
				}
			} 
		}
		else if(n == 6){
			if(flag == 0){
				cout<<"请先初始化栈"<<endl;
			}
			else if(flag == 2){
				cout<<"栈已销毁,请先初始化栈"<<endl;
			}
			else if(flag == 1){
				int e;
				if(GetTop(S, e)){
					cout<<"栈顶元素为:"<<e<<endl; 
				}
				else{
					cout<<"栈为空,栈顶无元素"<<endl;
				}
			}
		}
		else if(n == 7){
			if(flag == 0){
				cout<<"请先初始化栈"<<endl;
			}
			else if(flag == 2){
				cout<<"栈已销毁,请先初始化栈"<<endl;
			}
			else if(flag == 1){
				while(1){
					cout<<"请输入你要入栈的值:";
					int oi;
					cin>>oi;
					if(Push(S, oi)){
						cout<<"入栈成功!"<<endl;
					}
					else{
						cout<<"入栈失败!"<<endl;
					}
					cout<<"请选择是否要继续输入:"<<endl;
					cout<<"1.继续输入"<<endl;
					cout<<"2.退出"<<endl;
					int a;
					cin>>a;
					if(a == 2) break; 
				}
			}	
		}
		else if(n == 8){
			if(flag == 0){
				cout<<"请先初始化栈"<<endl;
			}
			else if(flag == 2){
				cout<<"栈已销毁,请先初始化栈"<<endl;
			}
			else if(flag == 1){
				int e;
				if(Pop(S, e)){
					cout<<"弹出成功,出栈的元素为:"<<e<<endl;
				} 
				else{
					cout<<"栈为空,无栈顶元素"<<endl;
				}
			} 	
		}
		else if(n == 9){
			if(flag == 0){
				cout<<"请先初始化栈"<<endl;
			}
			else if(flag == 2){
				cout<<"栈已销毁,请先初始化栈"<<endl;
			}
			else if(flag == 1){
				if(Pan(S)){
					cout<<"栈为空"<<endl;
				}
				else{
					cout<<"栈内的元素:";
					View(S);
					cout<<endl; 
				}
			}
		} 
		else if(n == 10){
			if(flag == 0){
				cout<<"请先初始化栈"<<endl;
			}
			else if(flag == 2){
				cout<<"栈已销毁,请先初始化栈"<<endl;
			}
			else if(flag == 1){
				int a, b;
				cout<<"请输入你要转化的数字和数制:"<<endl;
				cin>>a>>b;
				JinZhi(a, b);
			}
		} 
		else{
			cout<<"请输入正确的指令!"<<endl;
		}
		cout<<endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值