#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); } } }



