1.头文件 声明和宏定义

#include 
#include 

#define ElemType int
#define OK 1
#define ERROR 0
typedef int Status;

2.结构体声明

typedef struct SqList
{
    ElemType* data;
    int listSize;
    int len;
}SqList,*pSqList;

3.类和方法的声明

#define  LIST_MAX_SIZE 100
#define  LIST_INCREMENT 10

class CSqList
{
public:
    CSqList();
    ~CSqList();
    void   CreateList_L(SqList& L);
    Status AppendElem_L(SqList& L, ElemType &e);
    Status GetElem_L(SqList& L,int i, ElemType &e);
    Status ListInsert_L(SqList& L,int i, ElemType &e);
    Status ListDelete_L(SqList& L,int i, ElemType &e);
    void   DestroyList_L(SqList& L);

};

4.类的方法实现

CSqList::CSqList(){

}
CSqList::~CSqList(){

}
void   CSqList::CreateList_L(SqList& L)
{
    L.data = (ElemType*)malloc(LIST_MAX_SIZE*sizeof(SqList));
    L.len = 0;
    L.listSize = LIST_MAX_SIZE;

}
Status CSqList::AppendElem_L(SqList& L, ElemType &e){

    if (L.len>=L.listSize)
    {
        ElemType* q = (ElemType*)realloc(L.data,(L.listSize+LIST_INCREMENT)*sizeof(ElemType));
        if (!q)
            return ERROR;
        L.data = q;
        L.listSize +=LIST_INCREMENT;

    }
    L.data[L.len++] = e;
}
Status CSqList::GetElem_L(SqList& L,int i, ElemType &e){
    if (i>L.len)
        return ERROR;
    e=L.data[i-1];
    return OK;
}
Status CSqList::ListInsert_L(SqList& L,int i, ElemType &e){
    if (L.len>=L.listSize)
    {
        ElemType* q = (ElemType*)realloc(L.data,(L.listSize+LIST_INCREMENT)*sizeof(ElemType));
        if (!q)
            return ERROR;
        L.data = q;
        L.listSize +=LIST_INCREMENT;

    }
    for (int k=L.len;k>=i;k--)
        L.data[k] = L.data[k-1];//*p = *--p;
    L.data[i-1] = e;
    L.len++;
}
Status CSqList::ListDelete_L(SqList& L,int i, ElemType &e){
    if (i>L.len)
    {
        return ERROR;
    }
    e=L.data[i-1];
    for (int k=i-1;k

5.测试

int _tmain(int argc, _TCHAR* argv[]){
    CSqList cSqList;
    SqList sq;
    cSqList.CreateList_L(sq);
    int i=0;
    while(i++<10)
        cSqList.AppendElem_L(sq,i);
    int e = 999;
    cSqList.ListInsert_L(sq,5,e);
    i=1;
    while(i<=sq.len){
        cSqList.GetElem_L(sq,i,e);
        printf("%d:%d\n",i++,e);
    }
    cSqList.ListDelete_L(sq,6,e);
    printf("delete:%d",e);
    cSqList.DestroyList_L(sq);
    getchar();
}