#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int ElemType;
typedef int Status;
#define LIST_INIT_SIZE 100
#define LIST_INCREMENT 10
typedef struct {
ElemType *elem;
int length;
int listSize;
} SqList;
Status initList(SqList *L)
{
L->elem=(ElemType *) malloc(LIST_INIT_SIZE * sizeof(ElemType));
if( L->elem == NULL)
{
return ERROR;
}
L->length = 0;
L->listSize = LIST_INIT_SIZE;
return OK;
}
Status insertList(SqList *L, int i,ElemType e)
{
if(i<1 || i>L->length +1 )
{
return ERROR;
}
if(L->length >= L->listSize){
ElemType *newbase = (ElemType *) realloc(L->elem,(L->listSize+LIST_INCREMENT) * sizeof(ElemType));
if (newbase == NULL) exit(OVERFLOW);
L->elem =newbase;
L->listSize +=LIST_INCREMENT;
}
for (int j=L->length -1 ; j>=i-1;--j){
L->elem[j+1]=L->elem[j];
}
L->elem[i-1]=e;
++L->length;
return OK;
}
void traverseList(SqList L){
printf("SqList = [");
for (int i=0;i<L.length;++i){
printf("%d",L.elem[i]);
if(i != L.length -1)printf(",");
}
printf("]\n");
}
int main()
{
SqList L;
initList(&L);
insertList(&L,1,42);
insertList(&L,1,63);
insertList(&L,1,92);
insertList(&L,1,46);
traverseList(L);
return 0;
}