第三周——项目三—求集合并集

问题及代码:

/*  

*Copyright  (c)2017,烟台大学计算机与控制工程学院      

*All rights reservrd.      

*文件名称 :test.cpp      

*作者:潘亚楠  

*完成时间:2017年9月15日      

*版本号:v1.0      

*问题描述:将两个集合的并集放在线性表LC 中

#include "list.h"
#include <stdio.h>

void unionList(SqList *LA, SqList *LB, SqList *&LC)
{
    int lena,i;
    ElemType e;
    InitList(LC);
    for (i=1; i<=ListLength(LA); i++) //将LA的所有元素插入到Lc中
    {
        GetElem(LA,i,e);
        ListInsert(LC,i,e);
    }
    lena=ListLength(LA);         //求线性表LA的长度
    for (i=1; i<=ListLength(LB); i++)
    {
        GetElem(LB,i,e);         //取LB中第i个数据元素赋给e
        if (!LocateElem(LA,e)) //LA中不存在和e相同者,插入到LC中
            ListInsert(LC,++lena,e);
    }
}

//用main写测试代码
int main()
{
    SqList *sq_a, *sq_b, *sq_c;
    ElemType a[6]= {5,8,7,2,4,9};
    CreateList(sq_a, a, 6);
    printf("LA: ");
    DispList(sq_a);

    ElemType b[6]= {2,3,8,6,0};
    CreateList(sq_b, b, 5);
    printf("LB: ");
    DispList(sq_b);
    unionList(sq_a, sq_b, sq_c);
    printf("LC: ");
    DispList(sq_c);
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值