项目3 求集合并集

来源:互联网 发布:51单片机控制机箱风扇 编辑:程序博客网 时间:2024/06/06 04:17
  1. /*     
  2.  * Copyright (c++) 2017 烟台大学计算机学院     
  3.  * All right reserved.     
  4.  * 文件名称:creat2.cpp     
  5.  * 作    者: 张延飞    
  6.  * 完成日期:2017年9月17日     
  7.  * 版 本 号:v1.9      
  8.  *     
  9.  *问题描述:假设有两个集合 A 和 B 分别用两个线性表 LA 和 LB 表示,即线性表中的数据元素即为  
  10.           集合中的成员。设计算法,用函数unionList(List LA, List LB, List &LC )函数实现该  
  11.           算法,求一个新的集合C=A∪B,即将两个集合的并集放在线性表LC中。    
  12.  *输入描述:各个集合    
  13.  *程序输出:合并后的集合   
  14. */      


main.cpp

[cpp] view plain copy
  1. #include "h.h"  
  2.   
  3. int main()  
  4. {  
  5.     SqList *sq_a, *sq_b, *sq_c;  
  6.     ElemType a[6]= {5,8,7,2,4,9};  
  7.     CreateList(sq_a, a, 6);  
  8.     printf("LA: ");  
  9.     DispList(sq_a);  
  10.     ElemType b[6]= {2,3,8,6,0};  
  11.     CreateList(sq_b, b, 5);  
  12.     printf("LB: ");  
  13.     DispList(sq_b);  
  14.     unionList(sq_a, sq_b, sq_c);  
  15.     printf("LC: ");  
  16.     DispList(sq_c);  
  17.     return 0;  
  18. }  


s.cpp

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


h.h

[cpp] view plain copy
  1. #ifndef LIST_H_INCLUDED  
  2. #define LIST_H_INCLUDED  
  3. #include<stdio.h>  
  4. #include<malloc.h>  
  5. #include<stdio.h>  
  6. #define MaxSize 50  
  7.   
  8. typedef int ElemType;  
  9. typedef struct  
  10. {  
  11.     ElemType data[MaxSize];  
  12.     int length;  
  13. } SqList;  
  14. void unionList(SqList *LA, SqList *LB, SqList *&LC);  
  15. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表  
  16. void InitList(SqList *&L);//初始化线性表InitList(L)  
  17. void DestroyList(SqList *&L);//销毁线性表DestroyList(L)  
  18. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)  
  19. int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)  
  20. int ListLength(SqList *L);//求线性表的长度ListLength(L)  
  21. void DispList(SqList *L);//输出线性表DispList(L)  
  22. bool GetElem(SqList *L,int i,ElemType &e);//求某个数据元素值GetElem(L,i,e)  
  23. int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)  
  24. bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素ListInsert(L,i,e)  
  25. bool ListDelete(SqList *&L,int i,ElemType &e);//删除数据元素ListDelete(L,i,e)#endif // LIST_H_INCLUDED  
  26.   
  27.   
  28. #endif  
运行结果:

知识点总结:

利用顺序表得基本运算以及顺序表中不包含相同元素这两个知识点完成

学习心得:

一开始我用程序的各个文件放在一个工程中,但最后老是出现错误,就直接用一个程序完成了,也是利用上一个项目总结的顺序表方便了好多,以后的学习路上还是要多实践,多多总结!!