问题
将编号为 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二:
产生的原因是结构体写的有问题