待改写:存储数据类型int——>char
重复的元素可存储
功能上不完善
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int data;
int flag;
struct Linklist *next;
} Linklist;
//对LA,LB中相同的元素打上标记
Linklist* Link_get_flag(Linklist *LA,Linklist *LB)
{
Linklist *p = LA->next,*q = LB->next;
//此处双重while循环目的是将LA,LB中相同的元素打上标记,便于求他们的交并差集。
while(p != NULL)
{
while(q!=NULL)
{
if(p->data == q->data)
{
p->flag = 1;
q->flag = 1;
q = q->next;
}
else
{
q = q->next;
}
}
q = LB->next;
p = p->next;
}
}
int creatLinklinst(Linklist *L,int i)
{
Linklist *p;
L ->next = NULL;
printf("依次输入元素各个值");
for(;i>0;--i)
{
p = (Linklist*)malloc(sizeof(Linklist));
p->flag = 0;
scanf("%d",&p->data);
p->next = L->next;
L->next = p;
}
return 1;
}
/*
* 交集
*输入:打标记后的LA/LB
*/
int intersection(Linklist *L)
{
Linklist *node;
Linklist *LC;
LC = (Linklist *)malloc(sizeof(Linklist));
LC->next = NULL;
Linklist *p = L->next;
while(p!=NULL)
{
//处理LC的数据域(*****)
for(;p!=NULL;p = p->next)
{
if(p->flag == 1)
{
node = (Linklist *)malloc(sizeof(Linklist));
node->data = p->data;
node->flag = 0;
node->next = LC->next ;
LC->next = node;
}
else
{
continue;
}
}
printf("交集是:");
while(LC->next!=NULL)
{
LC = LC->next;
printf("%d ",LC->data);
}
printf("
");
return 1;
}
}
/*
* 并集
*输入:打标记后的LA&LB
*/
int union_LALB(Linklist *LA,Linklist *LB)
{
Linklist *p = LA,*q = LB;
Linklist *LC;
LC = (Linklist *)malloc(sizeof(Linklist));
LC->next = NULL;
Linklist *node;
while(p->next!=NULL)
{
if(p->flag==1)
{
p = p->next;
continue;
}
else
{
p = p->next;
node = (Linklist *)malloc(sizeof(Linklist));
node->flag = 0;
node->data = p->data;
node->next = LC->next;
LC->next = node;
}
}
while(q->next!=NULL)
{
q = q->next;
node = (Linklist *)malloc(sizeof(Linklist));
node->flag = 0;
node->data = q->data;
node->next = LC->next;
LC->next = node;
}
printf("并集是:");
while(LC->next!=NULL)
{
LC = LC->next;
printf("%d ",LC->data);
}
printf("
");
return 1;
}
/*
* 差集
*输入:打标记后的LA&LB
*/
int disset(Linklist *LA,Linklist *LB)
{
Linklist *p = LA,*q = LB;
Linklist *LC;
LC = (Linklist *)malloc(sizeof(Linklist));
LC->next = NULL;
Linklist *node;
while(p->next!=NULL)
{
if(p->flag==1)
{
p = p->next;
continue;
}
else
{
p = p->next;
node = (Linklist *)malloc(sizeof(Linklist));
node->flag = 0;
node->data = p->data;
node->next = LC->next;
LC->next = node;
}
}
printf("A-B的差集是:");
while(LC->next!=NULL)
{
LC = LC->next;
printf("%d ",LC->data);
}
printf("
");
return 1;
}
/*
* 输出打标记后的各个元素
*输入:打标记后的LA&LB
*/
int printfLinklist(Linklist *LA,Linklist *LB)
{
Linklist *a,*b;
a = LA;
b = LB;
printf("
LA:");
while(a->next!=NULL)
{
a = a->next;
printf(" %d(%d)",a->data,a->flag);
}
printf("
LB:");
while(b->next!=NULL)
{
b = b->next;
printf(" %d(%d)",b->data,b->flag);
}
printf("
");
}
int main()
{
Linklist *LA,*LB;
int i;
LA = (Linklist *)malloc(sizeof(Linklist));
LB = (Linklist *)malloc(sizeof(Linklist));
//创建链表
printf("输入LA长度:");
scanf("%d",&i);
creatLinklinst( LA,i);
printf("输入LB长度:");
scanf("%d",&i);
creatLinklinst( LB,i);
//打标记
Link_get_flag(LA,LB);
printfLinklist(LA,LB);
//交集
intersection(LA);
//并集
union_LALB(LA,LB);
//差集
disset(LA,LB);
return 0;
}