第三周项目3-求集合并集
来源:互联网 发布:网络团队建设 编辑:程序博客网 时间:2024/06/04 18:03
问题及代码:
01./* 02.*烟台大学计算计控制与工程学院 03.*作 者:房斐 04.*完成日期:2016年9月17日 05.*问题描述:假设有两个集合 A 和 B 分别用两个线性表 LA 和 LB 表示,即线性表中的数据元素即为集合中的成员。设计算法,用函数unionList(List LA, List LB, List &LC )函数实现该算法,求一个新的集合C=A∪B,即将两个集合的并集放在线性表LC中。 06.提示: 07.(1)除了实现unnionList函数外,还需要在main函数中设计代码,调用unionList进行测试和演示; 08.(2)可以充分利用前面建好的算法库[点击…],在程序头部直接加 #include<list.h>即可(工程中最普遍的方法,建议采纳); 09.(3)也可以将实现算法中需要的线性表的基本运算对应的函数,与自己设计的所有程序放在同一个文件中。 10.*/
#include<stdio.h> 02.#include<malloc.h> 03.#define MaxSize 50 04.typedef int ElemType; 05.typedef struct 06.{ 07. ElemType data[MaxSize]; 08. int length; 09.} SqList; 10.void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表 11.void InitList(SqList *&L);//初始化线性表InitList(L) 12.void DestroyList(SqList *&L);//销毁线性表DestroyList(L) 13.bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L) 14.int ListLength(SqList *L);//求线性表的长度ListLength(L) 15.void DispList(SqList *L);//输出线性表DispList(L) 16.bool GetElem(SqList *L,int i,ElemType &e);//求某个数据元素值GetElem(L,i,e) 17.int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e) 18.bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素ListInsert(L,i,e) 19.bool ListDelete(SqList *&L,int i,ElemType &e);//删除数据元素ListDelete(L,i,e) 20.void unionList(SqList *LA, SqList *LB, SqList *&LC);//合并 21.void CreateList(SqList *&L, ElemType a[], int n) 22.{ 23. int i; 24. L=(SqList *)malloc(sizeof(SqList)); 25. for (i=0; i<n; i++) 26. L->data[i]=a[i]; 27. L->length=n; 28.} 29. 30.//初始化线性表InitList(L) 31.void InitList(SqList *&L) //引用型指针 32.{ 33. L=(SqList *)malloc(sizeof(SqList)); //分配存放线性表的空间 34. L->length=0; 35.} 36. 37.//销毁线性表DestroyList(L) 38.void DestroyList(SqList *&L) 39.{ 40. free(L); 41.} 42. 43.//判定是否为空表ListEmpty(L) 44.bool ListEmpty(SqList *L) 45.{ 46. return(L->length==0); 47.} 48. 49.//求线性表的长度ListLength(L) 50.int ListLength(SqList *L) 51.{ 52. return(L->length); 53.} 54. 55.//输出线性表DispList(L) 56.void DispList(SqList *L) 57.{ 58. int i; 59. if (ListEmpty(L)) return; 60. for (i=0; i<L->length; i++) 61. printf("%d ",L->data[i]); 62. printf("\n"); 63.} 64.bool GetElem(SqList *L,int i,ElemType &e) 65.{ 66. if (i<1 || i>L->length) return false; 67. e=L->data[i-1]; 68. return true; 69.} 70. 71. 72.bool ListInsert(SqList *&L,int i,ElemType e) 73.{ 74. int j; 75. if (i<1 || i>L->length+1) 76. return false; //参数错误时返回false 77. i--; //将顺序表逻辑序号转化为物理序号 78. for (j=L->length; j>i; j--) //将data[i..n]元素后移一个位置 79. L->data[j]=L->data[j-1]; 80. L->data[i]=e; //插入元素e 81. L->length++; //顺序表长度增1 82. return true; //成功插入返回true 83.} 84.int LocateElem(SqList *L, ElemType e) 85.{ 86. int i=0; 87. while (i<L->length && L->data[i]!=e) i++; 88. if (i>=L->length) return 0; 89. else return i+1; 90.} 91.void unionList(SqList *LA, SqList *LB, SqList *&LC) 92.{ 93. int lena,i; 94. ElemType e; 95. InitList(LC); 96. for (i=1; i<=ListLength(LA); i++) //将LA的所有元素插入到Lc中 97. { 98. GetElem(LA,i,e); 99. ListInsert(LC,i,e); 100. } 101. lena=ListLength(LA); //求线性表LA的长度 102. for (i=1; i<=ListLength(LB); i++) 103. { 104. GetElem(LB,i,e); //取LB中第i个数据元素赋给e 105. if (!LocateElem(LA,e)) //LA中不存在和e相同者,插入到LC中 106. ListInsert(LC,++lena,e); 107. } 108.} 109.int main() 110.{ 111. SqList *sq_a, *sq_b, *sq_c; 112. ElemType a[6]= {5,8,7,2,4,9}; 113. CreateList(sq_a, a, 6); 114. printf("LA: "); 115. DispList(sq_a); 116. 117. ElemType b[6]= {2,3,8,6,0}; 118. CreateList(sq_b, b, 5); 119. printf("LB: "); 120. DispList(sq_b); 121. unionList(sq_a, sq_b, sq_c); 122. printf("LC: "); 123. DispList(sq_c); 124. return 0; 125.}
运行结果:
知识点总结:
利用顺序表的基本运算以及顺序表中不包含相同元素这两个知识点完成。
心得体会:
还要继续掌握此处知识点。
0 0
- 第三周--【项目 - 求集合并集】
- 第三周项目--求集合并集
- 第三周—项目3 求集合并集
- 第三周项目3求集合并集
- 第三周-项目3-求两集合并集
- (第三周项目3)求集合并集
- 第三周项目3—求集合并集
- 第三周 项目3-求集合并集
- 第三周项目3—求集合并集
- 第三周 项目3-求集合并集
- 第三周项目3 求集合并集
- 第三周—项目3 - 求集合并集
- 第三周 项目3 - 求集合并集
- 第三周项目3-求集合并集
- 第三周项目3-求集合并集
- 第三周项目3-求集合并集
- 第三周项目3-求集合并集
- 第三周实践项目3--求集合并集
- 简单日历的制作
- Unity客户端架构-PanelManager
- 基础Dialog对话框
- php面向对象-构造方法
- install win7-64 and fedora64-24 on hp540 successful
- 第三周项目3-求集合并集
- ncurses--读书笔记2
- Git多人协作功能测试
- 易经64卦记忆
- 九度1088
- hdu2022 海选女主角
- open GL 、DirectX、open CV、 open Inventor 、cocos2dx、unity3d、3dmax辨析
- MQTT,MQTT-SN的重要文章
- 归并排序与分治法求逆序对数