第三周项目3 - 求集合并集

  1. /*烟台大学计算机学院  
  2.   
  3. 文件名称:main.cpp  
  4.   
  5. 作者:刘浩
  6.   
  7. 完成日期: 2017 10 26 
  8.   
  9. 问题描述:假设有两个集合 A 和 B 分别用两个线性表 LA 和 LB 表示,即线性表中的数据元素即为集合中的成员。设计算法,用函数unionList(List LA, List LB, List &LC )函数实现该算法,求一个新的集合C=A∪B,即将两个集合的并集放在线性表LC中  
  10. */  
[cpp]  view plain  copy
  1. 问题及代码:  
[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include "list.h"  
  3. using namespace std;  
  4. void unionList(SqList *LA,SqList *LB,SqList *&LC)  
  5. {  
  6.     int i;  
  7.     int lena;  
  8.     ElemType e;  
  9.     InitList(LC);  
  10.     for(i=1;i<=ListLength(LA);i++)  
  11.     {  
  12.         GetElem(LA,i,e);  
  13.         ListInsert(LC,i,e);  
  14.   
  15.     }  
  16.     lena=ListLength(LA);  
  17.     for(i=1;i<=ListLength(LB);i++)  
  18.     {  
  19.         GetElem(LB,i,e);  
  20.         if(!LocateElem(LA,e))  
  21.            ListInsert(LC,++lena,e);  
  22.     }  
  23.   
  24.   
  25. }  
  26. int main()  
  27.   
  28. {  
  29.     SqList *s1,*s2,*s3;  
  30.      ElemType  x[6]= {1,2,3,4,5,6};  
  31.     ElemType y[6]= {7,8,9,10,11,12};  
  32.   
  33.     CreateList(s1,x,6);  
  34.     CreateList(s2,y,6);  
  35.   
  36.   
  37.     unionList(s1,s2,s3);  
  38.   
  39.     DispList(s3);  
  40.     return 0;  
  41. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值