#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);
}
}
}
2021-10 - 09 栈
最新推荐文章于 2024-08-31 22:41:09 发布