速求顺序表插入函数怎么错了???
#include"stdio.h"#include"malloc.h"
typedef struct {
char a[20];
int num;
char c[20];
} ElemType;
typedef struct {
ElemType city[3];
int length; //当前线性表长度
} SqList;
void InitList(SqList *&L)
{
L=(SqList*)malloc(sizeof(SqList));
L->length=0;
}
void CreateList(SqList *&L,ElemType city[],int n)
{
int i;
L=(SqList*)malloc(sizeof(SqList));
for(i=0;i<n;i++)
L->city[i]=city[i];
L->length=n;
}
void DestroyList(SqList *L)
{
free(L);
}
int ListEmpty(SqList *L)
{
return (L->length==0);
}
int ListLength(SqList *L)
{
return (L->length);
}
void DispList(SqList *L)
{
int i;
if(ListEmpty(L)) return;
for(i=0;i<L->length;i++)
{
printf("%s 0%o %s\n",L->city[i].a,L->city[i].num,L->city[i].c);
}
}
int LocateElem(SqList *L,ElemType e)
{
int i=0;
while(i<L->length&&L->city[i].num!=e.num)
i++;
if(i>=L->length)
return 0;
else
return i+1;
}
int ListInsert(SqList *L,int i,ElemType e)
{
int j;
if(i<1||i>L->length+1)
return 0;
i--;
for(j=L->length;j>i;j--)
L->city[j]=L->city[j-1];
L->city[i]=e;
L->length++;
return 1;
}
int ListDelete(SqList *&L,int i,ElemType &e)
{
int j;
if(i<1||i>L->length) return 0;
i--;
e=L->city[i];
for(j=i;j<L->length-1;j++)
L->city[j]=L->city[j+1];
L->length--;
return 1;
}
void main()
{
SqList *L;
ElemType city[3]={{"beijing",010,"首都"},{"shanghai",021,"直辖市"},{"tianjin",032,"直辖市"}};
ElemType e1={"shanghai",021,"直辖市"};
// ElemType city[1]={"beijing",010,"首都"};
// ElemType city[2]={"shanghai",021,"直辖市"};
printf("1建立顺序表L\n");
CreateList(L,city,3) ;
printf("2顺序表L为%s\n",(ListEmpty(L)?"空":"非空"));
printf("3顺序表L的长度=%d\n",ListLength(L));
printf("4输出线性表L:\n");
DispList(L);
printf("5元素e1的位置是%d\n",LocateElem(L,e1));
ElemType e2={"chongqing",041,"直辖市"};
printf("6在第2个位置上插入元素e2\n");
ListInsert(L,2,e2);
printf("输出插入后的顺序表\n");
DispList(L);
ElemType e;
printf("7删除L的第2个元素\n");
ListDelete(L,2,e);
printf("输出删除后的顺序表\n");
DispList(L);
}