实现数组双栈共享

这篇博客介绍了如何利用一个数组空间实现两个栈的共享,栈底分别位于数组的两端,栈从两端向中间增长。内容包括双栈的初始化、判断栈空、栈满以及进栈和出栈操作的算法实现,并提供了在VS2015环境下测试通过的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将编号为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");
	else
		printf("The 0 stack of dblstack isn't empty!\r\n");

	if(IsDblStackEmpty(MyDbStackTest,1))
		printf("The 1 stack of dblstack is empty!\r\n");
	else
		printf("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");
	else
		printf("The 0 stack of dblstack isn't full!\r\n");

	if(ISDblStackFull(MyDbStackTest,1))
		printf("The 1 stack of dblstack is full!\r\n");
	else
		printf("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;
		else
			return false;
	}
	else if(index ==1)
	{
		if(doublestack.Top[1]==doublestack.m)
			return true;
		else
			return false;
	}
}
bool ISDblStackFull(DblStack doublestack,int index)
{
	if(index==0)
	{
		if(doublestack.Top[0]>= floor(doublestack.m/2.0-1))
			return true;
		else
			return false;
	}
	else if(index==1)
	{
		if(doublestack.Top[1]<=floor(doublestack.m/2.0))
			return true;
		else
			return 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];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值