用c++敲一个数据结构的顺序表编译器老是提示出错,但是我已经声明了求大神赐教啊c语言没好好学 10
#include<stdio.h>#include<stdlib.h>#defineok1#defineLIST_INIT_SIZE100typedefintElemTy...
#include <stdio.h>
#include<stdlib.h>
#define ok 1
#define LIST_ INIT_SIZE 100
typedef int ElemType;
typedef int Status;
typedef struct
{ElemType * elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList &L)
{
L.elem=(ElemType *)malloc(LIST_ INIT_SIZE*sizeof(ElemType));
if(!L.elem)exit(0);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return ok;
}
void main()
{
SqList L;
int x;
x=InitList_Sq(L);
printf("%d",x);
for(int i=0;i<3;i++)
scanf("%d",&L.elem[i]);
for(int t=0;t<3;t++)
printf("%d",L.elem[i]);
}
Status Listlnsert_Sq(SqList &L,int i,ElemType e)
{
if(i<1||i>L.length+1)return 0;
for(int j=L.length-1;j>=i-1;j--)
L.elem[j+1]=L.elem[j];
L.elem[i-1]=e;
++L.length;
return ok;
}
Status ListDelete_Sq(SqList &L,int i,ElemType &e)
{
int j;
if(i<1||i>L.length+1)return 0;
e=L.elem[j-1];
for(j=i;j<=L.length-1;j++)
L.elem[j-1]=L.elem[j];
--L.length;
return ok;}
int LocateELem(SqList L,ElemType e)
{
int h;
int j;
for(h=0;j<L.length;h++)
if(L.elem[h]==e)return h+1;
return 0;
}
这是代码
下面是错误
lianxi2.cpp
C:\Users\lenovo\Desktop\lianxi2.cpp(14) : error C2065: 'INIT_SIZE' : undeclared identifier
C:\Users\lenovo\Desktop\lianxi2.cpp(14) : error C2143: syntax error : missing ')' before 'constant'
C:\Users\lenovo\Desktop\lianxi2.cpp(14) : error C2059: syntax error : ')'
C:\Users\lenovo\Desktop\lianxi2.cpp(17) : error C2065: 'LIST_INIT_SIZE' : undeclared identifier
执行 cl.exe 时出错. 展开
#include<stdlib.h>
#define ok 1
#define LIST_ INIT_SIZE 100
typedef int ElemType;
typedef int Status;
typedef struct
{ElemType * elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList &L)
{
L.elem=(ElemType *)malloc(LIST_ INIT_SIZE*sizeof(ElemType));
if(!L.elem)exit(0);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return ok;
}
void main()
{
SqList L;
int x;
x=InitList_Sq(L);
printf("%d",x);
for(int i=0;i<3;i++)
scanf("%d",&L.elem[i]);
for(int t=0;t<3;t++)
printf("%d",L.elem[i]);
}
Status Listlnsert_Sq(SqList &L,int i,ElemType e)
{
if(i<1||i>L.length+1)return 0;
for(int j=L.length-1;j>=i-1;j--)
L.elem[j+1]=L.elem[j];
L.elem[i-1]=e;
++L.length;
return ok;
}
Status ListDelete_Sq(SqList &L,int i,ElemType &e)
{
int j;
if(i<1||i>L.length+1)return 0;
e=L.elem[j-1];
for(j=i;j<=L.length-1;j++)
L.elem[j-1]=L.elem[j];
--L.length;
return ok;}
int LocateELem(SqList L,ElemType e)
{
int h;
int j;
for(h=0;j<L.length;h++)
if(L.elem[h]==e)return h+1;
return 0;
}
这是代码
下面是错误
lianxi2.cpp
C:\Users\lenovo\Desktop\lianxi2.cpp(14) : error C2065: 'INIT_SIZE' : undeclared identifier
C:\Users\lenovo\Desktop\lianxi2.cpp(14) : error C2143: syntax error : missing ')' before 'constant'
C:\Users\lenovo\Desktop\lianxi2.cpp(14) : error C2059: syntax error : ')'
C:\Users\lenovo\Desktop\lianxi2.cpp(17) : error C2065: 'LIST_INIT_SIZE' : undeclared identifier
执行 cl.exe 时出错. 展开
1个回答
2015-11-29
展开全部
第四行里#define LIST_ INIT_SIZE 100的LIST_后多了个空格吧,应该是#define LIST_INIT_SIZE 100
追问
不是,还是报错
追答
你这代码里各种低级错误,
...
#define LIST_ INIT_SIZE 100 //多了空格
...
Status InitList_Sq(SqList &L)
{
L.elem = (ElemType *)malloc(LIST_ INIT_SIZE*sizeof(ElemType)); //多了空格
...
}
void main()
{
...
for (int t = 0; t < 3; t++)
printf("%d", L.elem[i]); //这里是i还是t
}
Status Listlnsert_Sq(SqList &L, int i, ElemType e)
{
...
}
Status ListDelete_Sq(SqList &L, int i, ElemType &e)
{
int j;
if (i<1 || i>L.length + 1)return 0;
e = L.elem[j - 1]; //j没有初始化,
...
}
int LocateELem(SqList L, ElemType e)
{
int h;
int j;
for (h = 0; j < L.length; h++) //j没有初始化
if (L.elem[h] == e)return h + 1;
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询