顺序表为什么初始化不了啊,求大神帮忙,新手上路坎坷重重啊~
#include "stdio.h"#include "stdlib.h"
#include "malloc.h"
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OVERFLOW -2
#define OK 1
typedef struct
{
int *elem;
int length;
int listsize;
}Sqlist;
int InitList_Sq(Sqlist L)
{
L.elem=(int *)malloc(LIST_INIT_SIZE*sizeof(int));
if(! L.elem) exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}
int readlist(Sqlist L){
int a,i=0;
printf("请输入顺序表A的数据:");
scanf("%d",&a);
if(a=='\n')printf("\n你输入的数据为空,请重新输入:");
while(a!='\n'){
L.elem[i]=a;
scanf("%d",&a);
i++;
}
return OK;
}
void sortlist(Sqlist L){
int temp,j,k;
for(j=1; j<=L.length;j++)
for(k=1;k<=L.length-j;j++)
if(L.elem[k]>L.elem[k+1])
{temp=L.elem[k];
L.elem[k]=L.elem[k+1];
L.elem[k+1]=temp;
}
printf("创建顺序表A成功。\n");
printf("新建顺序表A:");
for(j=1;j<=L.length;j++)printf("%d",L.elem[j-1]);
printf("\n");
}
int ListInsert_Sq(Sqlist L,int x){
int *newbase,*p; int q,i=0;
if(L.length>=L.listsize){
newbase=(int *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(int));
if(!newbase)exit(OVERFLOW);
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
if(x>L.elem[i-1])i++;
q=L.elem[i-1];
p=&(L.elem[L.length-1]);
while(i<=L.length-1&&p>&(L.elem[i])) {
*(p+1)= *p;
p--;
}
L.elem[i]=q;
L.elem[i-1]=x;
L.length++;
return OK;
}
void main()
{
Sqlist A;
int x;
int i;
InitList_Sq(A);
if(InitList_Sq(A)==OK){
if(readlist(A)==OK)
sortlist(A);
}
else printf("创建顺序表A失败\n");
printf("\n请输入所需插入的数据:");
scanf("%d",&x);
if(ListInsert_Sq(A,x)==OK)
printf("\n插入数据成功\n");
sortlist(A);
printf("插入后顺序表A:");
for(i=0;i<=A.length-1;i++)
printf("%d",A.elem[i]);
}