第三周项目3

本文介绍了一种基于线性表的数据结构实现两个线性表的并集操作的方法。通过遍历两个线性表并将非重复元素插入新的线性表中来完成并集运算。
  1. /* 
  2. *Copyright (c) 2017,烟台大学计算机与控制工程学院 
  3. *All rights reserved. 
  4. *文件名称:lsy 
  5. *作    者:刘思源 
  6. *完成日期:2017年9月16日 
  7. *版 本 号:v1.0 
  8. * 
  9. *问题描述:
  10. *输入描述:
  11. *程序输出: 
  12. */  
  13. #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;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值