将编号为 0 和 1 的两个栈存放于一个数组空间 V[m]中,栈底分别处于数组的两端。当第0 号栈的栈顶指针 top[0]等于-1 时该栈为空;当第 1 号栈的栈顶指针 top[1]等于 m 时,该栈

问题

将编号为 0 和 1 的两个栈存放于一个数组空间 V[m]中,栈底分别处于数组的两端。当第0 号栈的栈顶指针 top[0]等于-1 时该栈为空;当第 1 号栈的栈顶指针 top[1]等于 m 时,该栈为空。两个栈均从两端向中间增长(见下图)。试编写双栈初始化,判断栈空、栈满、进栈
和出栈等算法的函数。双栈数据结构的定义如下;
在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
typedef int SElemType;
typedef int Status;
typedef struct 
{
	int top[2],bot[2];	//栈顶和栈底指针
	SElemType *V;	//栈数组
	int m;	//栈最大可容纳元素个数
}DblStack;
Status InitStack1(DblStack S)
{
	S.V = (SElemType*)malloc(S.m*sizeof(SElemType)); 
	S.bot[0] = S.top[0]=-1;
	S.bot[1] = S.top[1]=S.m-1;	
	return 1;
}
int main()
{

	DblStack S;
	S.m=8;
	InitStack1(S);
	//Push0(S,1,8);
	//for(i=0;i<7;i++)
	//	printf("%d\n",S.V[i]);

}

这样写也是对的

#include<stdio.h>
#include<stdlib.h>
typedef int SElemType;
typedef int Status;
typedef struct 
{
	int top[2],bot[2];	//栈顶和栈底指针
	SElemType *V;	//栈数组
	int m;	//栈最大可容纳元素个数
}DblStack;

Status InitStack1(DblStack S)
{
	S.m = 8;
	S.V = (SElemType*)malloc(8*sizeof(SElemType)); 
	S.bot[0] = S.top[0]=-1;
	S.bot[1] = S.top[1]=S.m-1;	
	return 1;
}

Status IsEmpty(DblStack S)
{
	if(S.bot[0]==S.top[0]||S.bot[1]==S.top[1])
		return 0;
	else
		return 1;
}

//Status Push1(DblStack S,int e)
//{
//	if( (S.top[0]-S.bot[0]) + (S.bot[1]-S.top[1]) == S.m )
//		return -1;
//	S.V[S.top[1]] = e;
//	S.top[1]--;
//}

//Status IsFull(DblStack &S,int m)
//{
//	if((S.top[0]-S.bot[0])+(S.bot[1]-S.top[1])==m)
//		return 1;
//	else 
//		return 0;
//}
//


//Status Push0(DblStack &S,int e,int m)
//{
//	if((S.top[0]-S.bot[0]) + (S.top[1]-S.bot[1]) == m)
//		return -1;
//	S.top[0]++;
//	S.V[S.top[0]];
//}
//
//
//void show(DblStack S,int m)
//{
//	int i;
//	for(i=0;i<m;i++)
//		printf("%d\n",S.V[m]);
//}


int main()
{
	DblStack S;
	InitStack1(S);
	//Push1(S,1);
	system("pause");
	//Push0(S,1,8);
	//for(i=0;i<7;i++)
	//	printf("%d\n",S.V[i]);

}

error一:
The variable ‘s’ is being used without being initialized
产生的原因是s的m没有赋值
error二:
在这里插入图片描述
产生的原因是结构体写的有问题

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值