第三周项目3-求集合并集(1)-利用算法库实现

来源:互联网 发布:手机上淘宝怎么付款 编辑:程序博客网 时间:2024/04/29 23:31

问题及代码

/*  *Copyright (c) 2015,烟台大学计算机学院  *All rights reserved.  *文件名称:test.cpp  *作者:王敏 *完成日期:2015年09月16日  *版本号:v1.0  *  *问题描述:求集合并集*输入描述:  *程序输出:  集合A,集合B,A和B的并集C*/   #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