线性表的初始化,查找,插入,删除的程序错在哪里???还请指教。调试了几次,改了几次还是通不过
# include<stdio.h># include<stdlib.h>
# define LIST_INIT_SIZE 100
# define LISTINCREMENT 10
# define OVERFLOW -2
# define OK 1
# define ERROR 0
typedef int ElemType;
typedef struct Sqlist
{
ElemType *elem;
int length;
int listsize;
}Sqlist;
void main()
{
Sqlist *L,S;
L=&S;
int i,j,e,n;
int InitList_Sq(Sqlist *L,int n);
int LocateELem_Sq(Sqlist *L,int i,ElemType e);
int ListInsert_Sq(Sqlist *L,int i,ElemType e);
int ListDelete_Sq(Sqlist *L,int i);
int PutList(Sqlist *L);
printf("输入表的长度:\n");
scanf("%d",&n);
InitList_Sq(L,n);
PutList(L);
printf("输入查找的位置:\n");
scanf("%d",&i);
LocateElem_Sq(L,i);
PutList(L);
printf("输入插入的位置:\n");
scanf("%d",&i);
printf("输入插入的值:\n");
scanf("%d",&e);
ListInsert_Sq(L,i,e);
PutList(L);
printf("输入删除的位置:\n");
scanf("%d",&j);
ListDelete_Sq(L,j);
PutList(L);
}
int InitList_Sq(Sqlist *L,int n)
{
int i;
ElemType *newbase;
L->elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->elem)
exit(OVERFLOW);
L->length=0;
L->listsize=LIST_INIT_SIZE;
printf("请输入表中的数据:\n");
for(i=0;i<n;i++)
{
scanf("%d",&L->elem[i]);
++L->length;
if(L->length>L->listsize)
{
newbase=(ElemType*)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase)
exit(OVERFLOW);
L->elem=newbase;
L->listsize+=LISTINCREMENT;
}
}
return OK;
}
int ListInsert_Sq(Sqlist *L,int i,ElemType e)
{
ElemType *p,*q,*newbase;
if(i<1||i>L->length+1)
return ERROR;
if(L->length>=L->listsize)
{
newbase=(ElemType*)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase)
exit(OVERFLOW);
L->elem=newbase;
L->listsize+=LISTINCREMENT;
}
q=&L->elem[i-1];
for(p=&L->elem[L->length-1];p>=q;--p)
*(p+1)=*p;
*q=e;
++L->length;
return OK;
}
int LocateELem_Sq(SqList *L,ElemType e){
for(i=0;i<L.length;i++)
if(L.elem[i]==e) return i+1;
printf("查找的元素为:\n%d\n",e);
return 0;
}
int ListDelete_Sq(Sqlist *L,int i)
{
ElemType *p,*q;
int e;
if(i<1||i>L->length)
return ERROR;
p=&L->elem[i-1];
e=*p;
q=&(L->elem[L->length-1]);
for(++p;p<=q;++p)
*(p-1)=*p;
--L->length;
printf("删除的元素为:\n%d\n",e);
return 0;
}
int PutList(Sqlist *L)
{
int *p,*q;
p=L->elem;
q=L->elem+(L->length-1);
printf("线性表中元素为:\n");
for(p=L->elem;p<=q;++p)
{
printf("%d ",*p);
}
printf("\n表长为:%d \n",L->length);
return OK;
}