题目描述
假设有两个集合 A 和 B 分别用两个线性表 LA 和 LB 表示,即线性表中的数据元素即为集合中的成员。编程实现集合A和集合B的交运算。
输入
第一行为集合A的数据元素个数n;
第二行输入n个集合A的数据元素 ;
第三行为集合B的数据元素的个数;
第四行输入m个集合B的数据元素
输出
A和B的交集
样例输入
8
0 5 6 3 8 7 9 10
7
1 3 4 7 8 9 5
样例输出
5 3 8 7 9
#include<stdio.h>
#include<malloc.h>
#define MaxSize 1000
typedef struct node
{
int data[MaxSize];
int length;
}Lnode;
void init(Lnode *&L)
{
L=(Lnode*)malloc(sizeof(Lnode));
L->length=0;
}
void Insert(Lnode*&L,int num)
{
L->data[L->length++]=num;
}
int main()
{
Lnode *L1,*L2;
init(L1);
init(L2);
int n,num;
scanf("%d",&n);
while(n--)
{
scanf("%d",&num);
Insert(L1,num);
}
int m;
scanf("%d",&m);
while(m--)
{
scanf("%d",&num);
Insert(L2,num);
}
for(int i=0;i<L1->length;i++)
//遍历L1中的每个数,在L2中查找,如同找到相同数则输出
{
for(int j=0;j<L2->length;j++)
{
if(L1->data[i]==L2->data[j])
{
printf("%d ",L1->data[i]);
}
}
}
}