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

来源:互联网 发布:tv域名不能备案 编辑:程序博客网 时间:2024/06/05 03:29
  1. 问题描述及代码:  
  2. [cpp] view plain copy  
  3. 1.  /*       
  4. 2.  *烟台大学计控学院        
  5. 3.  *作    者:陈晓琳     
  6. 4.  *完成日期:2016年9月20日    
  7. 5.  *问题描述:假设有两个集合 A 和 B 分别用两个线性表 LA 和 LB 表示,即线性表中的数据元素即为集合中的成员。设计算法,用函数unionList(List LA, List LB, List &LC )函数实现该算法,求一个新的集合C=A∪B,即将两个集合的并集放在线性表LC中。   
  8. 6.           提示:    
  9. 7.           (1)除了实现unnionList函数外,还需要在main函数中设计代码,调用unionList进行测试和演示;    
  10. 8.           (2)可以充分利用前面建好的算法库[点击…],在程序头部直接加 #include<list.h>即可(工程中最普遍的方法,建议采纳);    
  11. 9.           (3)也可以将实现算法中需要的线性表的基本运算对应的函数,与自己设计的所有程序放在同一个文件中。   
  12. 10. */    
  13.    
  14.   
  15. 代码:  
  16. [cpp] view plain copy  
  17. 1.  #include<stdio.h>       
  18. 2.  #include<malloc.h>       
  19. 3.  #define MaxSize 50       
  20. 4.  typedef int ElemType;      
  21. 5.  typedef struct      
  22. 6.  {      
  23. 7.      ElemType data[MaxSize];      
  24. 8.     int length;      
  25. 9.  } SqList;      
  26. 10. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表       
  27. 11. void InitList(SqList *&L);//初始化线性表InitList(L)       
  28. 12. void DestroyList(SqList *&L);//销毁线性表DestroyList(L)       
  29. 13. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)       
  30. 14. int ListLength(SqList *L);//求线性表的长度ListLength(L)       
  31. 15. void DispList(SqList *L);//输出线性表DispList(L)       
  32. 16. bool GetElem(SqList *L,int i,ElemType &e);//求某个数据元素值GetElem(L,i,e)       
  33. 17. int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)       
  34. 18. bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素ListInsert(L,i,e)       
  35. 19. bool ListDelete(SqList *&L,int i,ElemType &e);//删除数据元素ListDelete(L,i,e)      
  36. 20. void unionList(SqList *LA, SqList *LB, SqList *&LC);//合并    
  37. 21. void CreateList(SqList *&L, ElemType a[], int n)      
  38. 22. {      
  39. 23.     int i;      
  40. 24.     L=(SqList *)malloc(sizeof(SqList));      
  41. 25.     for (i=0; i<n; i++)      
  42. 26.        L->data[i]=a[i];      
  43. 27.    L->length=n;      
  44. 28. }      
  45. 29.       
  46. 30. //初始化线性表InitList(L)       
  47. 31. void InitList(SqList *&L)   //引用型指针       
  48. 32. {      
  49. 33.     L=(SqList *)malloc(sizeof(SqList));  //分配存放线性表的空间       
  50. 34.     L->length=0;      
  51. 35. }      
  52. 36.       
  53. 37. //销毁线性表DestroyList(L)       
  54. 38. void DestroyList(SqList *&L)      
  55. 39. {      
  56. 40.     free(L);      
  57. 41. }      
  58. 42.      
  59. 43. //判定是否为空表ListEmpty(L)       
  60. 44. bool ListEmpty(SqList *L)      
  61. 45. {      
  62. 46.     return(L->length==0);      
  63. 47. }      
  64. 48.       
  65. 49. //求线性表的长度ListLength(L)       
  66. 50. int ListLength(SqList *L)      
  67. 51. {      
  68. 52.     return(L->length);      
  69. 53. }      
  70. 54.       
  71. 55. //输出线性表DispList(L)       
  72. 56. void DispList(SqList *L)      
  73. 57. {      
  74. 58.     int i;      
  75. 59.     if (ListEmpty(L)) return;      
  76. 60.     for (i=0; i<L->length; i++)      
  77. 61.         printf("%d ",L->data[i]);      
  78. 62.     printf("\n");      
  79. 63. }      
  80. 64. bool GetElem(SqList *L,int i,ElemType &e)      
  81. 65. {      
  82. 66.     if (i<1 || i>L->length)  return false;      
  83. 67.     e=L->data[i-1];      
  84. 68.     return true;      
  85. 69. }      
  86. 70.       
  87. 71.     
  88. 72. bool ListInsert(SqList *&L,int i,ElemType e)      
  89. 73. {      
  90. 74.     int j;      
  91. 75.     if (i<1 || i>L->length+1)      
  92. 76.         return false;   //参数错误时返回false       
  93. 77.     i--;            //将顺序表逻辑序号转化为物理序号       
  94. 78.     for (j=L->length; j>i; j--) //将data[i..n]元素后移一个位置       
  95. 79.         L->data[j]=L->data[j-1];      
  96. 80.     L->data[i]=e;           //插入元素e       
  97. 81.     L->length++;            //顺序表长度增1       
  98. 82.     return true;            //成功插入返回true       
  99. 83. }      
  100. 84. int LocateElem(SqList *L, ElemType e)      
  101. 85. {      
  102. 86.    int i=0;      
  103. 87.     while (i<L->length && L->data[i]!=e) i++;      
  104. 88.     if (i>=L->length)  return 0;      
  105. 89.     else  return i+1;      
  106. 90. }      
  107. 91. void unionList(SqList *LA, SqList *LB, SqList *&LC)    
  108. 92. {    
  109. 93.     int lena,i;    
  110. 94.     ElemType e;    
  111. 95.     InitList(LC);    
  112. 96.     for (i=1; i<=ListLength(LA); i++) //将LA的所有元素插入到Lc中    
  113. 97.     {    
  114. 98.         GetElem(LA,i,e);    
  115. 99.         ListInsert(LC,i,e);    
  116. 100.        }    
  117. 101.        lena=ListLength(LA);         //求线性表LA的长度    
  118. 102.        for (i=1; i<=ListLength(LB); i++)    
  119. 103.        {    
  120. 104.            GetElem(LB,i,e);         //取LB中第i个数据元素赋给e    
  121. 105.            if (!LocateElem(LA,e)) //LA中不存在和e相同者,插入到LC中    
  122. 106.                ListInsert(LC,++lena,e);    
  123. 107.        }    
  124. 108.    }    
  125. 109.    int main()    
  126. 110.    {    
  127. 111.        SqList *sq_a, *sq_b, *sq_c;    
  128. 112.        ElemType a[6]= {5,8,7,2,4,9};    
  129. 113.        CreateList(sq_a, a, 6);    
  130. 114.        printf("LA: ");    
  131. 115.        DispList(sq_a);    
  132. 116.        
  133. 117.        ElemType b[6]= {2,3,8,6,0};    
  134. 118.        CreateList(sq_b, b, 5);    
  135. 119.        printf("LB: ");    
  136. 120.        DispList(sq_b);    
  137. 121.        unionList(sq_a, sq_b, sq_c);    
  138. 122.        printf("LC: ");    
  139. 123.        DispList(sq_c);    
  140. 124.        return 0;    
  141. 125.    }    
  142.   
  143. 运行结果:  
  144. <img src="http://img.blog.csdn.net/20160918111345200?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />  
  145. 知识点总结:  
  146. 利用顺序表得基本运算以及顺序表中不包含相同元素这两个知识点完成  
  147. 学习心得:  
  148. 学习了新的知识,感觉难度加深,要多多努力
0 0
原创粉丝点击