第三周项目3 求集合并集

问题与代码

//list.h

/*
 *Copyright (c) 2015,烟台大学计算机与控制工程学院
 *All rights reserved.
 *文件名称:list.h,main.cpp,zdy.cpp,union.cpp
 *作者:陈梦萍
 *完成日期:2015年9月25日
 *版本号:v1.0
 *
 *问题描述: 假设有两个集合 A 和 B 分别用两个线性表 LA 和 LB 表示,
            即线性表中的数据元素即为集合中的成员。设计算法,用函
            数unionList(List LA, List LB, List &LC )函数实现该算
            法,求一个新的集合C=A∪B,即将两个集合的并集放在线性
            表LC中。
 *输入描述:无
 *程序输出:若干数据
*/

#include<iostream>
#include<stdio.h>
#include<malloc.h>

#define MaxSize 50

typedef int ElemType;
typedef struct
{
    ElemType data[MaxSize];
    int length;
} SqList;

void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表
void InitList(SqList *&L);//初始化线性表
void DestroyList(SqList *&L);//销毁线性表
bool ListEmpty(SqList *L);//判定是否为空表
int ListLength(SqList *L);//求线性表的长度
void DispList(SqList *L);//输出线性表
bool GetElem(SqList *L,int i,ElemType &e);//求某个数据元素值
int LocateElem(SqList *L, ElemType e);//按元素值查找
bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素
bool ListDelete(SqList *&L,int i,ElemType &e);//删除数据元素
void unionList(SqList *LA, SqList *LB, SqList *&LC);//求LA与LB的集合LC


//main.cpp

#include "list.h"

int main()
{
    SqList *sq1,*sq2,*sq3;
    ElemType x[6]= {5,8,7,2,4,9};
    ElemType y[6]= {1,7,3,12,5,6};
    CreateList(sq1, x, 6);
    DispList(sq1);
    CreateList(sq2, y, 6);
    DispList(sq2);
    unionList(sq1,sq2,sq3);
    DispList(sq3);
    return 0;
}


//zdy.cpp

#include "list.h"

void CreateList(SqList *&L, ElemType a[], int n)
{
    int i;
    L=(SqList *)malloc(sizeof(SqList));
    for (i=0; i<n; i++)
        L->data[i]=a[i];
    L->length=n;
}

void InitList(SqList *&L)
{
    L=(SqList *)malloc(sizeof(SqList));
    L->length=0;
}

void DestroyList(SqList *&L)
{
    free(L);
}

bool ListEmpty(SqList *L)
{
    return(L->length==0);
}

int ListLength(SqList *L)
{
    return(L->length);
}

void DispList(SqList *L)
{
    int i;
    if (ListEmpty(L)) return;
    for (i=0; i<L->length; i++)
        printf("%d ",L->data[i]);
    printf("\n");
}

bool GetElem(SqList *L,int i,ElemType &e)
{
    if (i<1 || i>L->length)  return false;
    e=L->data[i-1];
    return true;
}

int LocateElem(SqList *L, ElemType e)
{
    int i=0;
    while (i<L->length && L->data[i]!=e) i++;
    if (i>=L->length)  return 0;
    else  return i+1;
}

bool ListInsert(SqList *&L,int i,ElemType e)
{
    int j;
    if (i<1 || i>L->length+1)
        return false;
    i--;
    for (j=L->length; j>i; j--)
        L->data[j]=L->data[j-1];
    L->data[i]=e;
    L->length++;
    return true;
}

bool ListDelete(SqList *&L,int i,ElemType &e)
{
    int j;
    if (i<1 || i>L->length)
        return false;
    i--;
    e=L->data[i];
    for (j=i; j<L->length-1; j++)
        L->data[j]=L->data[j+1];
    L->length--;
    return true;
}




//union.cpp

#include "list.h"

void unionList(SqList *LA, SqList *LB, SqList *&LC)
{
    int lena,i;
    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);
    }
}

运行结果

知识点总结

       通过此次实践,认识到很重要的一点是,我们是可以借助以前所编写的程序的,我们是可以在原有程序的基础上,通过添加一些文件和定义来实现当前的既定目标的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值