#define _FUNC_H
#include<iostream>
using namespace std;
#define TRUE 1
#define FASLE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW_s -2
typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType *base;//栈底
ElemType *top;//栈顶
int stacksize;
}SqStack;
extern void InitStack(SqStack &S1,SqStack &S2);//构造一个空栈
extern Status DestroyStack(SqStack &S);//销毁栈
extern Status ClearStack(SqStack &S);//清空栈
extern Status StackEmpty(SqStack S);
int StackLength(SqStack S);
extern Status GetTop(SqStack S,ElemType &e);//取栈顶元素
extern Status Push(SqStack &S,ElemType e);//栈顶插入元素
extern Status Pop(SqStack &S,ElemType &e);//删除栈顶元素
void StackTraverse(SqStack S);//遍历栈
#endif
#include<iostream>
#include"date.h"
using namespace std;
//构造一个空栈
int a[MAX];
void InitStack(SqStack &S1,SqStack &S2)
{
S1.base=&a[0];
S2.base=&a[MAX-1];
S1.top=S1.base;
S2.top=S2.base;
S1.stacksize=MAX;
S2.stacksize=MAX;
}
//销毁栈
Status DestroyStack(SqStack &S)
{
ElemType e;
while(S.top!=S.base)
{
Pop(S,e);
}
return OK;
}
//清空栈
Status ClearStack(SqStack &S)
{
if(S.top!=S.base)
{
S.top=S.base;
}
return OK;
}
//置空栈
Status StackEmpty(SqStack S)
{
if(S.top==S.base)
{
return TRUE;
}
else
{
return FASLE;
}
}
//求栈的长度
int StackLength(SqStack S)
{
int length;
if(S.top!=S.base)
{
length=S.top-S.base;
return length;
}
else
{
return ERROR;
}
}
//取栈顶元素
Status GetTop(SqStack S,ElemType &e)
{
if(S.top==S.base)
{
return ERROR;
}
else
{
e=*(S.top-1);
return OK;
}
}
//栈顶插入元素
Status Push(SqStack &S,ElemType e)
{
if(S.top-S.base>MAX/2)
{
return OVERFLOW_s;
}
else
{
*S.top++=e;
return OK;
}
}
//删除栈顶元素
Status Pop(SqStack &S,ElemType &e)
{
if(S.top!=S.base)
{
e=*--S.top;
return OK;
}
else
{
return ERROR;
}
}
//遍历栈
void StackTraverse(SqStack S)
{
int i,length=S.top-S.base;
for(i=1;i<=length;i++)
{
cout<<*S.base++<<" ";
}
cout<<endl;
}
#include"date.h"
using namespace std;
int length1,length2;
SqStack S1,S2;
int main()
{
int i, select,n;
ElemType e;
InitStack(S1,S2);
cout << "请输入栈S1的长度:" << endl;
cin >> length1;
for (i = 1; i <= length1; i++)
{
cin >> n;
Push(S1, n);
}
cout << "请输入栈S2的长度:" << endl;
cin >> length2;
for (i = 1; i <= length2; i++)
{
cin >> n;
Push(S2, n);
}
StackTraverse(S1);
StackTraverse(S2);
{
cout << "1.求栈S1的长度" << endl;
cout << "2.求栈S2的长度" << endl;
cout << "3.取S1栈顶元素" << endl;
cout << "4.取S2栈顶元素" << endl;
cout << "5.S1栈顶插入元素" << endl;
cout << "6.S2栈顶插入元素" << endl;
cout << "7.删除S1栈顶元素" << endl;
cout << "8.删除S2栈顶元素" << endl;
cout << "9.遍历栈S1" << endl;
cout << "10.遍历栈S2" << endl;
cout << "11.结束!" << endl;
cout << "请选择!" << endl;
cin >> select;
switch (select)
{
case 1:
if (StackLength(S1) == ERROR)
{
cout << "栈为空!" << endl;
}
else
{
cout << "length1=" << StackLength(S1) << endl;
}
break;
case 2:
if (StackLength(S2) == ERROR)
{
cout << "栈为空!" << endl;
}
else
{
cout << "length2=" << StackLength(S2) << endl;
}
break;
case 3:
if (GetTop(S1, e) == ERROR)
{
cout << "栈为空!" << endl;
}
else
{
cout << "栈顶元素为:" << e << endl;
}
break;
case 4:
if (GetTop(S2, e) == ERROR)
{
cout << "栈为空!" << endl;
}
else
{
cout << "栈顶元素为:" << e << endl;
}
break;
case 5:
cout << "请输入要S1插入的元素:\n";
cin >> e;
if (Push(S1, e) == OVERFLOW_s)
{
cout << "栈满!" << endl;
}
else
{
StackTraverse(S1);
}
break;
case 6:
cout << "请输入S2要插入的元素:\n";
cin >> e;
if (Push(S2, e) == OVERFLOW_s)
{
cout << "栈满!" << endl;
}
else
{
StackTraverse(S2);
}
break;
case 7:
if (Pop(S1, e) == ERROR)
{
cout << "栈为空!" << endl;
}
else
{
cout << "删除的栈顶元素为:" << e << endl;
}
break;
case 8:
if (Pop(S2, e) == ERROR)
{
cout << "栈为空!" << endl;
}
else
{
cout << "删除的栈顶元素为:" << e << endl;
}
break;
case 9:
StackTraverse(S1);
break;
case 10:
StackTraverse(S2);
break;
case 0:
cout << "操作结束!" << endl;
break;
default:
cout << "输入错误!" << endl;
}
} while (select != 0);
DestroyStack(S1);//销毁栈
DestroyStack(S2);//销毁栈
return 0;
}