2021-10 - 09 栈

#include<iostream>
#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);
		}
	}
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值