《数据结构之线性表合并操作》

  1. #include <iostream>  
  2.   
  3. using namespace std;  
  4.   
  5. #define LIST_INIT_SIZE    100   //线性表存储空间的初始分配量  
  6.   
  7. #define LISTINCREMENT   10  //线性表存储空间的分配增量  
  8.   
  9. typedef int ElemType;      //定义别名  
  10.   
  11. typedef int Status;      //定义别名  
  12.   
  13. typedef struct{  
  14.     ElemType *elem;     //存储空间基址  
  15.     int length;     //当前长度  
  16.     int listsize;       //当前分配的存储容量(以sizeof(ElemType)为单位)  
  17. }SqList;  
  18.   
  19.   
  20. Status InitList_Sq(SqList &L){  
  21.     //构造一个空的线性表L  
  22.     L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));  
  23.     if(!L.elem)  
  24.         exit(1);        //存储分配失败,z正常退出  
  25.     L.length = 0;       //空表长度为0  
  26.     L.listsize = LIST_INIT_SIZE;        //初始存储容量  
  27.     return true;  
  28. }  
  29.   
  30. Status ListInsert_Sq(SqList &L,int i,ElemType e)  
  31. {  
  32.     //在顺序线性表L中第i个位置之前插入新的元素e  
  33.     //i的合法值为1<=i<=ListLength_Sq(L)+1  
  34.     if(i <1 || i> L.length + 1)  
  35.         return false;   //i值不合法  
  36.     if(L.length >= L.listsize)   //当前存储空间已满,增加分配  
  37.     {  
  38.         ElemType *newbase = (ElemType *)realloc(L.elem,(L.listsize + LISTINCREMENT )* sizeof(ElemType));  
  39.         if(!newbase)  
  40.             exit(1);    //存储分配失败  
  41.         L.elem = newbase;//新基址  
  42.         L.listsize += LISTINCREMENT;    //增加存储容量  
  43.     }  
  44.       
  45.     ElemType *q = &(L.elem[i-1]);//q为插入位置  
  46.       
  47.     for(ElemType *p = &(L.elem[L.length-1]);p>=q;--p)  
  48.         *(p+1) = *p;    //插入位置及之后的元素右移  
  49.       
  50.     *q = e;     //插入e  
  51.     ++L.length;     //表长增1  
  52.     return true;  
  53. }  
  54.   
  55. Status ListDelete_Sq(SqList &L,int i)  
  56. {  
  57.     //在顺序线性表L中删除第i个元素,并用e返回其值  
  58.     //i的合法值为 1<= i<=ListLength_Sq(L)  
  59.     if((i<1)||(i>L.length))  
  60.         return false;   //i值不合法  
  61.     ElemType *p = &(L.elem[i-1]);   //p为被删除元素的位置  
  62.     ElemType e = *p;    //被删除元素的值赋值给e  
  63.     ElemType *q = L.elem + L.length-1;  //表尾元素的位置  
  64.     for(++p;p<=q;++p)      
  65.         *(p-1) = *p;    //被删除元素之后的元素左移  
  66.     --L.length; //表长减1  
  67.     return e;  
  68. }  
  69.   
  70.   
  71. Status compare(ElemType e1,ElemType e2)  
  72. {  
  73.     if(e1==e2)  
  74.         return true;  
  75.     return false;  
  76.   
  77. }  
  78.   
  79. int LocateElem_sq(SqList L,ElemType e,Status (*compare)(ElemType,ElemType))  
  80. {  
  81.     //在顺序线性表L中查找第1个值与e满足compare()的元素的为序  
  82.     //若找到,则返回其在L中的位序,否则返回0  
  83.     int i = 1;//i的初值为第1个元素的位序  
  84.     ElemType *p = L.elem;       //p的初值为为第1个元素的存储位置  
  85.     while(i<=L.length &&!(*compare)(*p++,e))  
  86.     {  
  87.         ++i;  
  88.     }  
  89.     if(i<=L.length)  
  90.     {  
  91.         return i;  
  92.     }  
  93.     else   
  94.     {  
  95.         return 0;  
  96.     }  
  97. }  
  98.   
  99. void input(SqList L)  
  100. {  
  101.     int i = 1;//i的初值为第1个元素的位序  
  102.     ElemType *p = L.elem;       //p的初值为为第1个元素的存储位置  
  103.     while(i<=L.length)  
  104.     {  
  105.         cout<<*(p++)<<"    ";  
  106.         ++i;  
  107.     }  
  108.     cout<<endl;  
  109. }  
  110.   
  111. void MergeList_Sq(SqList La,SqList Lb,SqList &Lc)  
  112. {  
  113.     //已知顺序线性表La和Lb的元素按值非递减排列  
  114.     //归并La和Lb得到新的顺序线性表Lc,Lc的元素也按值非递减排列  
  115.     ElemType *pa = La.elem;  
  116.     ElemType *pb = Lb.elem;  
  117.     Lc.listsize = Lc.length = La.length + Lb.length;  
  118.     ElemType *pc = Lc.elem = (ElemType *)malloc(Lc.listsize * sizeof(ElemType));  
  119.     if(!Lc.elem)  
  120.         exit(1);    //存储分配失败,非正常退出  
  121.     ElemType *pa_last = (La.elem + La.length -1);  
  122.     ElemType *pb_last = (Lb.elem + Lb.length -1);  
  123.     while(pa<=pa_last&&pb<=pb_last)//归并  
  124.     {  
  125.         if(*pa<=*pb)  
  126.         {  
  127.             *pc++=*pa++;  
  128.         }  
  129.         else  
  130.         {  
  131.             *pc++=*pb++;  
  132.         }  
  133.     }  
  134.     while(pa<=pa_last)//插入La的剩余元素  
  135.     {  
  136.         *pc++=*pa++;  
  137.     }  
  138.     while(pb<=pb_last)//插入Lb的剩余元素  
  139.     {  
  140.         *pc++=*pb++;  
  141.     }  
  142. }  
  143.   
  144. void main()  
  145. {  
  146.     SqList La,Lb,Lc;  
  147.     InitList_Sq(La);  
  148.     InitList_Sq(Lb);  
  149.     InitList_Sq(Lc);  
  150.     ListInsert_Sq(La,1,2);//在顺序线性表L中第i个位置之前插入新的元素e  
  151.     ListInsert_Sq(La,2,3);//在顺序线性表L中第i个位置之前插入新的元素e  
  152.     ListInsert_Sq(La,3,4);//在顺序线性表L中第i个位置之前插入新的元素e  
  153.     ListInsert_Sq(La,4,5);//在顺序线性表L中第i个位置之前插入新的元素e  
  154.     ListInsert_Sq(Lb,1,6);//在顺序线性表L中第i个位置之前插入新的元素e  
  155.     ListInsert_Sq(Lb,2,7);//在顺序线性表L中第i个位置之前插入新的元素e  
  156.     ListInsert_Sq(Lb,3,8);//在顺序线性表L中第i个位置之前插入新的元素e  
  157.     ListInsert_Sq(Lb,4,9);//在顺序线性表L中第i个位置之前插入新的元素e  
  158.     //ListInsert_Sq(Lc,1,2);//在顺序线性表L中第i个位置之前插入新的元素e  
  159.     //ListInsert_Sq(Lc,2,3);//在顺序线性表L中第i个位置之前插入新的元素e  
  160.     //ListInsert_Sq(Lc,3,4);//在顺序线性表L中第i个位置之前插入新的元素e  
  161.     //ListInsert_Sq(Lc,4,5);//在顺序线性表L中第i个位置之前插入新的元素e  
  162.     //ElemType e = ListDelete_Sq(La,1);  
  163.     cout<<"线性表中La的所有元素为:";  
  164.     input(La);  
  165.     cout<<"线性表中Lb的所有元素为:";  
  166.     input(Lb);  
  167.     //int e = LocateElem_sq(La,3,(*compare));//在顺序线性表L中查找第1个值与e满足compare()的元素的为序  
  168.     //cout<<"线性表中与3相等的元素的位序为:"<<e<<endl;  
  169.     cout<<"La与Lb合并后,线性表中Lc的所有元素为:"<<endl;  
  170.     MergeList_Sq(La,Lb,Lc);  
  171.     input(Lc);  
  172.     system("PAUSE");  
  173.     return;  
  174. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值