实现数组双栈共享

来源:互联网 发布:java注解原理 编辑:程序博客网 时间:2024/06/08 19:49

将编号为0和1的两个栈放于一个数组空间V[m]中,栈底分别处于两数组的两端。
当第0号栈的栈顶指针top[0]等于-1时该栈为空,当第1号栈的栈顶指针top[1]等于m时.该栈为空。
两个栈均从两端向中间增长。试编写双栈初始化,判断栈空,栈满,进栈和出栈等算法的函数。

图示:


以下代码为在VS2015中测试通过:

#include<cstdio>#include<cmath>#include<windows.h>#define help "-----------------------------------------\r\n\接下来请根据提示输入操作:\r\n\            1.查询双栈中各栈是否为空\r\n\            2.查询双栈中各栈是否已满\r\n\            3.往双栈中加入元素\r\n\            4.从双栈中弹出数据\r\n\            5.退出系统\r\n"#define selectNum "请输入要操作的栈的编号:\r\n\                        0.双栈中的第一个栈\r\n\                        1.双栈中的第二个栈\r\n"typedef struct{int Top[2],bot[2];int*V;int m;}DblStack;DblStack MyDbStackTest;char Flesh[20]={0};void InitDblStack(DblStack& doublestack);bool IsDblStackEmpty(DblStack doublestack,int index);bool ISDblStackFull(DblStack doublestack,int index);void DblStackPush(DblStack &doublestack,int element,int index);void DblStackPop(DblStack &doublestack,int index);void ShowResultOfEmptyInspectation();void ShowResultOfFullInspectation();void PushAndInspect( int index);int main(){    system("color 0A");char inputData;InitDblStack(MyDbStackTest);ShowResultOfEmptyInspectation();ShowResultOfFullInspectation();printf(help);while(true){inputData =getchar();if(inputData=='5')break;else if(inputData=='1'){system("cls");printf("查询结果如下:\r\n");ShowResultOfEmptyInspectation();printf(help);}else if(inputData=='2'){system("cls");printf("查询结果如下:\r\n");ShowResultOfFullInspectation();printf(help);}else if(inputData=='3'){gets(Flesh);printf(selectNum);char inputSelect;inputSelect =getchar();if(inputSelect=='0'){if(ISDblStackFull(MyDbStackTest,0)){printf("双栈中编号为的栈已满|即将跳回主页\r\n");Sleep(2000);system("cls");printf(help);continue;}else{PushAndInspect(0);continue;}}else if(inputSelect=='1'){if(ISDblStackFull(MyDbStackTest,1)){printf("双栈中编号为的栈已满|即将跳回主页!\r\n");Sleep(2000);system("cls");printf(help);continue; }else{PushAndInspect(1);continue;}}else if(inputSelect!='\n'){system("cls");printf(help);continue;}}else if(inputData=='4'){gets(Flesh);char inputSelect;printf(selectNum);inputSelect =getchar();if(inputSelect=='0'){if(IsDblStackEmpty(MyDbStackTest,0)){printf("双栈中编号为的栈已空|即将跳回主页!\r\n");Sleep(2000);system("cls");printf(help);continue;}else{DblStackPop(MyDbStackTest,0);continue;}}else if(inputSelect=='1'){if(IsDblStackEmpty(MyDbStackTest,1)){printf("双栈中编号为的栈已空|即将跳回主页!\r\n");Sleep(2000);system("cls");printf(help);continue; }else{DblStackPop(MyDbStackTest,1);continue;}}else if(inputSelect!='\n')  {system("cls");printf(help);continue; }}}return 0;}void PushAndInspect( int index){int InsertElem=0;gets(Flesh);printf("请输入要插入%d号栈的元素(请输入数字,否则将自动转为Ascii码):\r\n",index);scanf("%d",&InsertElem);if(InsertElem>65535||InsertElem<-65536){printf("输入的数据超过了最大整数|即将跳回主页\r\n");Sleep(1000);system("cls");printf(help);}else{DblStackPush(MyDbStackTest,InsertElem,index);system("cls");printf(help);}}void DblStackPush(DblStack &doublestack,int element,int index){if(index==0){doublestack.Top[index]++;doublestack.V[doublestack.Top[index]]=element;printf("数据入栈号栈成功\r\n即将返回主页\r\n");Sleep(1000);}else if(index==1){doublestack.Top[index]--;doublestack.V[doublestack.Top[index]]=element;printf("数据入栈号栈成功\r\n即将返回主页\r\n");Sleep(1000);}}void DblStackPop(DblStack &doublestack,int index){if(index==0){printf("数据入栈号出栈成功\r\n出栈数据为:%d\r\n即将返回主页\r\n",doublestack.V[doublestack.Top[index]]);doublestack.Top[index]--;Sleep(2000);}else if(index==1){printf("数据入栈号出栈成功\r\n出栈数据为:%d\r\n即将返回主页\r\n",doublestack.V[doublestack.Top[index]]);doublestack.Top[index]++;Sleep(2000);}system("cls");printf(help);}void ShowResultOfEmptyInspectation(){if(IsDblStackEmpty(MyDbStackTest,0))printf("The 0 stack of dblstack is empty!\r\n");elseprintf("The 0 stack of dblstack isn't empty!\r\n");if(IsDblStackEmpty(MyDbStackTest,1))printf("The 1 stack of dblstack is empty!\r\n");elseprintf("The 1 stack of dblstack isn't empty!\r\n");}void ShowResultOfFullInspectation(){if(ISDblStackFull(MyDbStackTest,0))printf("The 0 stack of dblstack is full!\r\n");elseprintf("The 0 stack of dblstack isn't full!\r\n");if(ISDblStackFull(MyDbStackTest,1))printf("The 1 stack of dblstack is full!\r\n");elseprintf("The 1 stack of dblstack isn't full!\r\n");}bool IsDblStackEmpty(DblStack doublestack,int index){if(index==0){if(doublestack.Top[0]==-1)return true;elsereturn false;}else if(index ==1){if(doublestack.Top[1]==doublestack.m)return true;elsereturn false;}}bool ISDblStackFull(DblStack doublestack,int index){if(index==0){if(doublestack.Top[0]>= floor(doublestack.m/2.0-1))return true;elsereturn false;}else if(index==1){if(doublestack.Top[1]<=floor(doublestack.m/2.0))return true;elsereturn false;}}void InitDblStack(DblStack& doublestack){doublestack.m=4;doublestack.V=new int [doublestack.m];doublestack.bot[0]=-1;doublestack.Top[0]= doublestack.bot[0];doublestack.bot[1]=doublestack.m;doublestack.Top[1]=doublestack.bot[1];}


0 0
原创粉丝点击