线性表的应用:将所有在线性表Lb中但不在线性表La中的元素插入线性表La

电玩女神 2022-03-11 10:22 260阅读 0赞
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include <malloc.h>
  4. using namespace std;
  5. #define MaxSize 50
  6. typedef char ElemType;
  7. typedef struct
  8. {
  9. ElemType elem[MaxSize]; //存放顺序表中的元素
  10. int length; //存放顺序表的长度
  11. } SqList;
  12. void InitList(SqList *&L) //初始化线性表,构造一个空的线性表,并将长度设置为0
  13. {
  14. L=(SqList *)malloc(sizeof(SqList));
  15. L->length=0;
  16. }
  17. int ListLength(SqList *L) //求线性表的长度
  18. {
  19. return(L->length);
  20. }
  21. int GetElem(SqList *L,int i,ElemType &e)//求线性表中某个数据元素的值
  22. {
  23. if (i<1 || i>L->length)
  24. return 0;
  25. e=L->elem[i-1];
  26. return 1;
  27. }
  28. int LocateElem(SqList *L, ElemType e)//按元素查找,找到与该元素值相同的元素并返回其序号
  29. {
  30. int i=0;
  31. while (i<L->length && L->elem[i]!=e) i++;
  32. if (i>=L->length)
  33. return 0;
  34. else
  35. return i+1;
  36. }
  37. int ListInsert(SqList *L,int i,ElemType e)//插入数据元素,在第i个位置上插入新元素,使后面的元素依次后移,并是长度加1;
  38. {
  39. int j;
  40. if (i<1 || i>L->length+1)
  41. return 0;
  42. i--; /*将顺序表位序转化为elem下标*/
  43. for (j=L->length;j>i;j--) /*将elem[i]及后面元素后移一个位置*/
  44. L->elem[j]=L->elem[j-1];
  45. L->elem[i]=e;
  46. L->length++; /*顺序表长度增1*/
  47. return 1;
  48. }
  49. int ListEmpty(SqList *L) //判断线性表是否为空 即看其长度是否为0
  50. {
  51. return(L->length==0);
  52. }
  53. void DispList(SqList *L) //输出线性表
  54. {
  55. int i;
  56. if (ListEmpty(L)) return;
  57. for (i=0;i<L->length;i++)
  58. printf("%c",L->elem[i]);
  59. printf("\n");
  60. }
  61. void Union(SqList *La,SqList *Lb){
  62. ElemType e;
  63. int La_len;
  64. int Lb_len;
  65. La_len=ListLength(La);
  66. Lb_len=ListLength(Lb);
  67. for(int i=1;i<=Lb_len;i++){
  68. GetElem(Lb,i,e);
  69. if(!LocateElem(La,e)) ListInsert(La,++La_len,e);
  70. }
  71. }
  72. void main()
  73. {
  74. SqList *La;
  75. SqList *Lb;
  76. ElemType e;
  77. int La_len;
  78. int Lb_len;
  79. int i;
  80. printf("(1)初始化顺序表L\n");
  81. InitList(La);
  82. InitList(Lb);
  83. printf("输入La");
  84. for(i=0;i<5;i++)
  85. { cin>>e;
  86. ListInsert(La,i,e);}
  87. printf("输入Lb");
  88. for(i=0;i<5;i++)
  89. { cin>>e;
  90. ListInsert(Lb,i,e);}
  91. /*ListInsert(La,1,'a');
  92. ListInsert(La,2,'b');
  93. ListInsert(La,3,'c');
  94. ListInsert(La,4,'d');
  95. ListInsert(La,5,'e');
  96. printf("(2)依次采用尾插法插入元素\n");
  97. ListInsert(Lb,1,'a');
  98. ListInsert(Lb,2,'b');
  99. ListInsert(Lb,3,'c');
  100. ListInsert(Lb,4,'d');
  101. ListInsert(Lb,5,'5');*/
  102. printf("输出线性表L:");
  103. DispList(La);
  104. DispList(Lb);
  105. Union(La,Lb);
  106. /*La_len=ListLength(La);
  107. Lb_len=ListLength(Lb);
  108. for(int i=1;i<=Lb_len;i++){
  109. GetElem(Lb,i,e);
  110. if(!LocateElem(La,e)) ListInsert(La,++La_len,e);
  111. }*/
  112. printf("输出插入完成后的线性表:");
  113. DispList(La);
  114. }

代码参考:https://blog.csdn.net/ciganxian/article/details/27549653

发表评论

表情:
评论列表 (有 0 条评论,260人围观)

还没有评论,来说两句吧...

相关阅读

    相关 线性

    线性表 数据之间为线性关系,数据元素“一个接一个的排列” 在一个线性表中数据元素的类型必须是相同的。 一般只是存储查询数据就用顺序表,如果更改数据频繁就用链式表 1.顺

    相关 线性

    线性表的定义:一个线性表是n个数据元素的有限序列。 在复杂的线性表中,一个数据元素可以由若干个数据项组成,这种情况下,通常把数据元素称为记录,含有大量记录的线性表称为文件。

    相关 插入(线性)

    题目描述 (线性表)已知一单链表,从第二个结点至表尾递增有序,(设a1<x<an)如下图(“第二个结点至表尾”指a1…an )。试编写程序,将第一个结点删除并插入表中

    相关 线性

    线性表的类型定义 线性表:具有相同特性数据元素的一个有限序列 在复杂的线性表中,一个数据元素由若干个数据项组成。在这种情况下,常把数据元素

    相关 线性

    经历过了测试,如有不妥的地方,望请雅正!!! Matrix.h 1 /单链表: 2 基本操作:头结点创建以及尾节点创建 3