数据结构上机实践第三周项目3- 求集合并集

来源:互联网 发布:心理测评软件下载 编辑:程序博客网 时间:2024/05/17 09:22

【项目 - 求集合并集】 

  假设有两个集合 A 和 B 分别用两个线性表 LA 和 LB 表示,即线性表中的数据元素即为集合中的成员。设计算法,用函数unionList(List LA, List LB, List &LC )函数实现该算法,求一个新的集合C=A∪B,即将两个集合的并集放在线性表LC中。

       首先还是要建立一个工程,并将之前建立好的算法库作为头文件包进去。文件视角如下:


     list.cpp源代码如下:

[cpp] view plain copy
  1. <span style="font-family:Arial, Helvetica, sans-serif;">#include <stdio.h></span>  

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

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

   
[cpp] view plain copy
  1.   
[cpp] view plain copy
  1. #ifndef LIST_H_INCLUDED  
  2. #define LIST_H_INCLUDED  
  3.   
  4. #define MaxSize 50  
  5. typedef int ElemType;  
  6. typedef struct  
  7. {  
  8.     ElemType data[MaxSize];  
  9.     int length;  
  10. } SqList;  
  11. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表  
  12. void InitList(SqList *&L);//初始化线性表InitList(L)  
  13. void DestroyList(SqList *&L);//销毁线性表DestroyList(L)  
  14. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)  
  15. int ListLength(SqList *L);//求线性表的长度ListLength(L)  
  16. void DispList(SqList *L);//输出线性表DispList(L)  
  17. bool GetElem(SqList *L,int i,ElemType &e);//求某个数据元素值GetElem(L,i,e)  
  18. int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)  
  19. bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素ListInsert(L,i,e)  
  20. bool ListDelete(SqList *&L,int i,ElemType &e);//删除数据元素ListDelete(L,i,e)#endif // LIST_H_INCLUDED  
  21. void unionList(SqList *, SqList *, SqList *&);  
  22. #endif  
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 不锈钢补料槽 指纹打卡机怎么补打卡记录 手摇补鞋机 汽车充气补胎一体机 充气补胎一体机 补胎机 补胎充气一体机 补鞋机 补鞋机多少钱一台 正畸补牙 补牙正畸 补正裁定适用范围 商标续展申请补正 商标注册申请补正 吃什么补气虚 怎样补气虚 补血补气泡水喝的方子 补气血茶 补气血鸡汤 补气 三红补气养血汤 补气养血汤 补气补血 补气食物 补血补气 补气养血粥 补气的食物 补气中药 补肺气第一要药 补气养血 补血补气猪心汤 补气补血吃什么 补气补血吃什么好 补气的中药 补气药 什么补气 女人补血补气 吃什么补气 补气养神动物 补气益血的食物 补气的食物有哪些