[C++] 数据结构与算法----1.5 把两个递增的顺序表归并为递减的顺序表 →→→→→进入此内容的聊天室

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

回复 "数据结构与算法----1.5 把两个递增的顺序表归并为递减的顺序表"

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

captcha