栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

2021-10 - 09 栈

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021-10 - 09 栈

#include
#define rep(i, a, b) for(int i = a; i <= b; ++ i)
#define per(i, a, b) for(int i = a; i >= b; -- i)

using namespace std;
const int maxn = 5;

typedef struct Stack{
	int data[maxn];
	int top;
}stack;

void initStack(stack * s)
{
	s -> top = -1;
	cout << "初始化成功n";
}

void pushStack(stack * s)
{
	cout << "请输入你要输入的元素:";
	int tmp;
	cin >> tmp;
	if(s -> top == maxn - 1){
		cout << "栈已经满了!!!n";
	} else {
		s -> top ++;
		s -> data[s -> top] = tmp;
		cout << "成功入栈n"; 
	}
}

void backStack(stack * s)
{
	if(s -> top == -1){
		cout << "栈空n"; 
	} else {
		int tmp = s -> data[s -> top];
		s -> top --;
		cout << "出栈元素为:" << tmp << endl;
	}
}

void bianLiStack(stack * s)
{
	int i = 0;
	while(i <= s -> top){
		cout << s -> data[i] << " ";
		i ++;
	}
	cout << endl;
}


int main()
{
	stack s;
	bool f = true;
	while(f){
		cout << "操作菜单如下:n";
		cout << "1. 申请栈的空间n";
		cout << "2. 压栈n";
		cout << "3. 出栈n";
		cout << "4. 遍历栈n";
		cout << "请输入你要进行的操作:";
		int op;
		cin >> op;
		if(op == 1){
			initStack(&s);
		} else if(op == 2) {
			pushStack(&s);
		} else if(op == 3){
			backStack(&s);
		} else if(op == 4){
			bianLiStack(&s);
		}
	}
	
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/312289.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号