数据结构第三周项目--求集合并集

  1. /* 
  2. *Copyright(c)2017,烟台大学计算机与控制工程学院 
  3. *All rights reserved. 
  4. *文件名称:fd.cpp 
  5. *作    者:李庆耀
  6. *完成日期:2017年10月19日 
  7. *版 本 号:v1.0 
  8. *问题描述:假设有两个集合 A 和 B 分别用两个线性表 LA 和 LB 表示, 
  9.            即线性表中的数据元素即为集合中的成员。 
  10.            设计算法,用函数unionList(List LA, List LB, List &LC )函数实现该算法, 
  11.            求一个新的集合C=A∪B,即将两个集合的并集放在线性表LC中。 
  12. */  
运行代码:
[cpp]  view plain  copy
  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.   
  22.   
  23. //用数组创建线性表  
  24. void CreateList(SqList *&L, ElemType a[], int n)  
  25. {  
  26.     int i;  
  27.     L=(SqList *)malloc(sizeof(SqList));  
  28.     for (i=0; i<n; i++)  
  29.         L->data[i]=a[i];  
  30.     L->length=n;  
  31. }  
  32.   
  33. //初始化线性表InitList(L)  
  34. void InitList(SqList *&L)   //引用型指针  
  35. {  
  36.     L=(SqList *)malloc(sizeof(SqList));  
  37.     //分配存放线性表的空间  
  38.     L->length=0;  
  39. }  
  40.   
  41. //销毁线性表DestroyList(L)  
  42. void DestroyList(SqList *&L)  
  43. {  
  44.     free(L);  
  45. }  
  46.   
  47. //判定是否为空表ListEmpty(L)  
  48. bool ListEmpty(SqList *L)  
  49. {  
  50.     return(L->length==0);  
  51. }  
  52.   
  53. //求线性表的长度ListLength(L)  
  54. int ListLength(SqList *L)  
  55. {  
  56.     return(L->length);  
  57. }  
  58.   
  59. //输出线性表DispList(L)  
  60. void DispList(SqList *L)  
  61. {  
  62.     int i;  
  63.     if (ListEmpty(L)) return;  
  64.     for (i=0; i<L->length; i++)  
  65.         printf("%d ",L->data[i]);  
  66.     printf("\n");  
  67. }  
  68.   
  69. //求某个数据元素值GetElem(L,i,e)  
  70. bool GetElem(SqList *L,int i,ElemType &e)  
  71. {  
  72.     if (i<1 || i>L->length)  return false;  
  73.     e=L->data[i-1];  
  74.     return true;  
  75. }  
  76.   
  77. //按元素值查找LocateElem(L,e)  
  78. int LocateElem(SqList *L, ElemType e)  
  79. {  
  80.     int i=0;  
  81.     while (i<L->length && L->data[i]!=e) i++;  
  82.     if (i>=L->length)  return 0;  
  83.     else  return i+1;  
  84. }  
  85.   
  86. //插入数据元素ListInsert(L,i,e)  
  87. bool ListInsert(SqList *&L,int i,ElemType e)  
  88. {  
  89.     int j;  
  90.     if (i<1 || i>L->length+1)  
  91.         return false;   //参数错误时返回false  
  92.     i--;            //将顺序表逻辑序号转化为物理序号  
  93.     for (j=L->length; j>i; j--) //将data[i..n]元素后移一个位置  
  94.         L->data[j]=L->data[j-1];  
  95.     L->data[i]=e;           //插入元素e  
  96.     L->length++;            //顺序表长度增1  
  97.     return true;            //成功插入返回true  
  98. }  
  99.   
  100. //删除数据元素ListDelete(L,i,e)  
  101. bool ListDelete(SqList *&L,int i,ElemType &e)  
  102. {  
  103.     int j;  
  104.     if (i<1 || i>L->length)  //参数错误时返回false  
  105.         return false;  
  106.     i--;        //将顺序表逻辑序号转化为物理序号  
  107.     e=L->data[i];  
  108.     for (j=i; j<L->length-1; j++) //将data[i..n-1]元素前移  
  109.         L->data[j]=L->data[j+1];  
  110.     L->length--;              //顺序表长度减1  
  111.     return true;              //成功删除返回true  
  112. }  
  113.   
  114.   
  115. void unionList(SqList *LA, SqList *LB, SqList *&LC)  
  116. {  
  117.     int lena,i;  
  118.     ElemType e;  
  119.     InitList(LC);  
  120.     for (i=1; i<=ListLength(LA); i++) //将LA的所有元素插入到Lc中  
  121.     {  
  122.         GetElem(LA,i,e);  
  123.         ListInsert(LC,i,e);  
  124.     }  
  125.     lena=ListLength(LA);         //求线性表LA的长度  
  126.     for (i=1; i<=ListLength(LB); i++)  
  127.     {  
  128.         GetElem(LB,i,e);         //取LB中第i个数据元素赋给e  
  129.         if (!LocateElem(LA,e)) //LA中不存在和e相同者,插入到LC中  
  130.             ListInsert(LC,++lena,e);  
  131.     }  
  132. }  
  133.   
  134. //用main写测试代码  
  135. int main()  
  136. {  
  137.     SqList *sq_a, *sq_b, *sq_c;  
  138.     ElemType a[6]= {5,8,7,2,4,9};  
  139.     CreateList(sq_a, a, 6);  
  140.     printf("LA: ");  
  141.     DispList(sq_a);  
  142.   
  143.     ElemType b[6]= {2,3,8,6,0};  
  144.     CreateList(sq_b, b, 5);  
  145.     printf("LB: ");  
  146.     DispList(sq_b);  
  147.     unionList(sq_a, sq_b, sq_c);  
  148.     printf("LC: ");  
  149.     DispList(sq_c);  
  150.     return 0;  
  151. }  
运行结果:


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值