空间内双栈的存储

#include <bits/stdc++.h>
typedef int ElemType;
const int M = 10;

typedef struct DoubleStack{
	int top[2];
	ElemType *V;
	int size;
}DStack;

int Init_DStack(DStack &S)	//创建空间V并定义双栈 
{
	S.V = (ElemType *)malloc(sizeof(ElemType) * M);
	if(!S.V) exit(OVERFLOW);
	S.size = M;
	S.top[0] = -1;
	S.top[1] = S.size;
	return 1;
}
void Destory_DS(DStack &S)
{
	free(S.V);
	S.V =  NULL;
}
int EmptyLS(DStack S)	//判断左栈是否为空 
{
	if(S.top[0] == -1)	return 1;
	return 0;
} 
int EmptyRS(DStack S)	//判断右栈是否为空 
{
	if(S.top[1] == S.size)	return 1;
	return 0;
}
int FullDStack(DStack S)
{
	if(S.top[0] == S.top[1] - 1 || S.top[0] >= S.size - 1 || S.top[1] <= 0)	return 1;	//栈满有三种情况:
																						//一是左右两栈栈顶相遇,二是左栈栈满 ,三是右栈栈满 
	return 0;
}
int Push_LeftS(DStack &S, ElemType e)
{
	if(FullDStack(S))	return 0;
	S.V[++S.top[0]] = e;
	return 1;
}
int Push_RightS(DStack &S, ElemType e)
{
	if(FullDStack(S))	return 0;
	S.V[--S.top[1]] = e;
	return 1;
}
int PopLeftS(DStack &S, ElemType *e)
{
	if(EmptyLS(S))	return 0;
	*e = S.V[S.top[0]--];
	return 1;
}
int PopRightS(DStack &S, ElemType *e)
{
	if(EmptyRS(S))	return 0;
	*e = S.V[S.top[1]++];
	return 1;
}
void PrintDS(DStack S)
{
	for(int i = 0; i <= S.top[0]; ++i)	printf("%d ", S.V[i]);
	for(int i = S.top[1]; i < S.size; ++i)	printf("%d ", S.V[i]);
}
using namespace std;

void Menu()
{
	printf("--------------------------------------------------\n");
	printf("              0:清除双栈并结束程序\n");
	printf("              1.初始化双栈\n");
	printf("              2.判断双栈是否为满\n");
	printf("              3.判断左栈是否为空\n");
	printf("              4.判断右栈是否为空\n");
	printf("              5.左栈入栈\n");
	printf("              6.右栈入栈\n");
	printf("              7.左栈出栈\n");
	printf("              8.右栈出栈\n");
	printf("              9.打印出双栈\n");
	printf("--------------------------------------------------");
}
DStack S;
int op, e;
int main()
{
	Menu();
	while(true)
	{
		printf("\n请输入选择操作:");
		scanf("%d", &op);
		if(op == 0)
		{
			Destory_DS(S);
			break;
		}
		if(op == 1)
		{
			if(Init_DStack(S))	printf("初始化双栈成功");
			else	printf("初始化失败!");
		}
		if(op == 2)
		{
			if(FullDStack(S))	printf("双栈为满");
			else	printf("双栈未满");
		}
		if(op == 3)
		{
			if(EmptyLS(S))	printf("左栈为空");
			else	printf("左栈不为空"); 
		}
		if(op == 4)
		{
			if(EmptyRS(S))	printf("左栈为空");
			else	printf("左栈不为空");
		}
		if(op == 5)
		{
			printf("请输入入栈元素:");
			scanf("%d", &e);
			if(Push_LeftS(S, e))	printf("入栈成功");
			else	printf("入栈失败!"); 
		}
		if(op == 6)
		{
			printf("请输入入栈元素:");
			scanf("%d", &e);
			if(Push_RightS(S, e))	printf("入栈成功");
			else	printf("入栈失败!");
		}
		if(op == 7)
		{
			if(PopLeftS(S, &e))	printf("出栈成功, 出栈元素为%d", e);
			else	printf("出栈失败,左栈为空!");
		}
		if(op == 8)
		{
			if(PopRightS(S, &e))	printf("出栈成功, 出栈元素为%d", e);
			else	printf("出栈失败,右栈为空!");
		}
		if(op == 9)
		{
			PrintDS(S);
		}
	}
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值