第三周 项目3-求集合并集
2015-09-19 16:12
183 查看
/*
*Copyright(c) 2015, 烟台大学计算机学院
*All rights reserved.
*文件名称:求集合并集.cpp
*作 者:周洁
*完成日期:2015年 9月18日
*版 本 号:
*
*问题描述:假设有两个集合 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++;
}
}
运行结果:
![](https://img-blog.csdn.net/20150919161216609?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
*Copyright(c) 2015, 烟台大学计算机学院
*All rights reserved.
*文件名称:求集合并集.cpp
*作 者:周洁
*完成日期:2015年 9月18日
*版 本 号:
*
*问题描述:假设有两个集合 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++;
}
}
运行结果:
相关文章推荐
- NYIST OJ 1000 又见Fibonacci数列
- LeetCode -- Binary Tree Right Side View
- web.xml配置详解 部署描述符文件
- 面向对象的设计原则
- LeetCode -- Best Time to Buy and Sell Stock III
- jquery中的cookie操作
- js立即执行函数: (function ( ){...})( ) 与 (function ( ){...}( ))
- test5.5
- JavaScript 多种方法输出数组的元素(含递归)
- HttpClient结合Structs2进行文件上传(Action返回字符串)
- 数字摘要
- LeetCode -- Binary Tree Inorder Traversal
- 中值滤波 Qt代码 内含OpenCV类
- 调用deleteRowsAtIndexPaths没有刷新表格怎么办?
- 现实与想象的差距
- 【Python爬虫实战--1】深入理解urllib;urllib2;requests
- [2015-09-19 15:07:13 - LinearLayoutActivity] Unable to resolve target 'android-14'
- POJ 1190 生日蛋糕
- 安卓模拟器错误: Could not open
- POJ 1258 Agri-Net