线性表顺序存储结构出了问题
#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;
}
void main()
{
Sqlist *L;
int i;
ListInsert_Sq(L,1,1);
ListInsert_Sq(L,2,2);
ListInsert_Sq(L,3,3);
ListInsert_Sq(L,4,4);
ListInsert_Sq(L,5,5);
for(i=1;i<=L->length;i++)
printf("%-4d",L->elem[i-1]);
}