假设利用两个线性表LA和LB分别表示两个几何A和B(即线性表中的数据元素即为集合中的成员),现要求一个新的集合A=AUB。这就是要求对线性表做如下操作:扩大线性表LA,将存在于线性表LB中而不存在线性表A中的数据元素插入到线性表LA中去。只要从线性表LB中依次取得每个数据元素,并依次在线性表LA中进行查访;若不存在,则插入之。上述操作过程可用下列算法描述之。
线性表实现
void Union(SqList &La,SqList Lb)
{
for(int i=0;i<Lb.length;i++)
{
int count=0;
for(int j=0;j<La.length;j++)
{
if(La.elem[j]==Lb.elem[i])
count+=1;
}
if(count==0)
{
La.elem[La.length++]=Lb.elem[i];
}
}
}
main.cpp
#include <iostream>
using namespace std;
#define ERROR 0
#define OK 1
#define MAX 100
typedef struct
{
int *elem;
int length;
}SqList;
int InitList(SqList &L) //构造一个空的线性表L
{
L.elem = new