判满条件:top2-top1=1;
注意:case里面包含变量初始化的时候,需要用{}括起来
#include<iostream>
using namespace std;
#define maxsize 100
#define elemtp int
typedef struct {
elemtp stack[maxsize];
int top[2];
}stk;
stk s;
int push(int i, elemtp x) {
if (i < 0 || i>1) {
cout << "栈号输入不对" << endl;
exit(0);
}
if (s.top[1] - s.top[2] == 1) {
cout << "栈已满" << endl;
return 0;
}
switch (i)
{
case 0:s.stack[++s.top[0]] = x; return 1; break;
case 1:s.stack[--s.top[1]] = x; return 1;
}
}
elemtp pop(int i) {
if (i < 0 || i>1) {
cout << "栈号输入不对" << endl;
exit(0);
}
switch (i) {
case 0:
if (s.top[0] == 1) {
cout << "栈空" << endl;
return -1;
}
}
}
int main() {
while (1) {
cout << "请选择操作:1.入栈 2.出栈" << endl;
int opt=0;
cin >> opt;
switch (opt)
{
case 1:{
cout << "请输左栈,还是右栈" << endl;
int top = 0;
cin >> top;
cout << "入栈数字" << endl;
int num = 0;
cin >> num;
push(top, num);
break;
}
case 2:{
cout << "请输左栈,还是右栈" << endl;
int top = 0;
cin >> top;
pop(top);
break;
}
}
}
}