#include <stdio.h>

#include <stdlib.h>

#include <malloc.h>

#define LISTINCREASMENT 100                 /*每次分配元素的个数*/

#define  LISTSIZE 10                           /*顺序存储的最大个数*/

#define  OVERFLOW -1

#define  OK 1  

typedef int ElemType;


typedef struct                                   /*顺序表元素的的定义*/

{

    ElemType * elem;

    int length;

    int listsize;

} Sqlist;


int SqInitial(Sqlist &L)                           /*初始化线性表*/

{

    L.elem=(ElemType *) malloc (LISTSIZE*sizeof(ElemType));

    if (! L.elem)  exit(OVERFLOW); //存储分配失败 

    L.length=0;

    L.listsize=LISTSIZE;

    return OK;

}


int ListInsert(Sqlist &L,int i,ElemType e)            /*插入元素*/

{

    if(i<1|| i > L.length+1) printf("ERROR!");

    if(L.length>=L.listsize)

    {

        ElemType*newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREASMENT)

                                            *sizeof(ElemType));

        if(!newbase)   return  OVERFLOW;// 当前存储空间已满


L.elem=newbase;

        L.listsize+=LISTINCREASMENT;         /*表的容量不足分配内存*/

    }

    ElemType *q=&(L.elem[i-1]);

    ElemType *p;

    for(p=&(L.elem[L.length-1]); p>=q; --p)

        *(p+1)=*p;

    *q=e;

    ++L.length;

    return OK;


}


void ListDelete(Sqlist &L,int i,ElemType &e)           //删除线性表中第i个位置上的元素

{

    if(i<1||i>L.length) printf("ERROR!");

    else

    {

        e=L.elem[i-1];

        for(;i<L.length;i++)

        {

            L.elem[i-1]=L.elem[i];

        }

        L.length--;

    }


}

ElemType GetElem(Sqlist &L,int i)

{

    if(i<1||i>L.length) printf("ERROR!");

    else

    {

        return L.elem[i-1];

    }

}