#include "stdio.h"
#include "malloc.h"
#define OK 1
#define OVERFLOW -1
#define ERROR 0
#define LIST_INIT_SIZE 100/*线性表存储空间的初始分配量*/
#define LISTINCREMENT 10/*线性表存储空间的分配增量*/
typedef int ElemType;
typedef int Status;
typedef struct{
ElemType *elem;/*存储空间基址*/
int length;/*当前长度*/
int listsize;/*当前分配的存储增量*/
}sqlist;
/*构造一个空的线性表 L*/
Status InitList_sq(sqlist *L){
L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));/*(ElemType*)使 malloc 返
回的指针转换为指向 ElemType 类型数据的指针*/
if(!L->elem) exit(OVERFLOW);/*分配失败*/
L->length=0;/*空表长度为 0*/
L->listsize=LIST_INIT_SIZE;/*初始存储容量*/
return OK;
}
/*在 L 中的第 i 个位置之前插入元素 e*/
Status ListInsert_Sq(sqlist *L,int i,ElemType e){
ElemType *newbase,*p,*q;
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=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;
}
Status ListDelete_Sq(sqlist *L,int i,ElemType *e){
ElemType *p,*q;
if(i<1||i>L->length) return ERROR;