码迷,mamicode.com
首页 > 编程语言 > 详细

关于顺序表示的线性表一些算法的实现

时间:2015-09-09 21:27:09      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:

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()对表进行遍历
}

void assignment(SqList &L){//对线性表进行赋值
    InitList(L);
    cout << "please input the length of the list:" << endl;
    int n;
    cin >> n;
    cout << "please input the elem:" << endl;
    for (int i = 0; i < n; i++){
        cin >> *(L.elem + i);     //为表中的元素赋值
    }
    L.length = n;
    cout << "the assignment is completed!" << endl;
}
void Union(SqList &La, SqList Lb)//将所有在线性表Lb中但不在La中的数据元素插入到La中
{
    for (int i = 0; i < Lb.length; i++){
        if (!LocateElem(La, *(Lb.elem + i), compare)){//判断Lb的元素是否在La中,如果不在则进行插入操作
            int j = 1;
            ListInsert(La, La.length + j, *(Lb.elem + i));//将判断不在La中的元素插入到La中
            j++;
        }
    }
}
void MergeList(SqList La, SqList Lb, SqList &Lc)
//已知线性表La和Lb中的数据元素按值非递减排列
//归并La和Lb得到新的线性表Lc,Lc的数据元素也按值非递减排列
{   
    int num_a = 0, num_b = 0;
    int i = 0;
    for (; i < (La.length + Lb.length) && num_a < La.length&&num_b < Lb.length; i++){//如果La先被归并到Lc,或者Lb先被归并到Lc,则循环停止
        if (*(La.elem + num_a) <= *(Lb.elem + num_b)){          //La的数据元素值小,则首先被赋给Lc的数据
            *(Lc.elem + i) = *(La.elem + num_a++);
        }
        else{                                                  //否则Lb的数据元素被赋给Lc的数据
            *(Lc.elem + i) = *(Lb.elem + num_b++);
        }
    }
    if (num_a == La.length){                                   //如果La中的数据首先被完全归并到Lc中,则Lb中还有数据未被归并到Lc中,继续归并
        for (; i < (La.length + Lb.length)&&num_b<Lb.length; i++){
            *(Lc.elem + i) = *(Lb.elem + num_b++);
        }
    }
    else{                                                      //否则La中还有数据未被归并到Lc中,继续归并
        for (; i < (La.length + Lb.length)&&num_a<La.length; i++){
            *(Lc.elem + i) = *(La.elem + num_a++);
        }
    }
    Lc.length = La.length + Lb.length;                         //将La.length+Lb.length的值赋给Lc.length
    
}
void MergeList_t(SqList La, SqList Lb, SqList &Lc)
//这是书上的通过调用基本操作来完成合并
//已知线性表La和Lb中的数据元素按值非递减排列
//归并La和Lb得到新的线性表Lc,Lc的数据元素也按值非递减排列
{
    int i =1, j = 1, k = 0;
    int La_len = ListLength(La);//将La.length赋给La_len
    int Lb_len = ListLength(Lb);//将Lb.length赋给Lb_len
    Elemtype ai, bj;     
    while ((i <= La_len) && (j <= Lb_len)){
        
        GetElem(La, i, ai);//得到i位的La中数据元素的值并赋给ai
        GetElem(Lb, j, bj);//得到j位的Lb中数据元素的值并赋给bj
        if (ai <= bj){//ai小则首先插入Lc中,并且i++
            ListInsert(Lc, ++k, ai);
            i++;
        }
        else{       //bj小则bj插入Lc中,并且j++
            ListInsert(Lc, ++k, bj);
            j++;
        }    
    }
    while (i <= La_len){//如果La中还有剩余元素,则继续插入Lc中
            GetElem(La, i++, ai);
            ListInsert(Lc, ++k, ai);
        }
    while (j <= Lb_len){//如果Lb中还有剩余元素,则继续插入Lc中
        GetElem(Lb, j++, bj);
        ListInsert(Lc, ++k, bj);
    }
}

 

关于顺序表示的线性表一些算法的实现

标签:

原文地址:http://www.cnblogs.com/csudanli/p/4795817.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!