算法与数据结构--实现线性表的合并操作(合并后按非递减排列)

/*
文件名称:实现线性表的合并操作(合并后按非递减排列)

*/

#include <bits/stdc++.h>  
  
using namespace std;  
  
#define LIST_INIT_SIZE    100   //线性表存储空间的初始分配量  
  
#define LISTINCREMENT   10  //线性表存储空间的分配增量  
  
typedef int ElemType;      //定义别名  
  
typedef int Status;      //定义别名  
  
typedef struct{  
    ElemType *elem;     //存储空间基址  
    int length;     //当前长度  
    int listsize;       //当前分配的存储容量(以sizeof(ElemType)为单位)  
}SqList;  
  
  
Status InitList_Sq(SqList &L){  
    //构造一个空的线性表L  
    L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));  
    if(!L.elem)  
        exit(1);        //存储分配失败,z正常退出  
    L.length = 0;       //空表长度为0  
    L.listsize = LIST_INIT_SIZE;        //初始存储容量  
    return true;  
}  
  
Status ListInsert_Sq(SqList &L,int i,ElemType e)  
{  
    //在顺序线性表L中第i个位置之前插入新的元素e  
    //i的合法值为1<=i<=ListLength_Sq(L)+1  
    if(i <1 || i> L.length + 1)  
        return false;   //i值不合法  
    if(L.length >= L.listsize)   //当前存储空间已满,增加分配  
    {  
        ElemType *newbase = (ElemType *)realloc(L.elem,(L.listsize + LISTINCREMENT )* sizeof(ElemType));  
        if(!newbase)  
            exit(1);    //存储分配失败  
        L.elem = newbase;//新基址  
        L.listsize += LISTINCREMENT;    //增加存储容量  
    }  
      
    ElemType *q = &(L.elem[i-1]);//q为插入位置  
      
    for(ElemType *p = &(L.elem[L.length-1]);p>=q;--p)  
        *(p+1) = *p;    //插入位置及之后的元素右移  
      
    *q = e;     //插入e  
    ++L.length;     //表长增1  
    return true;  
}  
  
Status ListDelete_Sq(SqList &L,int i)  
{  
    //在顺序线性表L中删除第i个元素,并用e返回其值  
    //i的合法值为 1<= i<=ListLength_Sq(L)  
    if((i<1)||(i>L.length))  
        return false;   //i值不合法  
    ElemType *p = &(L.elem[i-1]);   //p为被删除元素的位置  
    ElemType e = *p;    //被删除元素的值赋值给e  
    ElemType *q = L.elem + L.length-1;  //表尾元素的位置  
    for(++p;p<=q;++p)      
        *(p-1) = *p;    //被删除元素之后的元素左移  
    --L.length; //表长减1  
    return e;  
}  
  
  
Status compare(ElemType e1,ElemType e2)  
{  
    if(e1==e2)  
        return true;  
    return false;  
  
}  
  
int LocateElem_sq(SqList L,ElemType e,Status (*compare)(ElemType,ElemType))  
{  
    //在顺序线性表L中查找第1个值与e满足compare()的元素的为序  
    //若找到,则返回其在L中的位序,否则返回0  
    int i = 1;//i的初值为第1个元素的位序  
    ElemType *p = L.elem;       //p的初值为为第1个元素的存储位置  
    while(i<=L.length &&!(*compare)(*p++,e))  
    {  
        ++i;  
    }  
    if(i<=L.length)  
    {  
        return i;  
    }  
    else   
    {  
        return 0;  
    }  
}  
  
void input(SqList L)  
{  
    int i = 1;//i的初值为第1个元素的位序  
    ElemType *p = L.elem;       //p的初值为为第1个元素的存储位置  
    while(i<=L.length)  
    {  
        cout<<*(p++)<<"    ";  
        ++i;  
    }  
    cout<<endl;  
}  
  
void MergeList_Sq(SqList La,SqList Lb,SqList &Lc)  
{  
    //已知顺序线性表La和Lb的元素按值非递减排列  
    //归并La和Lb得到新的顺序线性表Lc,Lc的元素也按值非递减排列  
    ElemType *pa = La.elem;  
    ElemType *pb = Lb.elem;  
    Lc.listsize = Lc.length = La.length + Lb.length;  
    ElemType *pc = Lc.elem = (ElemType *)malloc(Lc.listsize * sizeof(ElemType));  
    if(!Lc.elem)  
        exit(1);    //存储分配失败,非正常退出  
    ElemType *pa_last = (La.elem + La.length -1);  
    ElemType *pb_last = (Lb.elem + Lb.length -1);  
    while(pa<=pa_last&&pb<=pb_last)//归并  
    {  
        if(*pa<=*pb)  
        {  
            *pc++=*pa++;  
        }  
        else  
        {  
            *pc++=*pb++;  
        }  
    }  
    while(pa<=pa_last)//插入La的剩余元素  
    {  
        *pc++=*pa++;  
    }  
    while(pb<=pb_last)//插入Lb的剩余元素  
    {  
        *pc++=*pb++;  
    }  
}  
  
int main()  
{  
    SqList La,Lb,Lc;  
    InitList_Sq(La);  
    InitList_Sq(Lb);  
    InitList_Sq(Lc);  
    ListInsert_Sq(La,1,2);//在顺序线性表L中第i个位置之前插入新的元素e  
    ListInsert_Sq(La,2,3);//在顺序线性表L中第i个位置之前插入新的元素e  
    ListInsert_Sq(La,3,4);//在顺序线性表L中第i个位置之前插入新的元素e  
    ListInsert_Sq(La,4,5);//在顺序线性表L中第i个位置之前插入新的元素e  
    ListInsert_Sq(Lb,1,6);//在顺序线性表L中第i个位置之前插入新的元素e  
    ListInsert_Sq(Lb,2,7);//在顺序线性表L中第i个位置之前插入新的元素e  
    ListInsert_Sq(Lb,3,8);//在顺序线性表L中第i个位置之前插入新的元素e  
    ListInsert_Sq(Lb,4,9);//在顺序线性表L中第i个位置之前插入新的元素e  
    //ListInsert_Sq(Lc,1,2);//在顺序线性表L中第i个位置之前插入新的元素e  
    //ListInsert_Sq(Lc,2,3);//在顺序线性表L中第i个位置之前插入新的元素e  
    //ListInsert_Sq(Lc,3,4);//在顺序线性表L中第i个位置之前插入新的元素e  
    //ListInsert_Sq(Lc,4,5);//在顺序线性表L中第i个位置之前插入新的元素e  
    //ElemType e = ListDelete_Sq(La,1);  
    cout<<"线性表中La的所有元素为:";  
    input(La);  
    cout<<"线性表中Lb的所有元素为:";  
    input(Lb);  
    //int e = LocateElem_sq(La,3,(*compare));//在顺序线性表L中查找第1个值与e满足compare()的元素的为序  
    //cout<<"线性表中与3相等的元素的位序为:"<<e<<endl;  
    cout<<"La与Lb合并后,线性表中Lc的所有元素为:"<<endl;  
    MergeList_Sq(La,Lb,Lc);  
    input(Lc);  
    system("PAUSE");  
    return 0;  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Taylor_29511

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值