第三周项目3

来源:互联网 发布:excle显示重复的数据 编辑:程序博客网 时间:2024/05/17 08:44
/*烟台大学计算机学院  文件名称:main.cpp  作者:董玉祥 完成日期: 2017 9 24 问题描述:假设有两个集合 A 和 B 分别用两个线性表 LA 和 LB 表示,即线性表中的数据元素即为集合中的成员。设计算法,用函数unionList(List LA, List LB, List &LC )函数实现该算法,求一个新的集合C=A∪B,即将两个集合的并集放在线性表LC中 */
问题及代码:
#include <iostream>#include "list.h"using namespace std;void unionList(SqList *LA,SqList *LB,SqList *&LC){    int i;    int lena;    ElemType e;    InitList(LC);    for(i=1;i<=ListLength(LA);i++)    {        GetElem(LA,i,e);        ListInsert(LC,i,e);    }    lena=ListLength(LA);    for(i=1;i<=ListLength(LB);i++)    {        GetElem(LB,i,e);        if(!LocateElem(LA,e))           ListInsert(LC,++lena,e);    }}int main(){    SqList *s1,*s2,*s3;     ElemType  x[6]= {1,2,3,4,5,6};    ElemType y[6]= {7,8,9,10,11,12};    CreateList(s1,x,6);    CreateList(s2,y,6);    unionList(s1,s2,s3);    DispList(s3);    return 0;}
运行结果截图:


原创粉丝点击