两个顺序表LA和LB分别表示两个集合A和B,要求一个新的集合A=A∪B。使用c语言
时间: 2023-06-01 18:04:08 浏览: 112
2_链表_求la和lb的交集_
5星 · 资源好评率100%
实现代码如下:
```c
#include <stdio.h>
#define MAXSIZE 100
typedef struct {
int data[MAXSIZE];
int length;
} SqList;
void InitList(SqList *L) {
L->length = 0;
}
void PrintList(SqList L) {
int i;
for (i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
}
void ListUnion(SqList *La, SqList Lb) {
int i;
for (i = 0; i < Lb.length; i++) {
int j, flag = 0;
for (j = 0; j < La->length; j++) {
if (Lb.data[i] == La->data[j]) {
flag = 1;
break;
}
}
if (!flag) {
La->data[La->length++] = Lb.data[i];
}
}
}
int main() {
SqList La, Lb;
int i;
InitList(&La);
InitList(&Lb);
La.data[0] = 1;
La.data[1] = 2;
La.data[2] = 3;
La.length = 3;
Lb.data[0] = 2;
Lb.data[1] = 3;
Lb.data[2] = 4;
Lb.length = 3;
printf("La: ");
PrintList(La);
printf("Lb: ");
PrintList(Lb);
ListUnion(&La, Lb);
printf("La after union: ");
PrintList(La);
return 0;
}
```
输出结果:
```
La: 1 2 3
Lb: 2 3 4
La after union: 1 2 3 4
```
阅读全文