第三周项目3求集合并集

  1. main.cpp

  2. #include "list.h"  
  3. #include <stdio.h>  
  4. void unionList(SqList *LA, SqList *LB, SqList *&LC)  
  5. {  
  6.     int lena,i;  
  7.     ElemType e;  
  8.     InitList(LC);  
  9.     for (i=1; i<=ListLength(LA); i++) //将LA的所有元素插入到Lc中  
  10.     {  
  11.         GetElem(LA,i,e);  
  12.         ListInsert(LC,i,e);  
  13.     }  
  14.     lena=ListLength(LA);         //求线性表LA的长度  
  15.     for (i=1; i<=ListLength(LB); i++)  
  16.     {  
  17.         GetElem(LB,i,e);         //取LB中第i个数据元素赋给e  
  18.         if (!LocateElem(LA,e)) //LA中不存在和e相同者,插入到LC中  
  19.             ListInsert(LC,++lena,e);  
  20.     }  
  21. }  
  22. //用main写测试代码  
  23. int main()  
  24. {  
  25.     SqList *sq_a, *sq_b, *sq_c;  
  26.     ElemType a[6]= {5,8,7,2,4,9};  
  27.     CreateList(sq_a, a, 6);  
  28.     printf("LA: ");  
  29.     DispList(sq_a);  
  30.   
  31.     ElemType b[6]= {2,3,8,6,0};  
  32.     CreateList(sq_b, b, 5);  
  33.     printf("LB: ");  
  34.     DispList(sq_b);  
  35.     unionList(sq_a, sq_b, sq_c);  
  36.     printf("LC: ");  
  37.     DispList(sq_c);  
  38.     return 0;  
  39. }  


hang.h

[cpp]  view plain  copy
  1. <span style="font-size:14px;">#ifndef LIST_H_INCLUDED  
  2. #define LIST_H_INCLUDED  
  3.   
  4. #define MaxSize 50  
  5. typedef int ElemType;  
  6. typedef struct  
  7. {  
  8.     ElemType data[MaxSize];  
  9.     int length;  
  10. } SqList;  
  11. void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表  
  12. void InitList(SqList *&L);//初始化线性表InitList(L)  
  13. void DestroyList(SqList *&L);//销毁线性表DestroyList(L)  
  14. bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)  
  15. int ListLength(SqList *L);//求线性表的长度ListLength(L)  
  16. void DispList(SqList *L);//输出线性表DispList(L)  
  17. bool GetElem(SqList *L,int i,ElemType &e);//求某个数据元素值GetElem(L,i,e)  
  18. int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)  
  19. bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素ListInsert(L,i,e)  
  20. bool ListDelete(SqList *&L,int i,ElemType &e);//删除数据元素ListDelete(L,i,e)#endif // LIST_H_INCLUDED  
  21. #endif  
  22. </span>  



   hang.cpp
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "list.h"  
  4. //用数组创建线性表  
  5. void CreateList(SqList *&L, ElemType a[], int n)  
  6. {  
  7.     int i;  
  8.     L=(SqList *)malloc(sizeof(SqList));<span style="color:#ff0000;">//注意c语言的开辟动态内存的格式</span>  
  9.     for (i=0; i<n; i++)  
  10.         L->data[i]=a[i];  
  11.     L->length=n;  
  12. }  
  13. //初始化线性表InitList(L)  
  14. void InitList(SqList *&L)   //引用型指针  
  15. {  
  16.     L=(SqList *)malloc(sizeof(SqList));  
  17.     //分配存放线性表的空间  
  18.     L->length=0;  
  19. }  
  20.   
  21. //销毁线性表DestroyList(L)  
  22. void DestroyList(SqList *&L)  
  23. {  
  24.     free(L);<span style="color:#ff0000;">//值得借鉴的</span>  
  25. }  
  26.   
  27. //判定是否为空表ListEmpty(L)  
  28. bool ListEmpty(SqList *L)  
  29. {  
  30.     return(L->length==0);<span style="color:#ff0000;">//为空时返回1</span>  
  31. }  
  32.   
  33. //求线性表的长度ListLength(L)  
  34. int ListLength(SqList *L)  
  35. {  
  36.     return(L->length);  
  37. }  
  38.   
  39. //输出线性表DispList(L)  
  40. void DispList(SqList *L)  
  41. {  
  42.     int i;  
  43.     if (ListEmpty(L))  
  44.         return;  
  45.     for (i=0; i<L->length; i++)  
  46.         printf("%d ",L->data[i]);  
  47.     printf("\n");  
  48. }  
  49.   
  50. //求某个数据元素值GetElem(L,i,e)  
  51. bool GetElem(SqList *L,int i,ElemType &e)<span style="color:#ff6666;">//引用,e</span>  
  52. {  
  53.     if (i<1 || i>L->length)  return false;  
  54.     e=L->data[i-1];  
  55.     return true;  
  56. }  
  57.   
  58. //按元素值查找LocateElem(L,e)  
  59. int LocateElem(SqList *L, ElemType e)  
  60. {  
  61.     int i=0;  
  62.     while (i<L->length && L->data[i]!=e) i++;  
  63.     if (i>=L->length)  return 0;  
  64.     else  return i+1;  
  65. }  
  66.   
  67. //插入数据元素ListInsert(L,i,e)  
  68. bool ListInsert(SqList *&L,int i,ElemType e)  
  69. {  
  70.     int j;  
  71.     if (i<1 || i>L->length+1)  
  72.         return false;   //参数错误时返回false  
  73.     i--;            //将顺序表逻辑序号转化为物理序号  
  74.     for (j=L->length; j>i; j--) //将data[i..n]元素后移一个位置  
  75.         L->data[j]=L->data[j-1];  
  76.     L->data[i]=e;           //插入元素e  
  77.     L->length++;            //顺序表长度增1  
  78.     return true;            //成功插入返回true  
  79. }  
  80.   
  81. //删除数据元素ListDelete(L,i,e)  
  82. bool ListDelete(SqList *&L,int i,ElemType &e)  
  83. {  
  84.     int j;  
  85.     if (i<1 || i>L->length)  //参数错误时返回false  
  86.         return false;  
  87.     i--;        //将顺序表逻辑序号转化为物理序号  
  88.     e=L->data[i];  
  89.     for (j=i; j<L->length-1; j++) //将data[i..n-1]元素前移  
  90.         L->data[j]=L->data[j+1];  
  91.     L->length--;              //顺序表长度减1  
  92.     return true;              //成功删除返回true  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值