第三周项目三求集合并集
2015-09-21 17:03
357 查看
/*
Copyright (c)2015,烟台大学计算机与控制工程学院
All rights reserved.
文件名称:项目3--求集合并集.cpp
作 者:李丛丛
完成日期:2015年9月18日
版 本 号:v1.0
问题描述:假设有两个集合 A 和 B 分别用两个线性表 LA 和 LB 表示,
即线性表中的数据元素即为集合中的成员。
设计算法,用函数unionList(List LA, List LB, List &LC )函数实现该算法,
求一个新的集合C=A∪B,即将两个集合的并集放在线性表LC中。
输入描述: 若干数据 。
程序输出: LA的集合数据、LB的集合数据、LC的集合数据。
*/
运行结果为:
Copyright (c)2015,烟台大学计算机与控制工程学院
All rights reserved.
文件名称:项目3--求集合并集.cpp
作 者:李丛丛
完成日期:2015年9月18日
版 本 号:v1.0
问题描述:假设有两个集合 A 和 B 分别用两个线性表 LA 和 LB 表示,
即线性表中的数据元素即为集合中的成员。
设计算法,用函数unionList(List LA, List LB, List &LC )函数实现该算法,
求一个新的集合C=A∪B,即将两个集合的并集放在线性表LC中。
输入描述: 若干数据 。
程序输出: LA的集合数据、LB的集合数据、LC的集合数据。
*/
#include<stdio.h> #include<malloc.h> typedef struct { int data[50]; int length; }sqlist; void creatlist(sqlist *&,int a[],int ); void DispList(sqlist *); void unionlist(sqlist *,sqlist *,sqlist *&); int main() { sqlist *sqa,*sqb,*sqc; int a[6]= {5,8,7,2,4,9}; creatlist(sqa,a,6); printf("LA:"); DispList(sqa); int b[6]= {2,3,8,6,0}; creatlist(sqb,b,6); printf("LB:"); DispList(sqb); unionlist(sqa,sqb,sqc); printf("LC:"); DispList(sqc); } void creatlist(sqlist *&l,int 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 DispList(sqlist *l) { int i; for(i=0;i<l->length;i++) { printf("%d ",l->data[i]); } printf("\n"); } void unionlist(sqlist *a,sqlist *b,sqlist *&c) { c=(sqlist *)malloc(sizeof(sqlist)); int i,j=0,k=0,l=0; c->length=0; for(i=0;i<a->length;i++) { c->data[i]=a->data[i]; c->length++; } while(j<b->length) { while(b->data[j]!=a->data[k]&&k<b->length) { k++; } if(k==b->length) { c->data[i]=b->data[j]; i++; c->length++; } k=0; j++; } }
运行结果为:
相关文章推荐
- 第四周上机实践项目2——建立“单链表”算法库
- Myeclipse 10 Maven 构建 Java Web 项目
- 使用OpenSSL生成证书
- poj2385+动态规划有点还 不能完全理解
- CentOS 6.5上使用gdb调试时出现Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.i686 .
- perl--匿名数据
- iOS开发-处理字符串中的双引号
- android NotificationCompat.Builder 使用
- No matching authentication protocol错误
- SQL Server数据库优化的10多种方法
- arpspoof dnsspoof中间人攻击
- 对话框显示图像的简单方法
- ios9升级后导致的多语言问题
- 15.Android 异步更新UI 技巧
- Spring的AOP中before,afterReturning,afterThrowing参数说明
- block_dump观察Linux IO写入的具体文件
- Singleton
- xib自定义cell,代码修改cell里控件frame加载后不生效,下一次reloaddata改变?
- memcache CAS实现原子操作
- 数据库SQL优化大总结之 百万级数据库优化方案