【第三周项目3-求集合并集】

来源:互联网 发布:南昌软件测试招聘 编辑:程序博客网 时间:2024/06/05 22:03

问题及描述:

/* * Copyright (c) 2016, 烟台大学计算机与控制工程学院 * All rights reserved. * 文件名称:Cube007.cpp * 作    者:刘小楠 * 完成日期:2016年9月18日 * * 问题描述:假设有两个集合A和B分别用两个线性表LA和LB表示,即线性表中的数据元素即为集合中的成员。设计算法,用函数unionList(ListLA,ListLB,List&LC)函数实现该算法,求一个新的集合C=A∪B,即将两个集合的并集放在线性表LC中 * 输入描述:无 * 输出描述:结果 */

list.h

#ifndef LIST_H_INCLUDED#define LIST_H_INCLUDED#define Maxsize 100typedef int Elemtype;          //自定义数据类型typedef struct list{    Elemtype data[Maxsize];    //存顺序表元素    int length;                //存顺序表长度} Sqlist;void CreateList(Sqlist *&l,Elemtype a[],int n);    //由a中的n个元素建立顺序表void DispList(Sqlist *l);                          //输出线性表int ListLength(Sqlist *l);                         //求顺序表长度bool GetElem(Sqlist *l,int i,Elemtype &e);         //求顺序表中某个数据元素值bool ListInsert(Sqlist *&l,int i,Elemtype e);      //插入数据元素void InitList(Sqlist *&l);                         //初始化线性表void unionList(Sqlist *LA,Sqlist *LB,Sqlist *&LC); //求两集合并集#endif // LIST_H_INCLUDED


lxn.cpp

#include <stdio.h>#include <malloc.h>#include "list.h"void CreateList(Sqlist *&l,Elemtype a[],int n)    //由a中的n个元素建立顺序表{    int i;    l=(Sqlist *)malloc(sizeof(Sqlist));           //分配存储空间    for(i=0;i<n;i++)        scanf("%d",&a[i]);    for(i=0;i<n;i++)        l->data[i]=a[i];                          //存放元素    l->length=n;                                  //设置长度}void DispList(Sqlist *l)                          //输出线性表{    int i;    for(i=0;i<l->length;i++)        printf("%d ",l->data[i]);    printf("\n");}int ListLength(Sqlist *l)                        //求顺序表长度{    return (l->length);}bool GetElem(Sqlist *l,int i,Elemtype &e)        //求顺序表中某个数据元素值{    if(i<1 || i>l->length)                       //i为逻辑序号,注意参数错误的情况        return false;                            //参数错误返回false    e=l->data[i-1];    return true;                                 //找到指定下标元素返回true}int LocateElem(Sqlist *l,Elemtype e)             //按元素值查找顺序表中元素{    int i=0;    while(i<l->length && l->data[i]!=e)          //e不是要查找的元素,继续往下遍历        i++;    if(i>=l->length)        return 0;                                //遍历一遍未找到返回false    else        return i+1;                              //否则找到返回true}bool ListInsert(Sqlist *&l,int i,Elemtype e)     //插入数据元素{    int j;    if(i<1 || i>l->length+1 || l->length>=Maxsize)//i为逻辑序号,注意参数错误的情况        return false;                             //参数错误返回false    i--;                                          //顺序表逻辑序号转化为物理序号    for(j=l->length-1;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+1)                      //i为逻辑序号,注意参数错误的情况        return false;                             //参数错误返回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;}void InitList(Sqlist *&l)                        //初始化线性表{    l=(Sqlist *)malloc(sizeof(Sqlist));          //分配存储空间    l->length=0;                                 //置空线性表,其长度为0}void DestroyList(Sqlist *&l)                     //销毁顺序表{    free(l);}void unionList(Sqlist *LA,Sqlist *LB,Sqlist *&LC)             //求两集合并集{    int i,LAlength;    Elemtype e;    InitList(LC);    LAlength=ListLength(LA);    for (i=1; i<=ListLength(LA); i++)                         //将LA的所有元素插入到LC中    {        GetElem(LA,i,e);        ListInsert(LC,i,e);    }    LAlength=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,++LAlength,e);    }}

main.cpp

#include <malloc.h>#include <stdio.h>#include "list.h"int main(){    Sqlist *LA,*LB,*LC;    Elemtype a[Maxsize];    int LAlength,LBlength;    printf("请输入线性表LA的长度:\n");    scanf("%d",&LAlength);    printf("请输入线性表LA中的元素:\n");    CreateList(LA,a,LAlength);    DispList(LA);    printf("请输入线性表LB的长度:\n");    scanf("%d",&LBlength);    printf("请输入线性表LA中的元素:\n");    CreateList(LB,a,LBlength);    DispList(LB);    unionList(LA,LB,LC);                       //求两集合并集    printf("线性表LC中的元素为:\n");    DispList(LC);    return 0;}

程序截图:

知识点总结:

项目3锻炼了我们对算法库的应用能力。


学习心得:

要学会回顾之前的问题,不能一味往前冲。


0 0
原创粉丝点击