第3章 栈和队列
3.1 栈
综合应用题 第5题
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define MaxSize 10
using namespace std;
typedef int ElemType;
typedef struct{
ElemType stack[MaxSize];
int top[2];
}SqStack;
SqStack s;
int Push(int i,ElemType x)
{
if(i<0||i>1)
{
cout<<"栈号输入不对"<<endl;
exit(0);
}
if(s.top[1]-s.top[0]==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;
}
}
int 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;
}else
return s.stack[s.top[0]--];
case 1:
if(s.top[1]==MaxSize){
cout<<"栈空"<<endl;
return -1;
}else
return s.stack[s.top[1]++];
}
}
int main() {
Push(1,3);
Push(0,4);
Push(1,7);
Push(0,8);
cout<<Pop(1)<<endl;
cout<<Pop(0)<<endl;
return 0;
}
```![在这里插入图片描述](https: