线性表的顺序表示与实现

#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define MYOVERFLOW -2
typedef int Status;
typedef int Elemtype;//用指定标识符Elemtype代表int类型,顾名思义表示元素类型为int型
typedef struct{      //在struct之前用了关键字typedef,表明是声明新类型名,新类型名为SqList
    Elemtype *elem;  //在新类型SqList中,有三个数据,一个为指向数据元素的指针,也是储存空间基址
    int length;      //当前长度
    int listsize;    //当前分配的储存容量(以sizeof(ElemType)为单位)
}SqList;
int compare(SqList l, Elemtype e);//用来比较的函数,现在选用==
bool visit(SqList L);   //遍历表中的数据元素
Status InitList(SqList &L);//构造一个空的线性表
Status DestroyList(SqList &L);//摧毁线性表L
Status ClearList(SqList &L);//将L重置为空表
bool ListEmpty(SqList L);//若L为空表,则返回TRUE,否则返回FALSE
int ListLength(SqList L);//返回L中的数据元素个数
Status GetElem(SqList L, int i, Elemtype &e);//用e返回L中第i个数据元素的值
int LocateElem(SqList L, Elemtype e, int compare(SqList l, Elemtype e));//返回L中第一个与e满足关系compare()的数据元素的
                                                                        //位序。若这样的元素不存在,则返回值为0
Status PriorElem(SqList L, Elemtype cur_e, Elemtype &pre_e);//若cur_e是L的数据元素,且不是第一个,则用pre_e返回它的前驱
                                                            //,否则操作失败,pre_e无定义
Status NextElem(SqList L, Elemtype cur_e, Elemtype &next_e);//若cur_e是L的数据元素,且不是最后一个,则用next_e返回它的后继,
                                                            //若操作失败,next_e无定义
Status ListInsert(SqList &L, int i, Elemtype e);  //在L中第i个位置之前插入新的数据元素e,L的长度加1
Status ListDelete(SqList &L, int i, Elemtype &e); //删除L的第i个数据元素,并用e返回其值,L的长度减1
void   ListTraverse(SqList L, bool (*p)(SqList L));    //依次对L的每个数据元素调用visit(),一旦visit()失败,则操作失败


int compare(SqList l, Elemtype e)//用来比较的函数,现在选用==
{
    int position=0;//表中没有元素e时,返回position=0
    for (int i = 0; i < l.length; i++){
        if (e == *(l.elem + i)){//找到e的位置,按照平常人的习惯,将i+1赋给position
            position = i + 1;
            break;
        }
    }
    return position;
}
bool visit(SqList L){   //遍历表中的数据元素
    if (!(&L.elem)){    //如果L的元素首地址不存在,说明L并未初始化,L不存在
        cout << "the List is't existed!" << endl;
        return FALSE;
    }
    if (L.length == 0)cout << "it is an empty list!" << endl;
    else
    cout << "the elem of the list is:" << endl;
    for (int i = 0; i < L.length; i++)//输出表中的每一个值
        cout << *(L.elem + i) << " ";
    cout << endl;
    return TRUE;
}
Status InitList(SqList &L)//构造一个空的线性表
{
    L.elem = (Elemtype *)malloc(LIST_INIT_SIZE*sizeof(Elemtype));//为L分配一个大小为LIST_INIT_SIZE*sizeof(Elemtype)大小的空间
    if (!L.elem){
        cout << "out of memory,space allocation is failed!";
        exit(MYOVERFLOW);
    }//内存不足,空间分配失败
    L.length = 0;  //空表长度为0
    L.listsize = LIST_INIT_SIZE;//初始储存容量
    cout << "the initialization of List is succeed!" << endl;
    return OK;
}
Status DestroyList(SqList &L)//摧毁线性表L,初始条件为线性表已存在
{
    if (!(&L.elem)){    //如果L的元素首地址不存在,说明L并未初始化,L不存在
        cout << "the List is't existed!" << endl;
        exit(-1);
    }    
    delete L.elem; //删除为L分配的空间
    L.length = 0;  //L的length和listsize都设置为0
    L.listsize = 0;
    cout << "the List is destroyed!" << endl;
    return OK;

}
Status ClearList(SqList &L)//将L重置为空表
{
    if (!(L.elem)){    //如果L的元素首地址不存在,说明L并未初始化,L不存在
        cout << "the List is't existed!" << endl;
        exit(-1);
    }
    delete L.elem; //删除为L分配的空间
    L.elem = (Elemtype *)malloc(LIST_INIT_SIZE*sizeof(Elemtype));//为L分配一个大小为LIST_INIT_SIZE*sizeof(Elemtype)大小的空间
    if (!L.elem){
        exit(MYOVERFLOW);
    }//内存不足,空间分配失败
    L.length = 0;  //空表长度为0
    L.listsize = LIST_INIT_SIZE;//初始储存容量
    cout << "the list has been reset!" << endl;
    return OK;
}
bool ListEmpty(SqList L)//若L为空表,则返回TRUE,否则返回FALSE
{ 
    if (!(L.elem)){    //如果L的元素首地址不存在,说明L并未初始化,L不存在
        cout << "the List is't existed!" << endl;
        exit(-1);
    }
    if (L.length == 0)return TRUE;
    else return FALSE;
}
int ListLength(SqList L)//返回L中的数据元素个数
{
    if (!(L.elem)){    //如果L的元素首地址不存在,说明L并未初始化,L不存在
        cout << "the List is't existed!" << endl;
        exit(-1);
    }
    return L.length;
}
Status GetElem(SqList L, int i, Elemtype &e)//用e返回L中第i个数据元素的值
{
    if (!(L.elem)){    //如果L的元素首地址不存在,说明L并未初始化,L不存在
        cout << "the List is't existed!" << endl;
        exit(-1);
    }
    if(0 >=i || i > L.length){  //如果i<0或者i>大于length,则超出了list的范围
        cout << "can't find the position of    " << i << endl;
        e = NULL;
        return ERROR;
    }
    else
    e = *(L.elem + i-1);
    return OK;
}
int LocateElem(SqList L, Elemtype e, int (*p)(SqList , Elemtype ))//返回L中第一个与e满足关系compare()的数据元素的
                                                                       //位序。若这样的元素不存在,则返回值为0
{
    if (!(L.elem)){    //如果L的元素首地址不存在,说明L并未初始化,L不存在
        cout << "the List is't existed!" << endl;
        exit(-1);
    }
    int i;
    i = p(L, e);//通过compare()函数得到位置,赋值给i
    return i;
}
Status PriorElem(SqList L, Elemtype cur_e, Elemtype &pre_e)//若cur_e是L的数据元素,且不是第一个,则用pre_e返回它的前驱
                                                           //,否则操作失败,pre_e无定义
{
    if (!(L.elem)){    //如果L的元素首地址不存在,说明L并未初始化,L不存在
        cout << "the List is't existed!" << endl;
        exit(-1);
    }
    int i = LocateElem(L, cur_e,compare);//调用LocateElem()函数,得到i的位置
    if (i == 1){
        cout << "It's the first elem,which doesn't has a prior elem!";//如果是第一个元素,则没有前驱
        return ERROR;
    }
    if (i == 0){                     //返回0说明cur_e不是表中的元素
        cout << "the elem doesn't exist in the list!";
        return ERROR;
    }
    else pre_e = *(L.elem + i - 2);//得到pre_e的值
    return OK;
}
Status NextElem(SqList L, Elemtype cur_e, Elemtype &next_e)//若cur_e是L的数据元素,且不是最后一个,则用next_e返回它的后继,
                                                           //若操作失败,next_e无定义
{
    if (!(L.elem)){    //如果L的元素首地址不存在,说明L并未初始化,L不存在
        cout << "the List isn't existed!" << endl;
        exit(-1);
    }
    int i = LocateElem(L, cur_e, compare);//调用LocateElem()函数,得到i的位置
    if (i == L.length){        //如果是最后一个元素,则没有后继
        cout << "It's the last elem,which doesn't has a next elem!";
        return ERROR;
    }
    if (i == 0){              //返回0说明cur_e不是表中的元素
        cout << "the elem doesn't exist in the list!" << endl;
        return ERROR;
    }
    else next_e = *(L.elem + i);//得到next_e
    return OK;
}
Status ListInsert(SqList &L, int i, Elemtype e)  //在L中第i个位置之前插入新的数据元素e,L的长度加1
{
    if (!(L.elem)){    //如果L的元素首地址不存在,说明L并未初始化,L不存在
        cout << "the List is't existed!" << endl;
        exit(-1);
    }
    if ((1 <= i )&&(i<= L.length + 1)){
        if (L.length >= L.listsize){
            Elemtype *newbase;
            newbase = (Elemtype *)realloc(L.elem, (L.listsize + LISTINCREMENT)*sizeof(Elemtype));
            if (!newbase)exit(MYOVERFLOW);
            L.elem = newbase;
            L.listsize += LISTINCREMENT;
        }
        for (int j = L.length; j >= i;j--){  //将i后的数据元素全部后移一位
            *(L.elem + j) = *(L.elem + j - 1);
        }
        *(L.elem + i-1) = e;//将i位赋值e
        L.length += 1;      //表的长度加1
        return OK;
    }
    else {                //当i不在[1,L.length+1]之间时,无法插入元素
        cout << "can't find the position of " <<i<< " in the list!"<<endl;
        return ERROR;
    }
}
Status ListDelete(SqList &L, int i, Elemtype &e)//删除L的第i个数据元素,并用e返回其值,L的长度减1
{
    if (!(L.elem)){    //如果L的元素首地址不存在,说明L并未初始化,L不存在
        cout << "the List is't existed!" << endl;
        exit(-1);
    }
    if ((1 <= i) &&(i<= L.length)){
        e = *(L.elem + i - 1);//将i位的值赋给e
        for (; i < L.length;i++){  //将i后包括i的数据元素全部前移一位
            *(L.elem + i-1) = *(L.elem + i);
        }
        L.length -= 1;      //表的长度减1
        return OK;
    }
    else {                //当i不在[1,L.length]之间时,无法删除元素
        cout << "can't find the position of "<< i<< " in the list!" << endl;
        return ERROR;
    }
}
void   ListTraverse(SqList L, bool(*p)(SqList L))   //依次对L的每个数据元素调用visit(),一旦visit()失败,则操作失败
{
    if (!(&L.elem)){    //如果L的元素首地址不存在,说明L并未初始化,L不存在
        cout << "the List is't existed!" << endl;
        exit(-1);
    }
     (*p)(L);    //调用visit()对表进行遍历
}
原文地址:https://www.cnblogs.com/csudanli/p/4795813.html