c语言 这到底是哪里出了问题??

#include "stdio.h"
#include "malloc.h"
#include"stdlib.h"
#define size 10
#define increment 5
typedef int ElemType;
typedef struct{
ElemType * elem;
int length;
int listsize;
}Sqlist;
int List(Sqlist * L)
{
L->elem = (ElemType *) malloc(size * sizeof(ElemType));
if (!L->elem)
return 0;
L->length = 0;
L->listsize = size;
return 1;
}
int insert (Sqlist * L,int i,ElemType e)
{
int j;
if (i < 1 || i > L->length + 1)
printf("插入的位置不合理");
return 0;
for(j=L->length-1;j>=i-1;j--)
L->elem[j+1]=L->elem[j];
L->elem[i-1]=e;
++L->length;
return 1;
}
int InputSqlist(Sqlist *L)
{
int i;
ElemType d;
printf("\n输入5个数字,数字间用空格空开,结束输入用#字符: ");
for (i = 0; scanf("%d", &d) == 1; ++i)
insert(L, i+1, d);
while ( (i = getchar()) != '\n' && i != EOF)
continue;
putchar('\n');
return i;
}
int scan()
{
int d;
printf("1.建表");
printf("2.插入");
scanf("%d",&d);
return (d);
}
void main()
{
int i;
ElemType e;
Sqlist L;
List(&L);
switch(scan())
{
case 1:InputSqlist(&L);break;
case 2: printf("插入位置和数字:");
scanf("%d%d",&i,&e);
insert(&L,i,e);break;
}

}

#define increment 5 //宏定义没有使用
主要问题:
int insert (Sqlist * L,int i,ElemType e) // 这个函数中
if (i < 1 || i > L->length + 1) // if函数没有花括号限定区域时,默认只对后一句语句有效
printf("插入的位置不合理");// 所以只有这一句在if里
return 0; // 这句却不在,所以每次执行到这里,就退出了。
解决方法:把 return 0; //也包含在if语句里
温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-10-17
先把if/for/。。。。等语句中的{}补充完整,代码会好很多

~