第三周 求集合并集

来源:互联网 发布:淘宝开店要交钱吗 编辑:程序博客网 时间:2024/05/22 17:18
[cpp] view plain copy
print?
  1. /*   
  2. *Copyright (c) 2017,烟台大学计算机与控制工程学院   
  3. *All rights reserved.   
  4. *文件名称:项目3——求集合并集   
  5. *作    者:李浩南
  6. *版 本 号:v1.0   
  7. *  假设有两个集合 A 和 B 分别用两个线性表 LA 和 LB 表示,   
  8.     即线性表中的数据元素即为集合中的成员。设计算法,   
  9.     用函数unionList(List LA, List LB, List &LC )函数实现该算法,   
  10.     求一个新的集合C=A∪B,即将两个集合的并集放在线性表LC中。   
  11. */    

代码:

[cpp] view plain copy
print?
  1. #include<stdio.h>    
  2. #include<malloc.h>    
  3. #define MaxSize 50    
  4. typedef int ElemType;    
  5. typedef struct    
  6. {    
  7.     ElemType data[MaxSize];    
  8.    int length;    
  9. } SqList;    
  10. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表    
  11. void InitList(SqList *&L);//初始化线性表InitList(L)    
  12. void DestroyList(SqList *&L);//销毁线性表DestroyList(L)    
  13. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)    
  14. int ListLength(SqList *L);//求线性表的长度ListLength(L)    
  15. void DispList(SqList *L);//输出线性表DispList(L)    
  16. bool GetElem(SqList *L,int i,ElemType &e);//求某个数据元素值GetElem(L,i,e)    
  17. int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)    
  18. bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素ListInsert(L,i,e)    
  19. bool ListDelete(SqList *&L,int i,ElemType &e);//删除数据元素ListDelete(L,i,e)    
  20. void unionList(SqList *LA, SqList *LB, SqList *&LC);//合并    
  21. void CreateList(SqList *&L, ElemType a[], int n)    
  22. {    
  23.     int i;    
  24.     L=(SqList *)malloc(sizeof(SqList));    
  25.     for (i=0; i<n; i++)    
  26.        L->data[i]=a[i];    
  27.    L->length=n;    
  28. }    
  29.     
  30. //初始化线性表InitList(L)    
  31. void InitList(SqList *&L)   //引用型指针    
  32. {    
  33.     L=(SqList *)malloc(sizeof(SqList));  //分配存放线性表的空间    
  34.     L->length=0;    
  35. }    
  36.     
  37. //销毁线性表DestroyList(L)    
  38. void DestroyList(SqList *&L)    
  39. {    
  40.     free(L);    
  41. }    
  42.     
  43. //判定是否为空表ListEmpty(L)    
  44. bool ListEmpty(SqList *L)    
  45. {    
  46.     return(L->length==0);    
  47. }    
  48.     
  49. //求线性表的长度ListLength(L)    
  50. int ListLength(SqList *L)    
  51. {    
  52.     return(L->length);    
  53. }    
  54.     
  55. //输出线性表DispList(L)    
  56. void DispList(SqList *L)    
  57. {    
  58.     int i;    
  59.     if (ListEmpty(L)) return;    
  60.     for (i=0; i<L->length; i++)    
  61.         printf("%d ",L->data[i]);    
  62.     printf("\n");    
  63. }    
  64. bool GetElem(SqList *L,int i,ElemType &e)    
  65. {    
  66.     if (i<1 || i>L->length)  return false;    
  67.     e=L->data[i-1];    
  68.     return true;    
  69. }    
  70.     
  71.     
  72. bool ListInsert(SqList *&L,int i,ElemType e)    
  73. {    
  74.     int j;    
  75.     if (i<1 || i>L->length+1)    
  76.         return false;   //参数错误时返回false    
  77.     i--;            //将顺序表逻辑序号转化为物理序号    
  78.     for (j=L->length; j>i; j--) //将data[i..n]元素后移一个位置    
  79.         L->data[j]=L->data[j-1];    
  80.     L->data[i]=e;           //插入元素e    
  81.     L->length++;            //顺序表长度增1    
  82.     return true;            //成功插入返回true    
  83. }    
  84. int LocateElem(SqList *L, ElemType e)    
  85. {    
  86.    int i=0;    
  87.     while (i<L->length && L->data[i]!=e) i++;    
  88.     if (i>=L->length)  return 0;    
  89.     else  return i+1;    
  90. }    
  91. void unionList(SqList *LA, SqList *LB, SqList *&LC)    
  92. {    
  93.     int lena,i;    
  94.     ElemType e;    
  95.     InitList(LC);    
  96.     for (i=1; i<=ListLength(LA); i++) //将LA的所有元素插入到Lc中    
  97.     {    
  98.         GetElem(LA,i,e);    
  99.         ListInsert(LC,i,e);    
  100.     }    
  101.     lena=ListLength(LA);         //求线性表LA的长度    
  102.     for (i=1; i<=ListLength(LB); i++)    
  103.     {    
  104.         GetElem(LB,i,e);         //取LB中第i个数据元素赋给e    
  105.         if (!LocateElem(LA,e)) //LA中不存在和e相同者,插入到LC中    
  106.             ListInsert(LC,++lena,e);    
  107.     }    
  108. }    
  109. int main()    
  110. {    
  111.     SqList *sq_a, *sq_b, *sq_c;    
  112.     ElemType a[6]= {5,8,7,2,4,9};    
  113.     CreateList(sq_a, a, 6);    
  114.     printf("LA: ");    
  115.     DispList(sq_a);    
  116.     
  117.     ElemType b[6]= {2,3,8,6,0};    
  118.     CreateList(sq_b, b, 5);    
  119.     printf("LB: ");    
  120.     DispList(sq_b);    
  121.     unionList(sq_a, sq_b, sq_c);    
  122.     printf("LC: ");    
  123.     DispList(sq_c);    
  124.     return 0;    
  125. }    
运行结果:

知识点总结:

线性表的基本运算