[C++] 数据结构与算法----1.3 两个表的元素严格递增排列,合并两表,并仍递增排列 →→→→→进入此内容的聊天室

来自 , 2020-12-19, 写在 C++, 查看 129 次.
URL http://www.code666.cn/view/a4856405
  1. //***********************************
  2. //功能:设两个顺序表的元素严格递增排列,把两个表的共同元素保存到新的顺序表中
  3. //      要求新表的元素仍递增排列,对新表另分配存储空间
  4. //日期:2017年9月13日
  5. //作者:Ryan2019
  6. //***********************************
  7. #include <iostream>
  8. using namespace std;
  9. const int ListlnitSize=0;
  10. const int Listlnc=10;
  11. typedef int LElemType;
  12. struct SList
  13. {
  14.     LElemType *elem;
  15.         int length,listsize;
  16. };
  17. bool Listlnit(SList &L);//顺序表初始化
  18. bool ListCreate(SList &L,int n,LElemType a[]);//创建顺序表
  19. bool MoveOrder(SList &A,SList &B,SList &C);//新表
  20. int main()
  21. {
  22.         const int m=5,n=5;
  23.     LElemType a[m]={1,2,3,4,5};
  24.     LElemType b[n]={1,3,5,7,9};
  25.     SList A,B,C;
  26.         ListCreate(A,m,a);
  27.         ListCreate(B,n,b);
  28.         cout<<"线性表A为"<<endl;
  29.         for(int j=0;j<m;j++){cout<<A.elem[j]<<"  ";}
  30.         cout<<endl<<"线性表B为"<<endl;
  31.         for(int i=0;i<n;i++){cout<<B.elem[i]<<"  ";}
  32.        
  33.         MoveOrder(A,B,C);
  34.     cout<<endl<<"重新排列后的顺序表C为"<<endl;
  35.         for(int k=0;k<C.length;k++){cout<<C.elem[k]<<"  ";}
  36.         cout<<endl;
  37.         return 0;
  38. }
  39. bool Listlnit(SList &L)
  40. {
  41.         L.elem=new LElemType[ListlnitSize];
  42.         if(!L.elem)             return false;  
  43.         L.length=0;
  44.     L.listsize=ListlnitSize;
  45.         return true;
  46. }
  47. bool ListCreate(SList &L,int n,LElemType a[])
  48. {
  49.         int i;
  50.         L.elem=new LElemType[n+ListlnitSize];
  51.         if(!L.elem)                     return false;  
  52.         L.length=n;
  53.     L.listsize=n+ListlnitSize;
  54.         for(i=0;i<n;i++)
  55.         {
  56.                 L.elem[i]=a[i];
  57.         }
  58.         return true;
  59. }
  60. bool MoveOrder(SList &A,SList &B,SList &C)
  61. {
  62.         int m=0,q=0,p=0;
  63.            if (A.length<=B.length)
  64.         {
  65.                 C.elem=new LElemType[A.length+ListlnitSize];
  66.                 C.listsize=A.length+ListlnitSize;
  67.         }
  68.         else
  69.         {
  70.                 C.elem=new LElemType[B.length+ListlnitSize];
  71.                 C.listsize=B.length+ListlnitSize;
  72.         }
  73.         C.length=0;
  74.         for(int i=0;i<A.length || i<B.length;i++)
  75.         {              
  76.                 if(A.elem[p]==B.elem[q])
  77.                 {
  78.                         C.length++;
  79.                         C.elem[m]=A.elem[p];
  80.                         m++;
  81.                         p++;
  82.                         q++;
  83.                 }
  84.                 else if(A.elem[p]<B.elem[q])
  85.                 {
  86.                         p++;
  87.                 }
  88.                 else
  89.                 {
  90.                         q++;
  91.                 }
  92.         }
  93.         return true;
  94. }

回复 "数据结构与算法----1.3 两个表的元素严格递增排列,合并两表,并仍递增排列"

这儿你可以回复上面这条便签

captcha