线性表顺序存储结构的插入和删除,程序有点问题
程序有点问题,请各位看看,指点,指点#include <stdio.h>
#include <stdlib.h>
#define Listinitsize 100
#define Listincrement 10
typedef struct {
int *elem;
int length;
int listsize;
}Sqlist;
int InitList_Sq(Sqlist *L)
{
L->elem=(int *)malloc((Listinitsize) * sizeof(int));
if(! L->elem)return(-1);
L->length=0;
L->listsize=Listinitsize;
return 1;
}
int ListInsert_Sq(Sqlist *L,int i,int e)
{
int *p,*q,*base;
if(i<1||i>L->length+1)return 0;
if(L->length>=L->listsize){
base =(int *)realloc(L->elem,(L->listsize+Listincrement) *sizeof(int));
if(! base)return -1;
L->elem=base;
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 1;
}
int ListDelete_Sq(Sqlist *L,int i)
{
int *p,*q;
if((i<1)||(i>L->length))return (-1);
p=&(L->elem[i-1]);
q=L->elem+L->length-1;
printf("the deleted elem is %d \n",L->elem[i-1]);
for(++p;p<=q;++p)
*(p-1)=*p;
--L->length;
return 1;
}
int inputlist(Sqlist *L)
{
int i,*p;
p=L->elem;
printf("the list L_length is:");
scanf("%d",&L->length);
printf("\n");
printf("the list element is:");
for(i=0;i<L->length;i++)
scanf("%d",(p+i));
return 1;
}
int display(Sqlist *L)
{ int i;
printf("the list is:");
for(i=0;i<L->length;i++)
printf("%5d",*(L->elem++));
printf("\n");
return 1;
}
int InitList_Sq(Sqlist *L);
int display(Sqlist *L);
int inputlist(Sqlist *L);
int ListInsert_Sq(Sqlist *L,int i,int e);
int ListDelete_Sq(Sqlist *L,int i);
main()
{
int i,choice, x;
Sqlist La;
InitList_Sq(&La);
inputlist(&La);
do{
printf("1 ListInsert\n");
printf("2 ListDelete\n");
printf("3 exit\n");
printf("welcome to choice: ");
scanf("%d",&choice);
printf("\n");
switch(choice){
case 1:{
printf("i,x=");
scanf("%d,&d",&i,&x);
ListInsert_Sq(&La, i, x);
display(&La);}
break;
case 2:
{
printf("i=");
scanf("%d",&i);
ListDelete_Sq(&La,i);
display(&La);
}
break;}
}while(choice!=3);
getchar();
}