第三周项目3 求集合并集
2015-09-25 08:17
218 查看
问题与代码
//list.h
//main.cpp
//zdy.cpp
//union.cpp
运行结果
知识点总结
通过此次实践,认识到很重要的一点是,我们是可以借助以前所编写的程序的,我们是可以在原有程序的基础上,通过添加一些文件和定义来实现当前的既定目标的。
//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); } }
运行结果
知识点总结
通过此次实践,认识到很重要的一点是,我们是可以借助以前所编写的程序的,我们是可以在原有程序的基础上,通过添加一些文件和定义来实现当前的既定目标的。
相关文章推荐
- Hive 操作数据库语句总结
- 第3周 项目3-求集合并集
- [leetcode-279]Perfect Squares(java)
- easyui 如何使个别onExpandRow的+号隐藏
- 项目5 -- 猴子选大王
- Hive 创建数据库异常
- 面对对象思想4
- Routing Manager for WCF4 z
- 从命令行输入数字相加
- 第3周项目2 建设“顺序表”算法库
- 大数据系列一--引文
- 第三周项目2—建设“顺序表”算法库
- 15-EMM Procedure 5. Periodic TAU
- ubuntu14.04通过将语音,耳机没有声音
- 简单的Poi导出Excel(.xls)
- Go语法简略 - 依赖注入
- 数据包
- ITOO4.0项目总结--成长
- View Controller 流(纯代码版)
- 项目经验分享