33,027
社区成员




#include <iostream.h>
#include <stdlib.h>
typedef struct List//节点的定义
{
int data;
struct List *next;
}List,* Link;
void CreateList(Link head)//创建链表
{
head=(Link)malloc(sizeof(List));
head->next=NULL;
while (1)
{
Link pointor=(Link)malloc(sizeof(List));
cin>>pointor->data;
if(!cin)//判断是否为字符
{
cin.clear();
cin.get();
continue;
}
else
{
break;
}
pointor->next=head->next;
head->next=pointor;
}
}
void PrintList(Link head)
{
Link newhead=head->next;
do
{
cout<<newhead->data<<" ";
newhead=newhead->next;
}while (newhead->next!=NULL);
}
void SetIntersection(Link head1,Link head2)//集合的交集
{
for(Link p=head1->next;p!=NULL;p=p->next)
for(Link q=head2->next;q!=NULL;q=q->next)
{
if (p->data==q->data)
{
cout<<p->data<<" ";
}
}
cout<<endl;
}
void SetAltogeter(Link head1,Link head2)//集合并集
{
for (Link p=head1->next;p!=NULL;p=p->next)
{
for (Link q=head2->next;q!=NULL;q=q->next)
{
if(p->data!=q->data)
{
cout<<p->next<<" ";
}
}
}
PrintList(head2);
cout<<endl;
}
void SetMission(Link head1,Link head2)//集合的差
{
cout<<"Head1-Head2:"<<endl;
for (Link p=head1->next;p!=NULL;p=p->next)
{
for (Link q=head2->next;q!=NULL;q=q->next)
{
if (p->data!=q->data)
{
cout<<p->data<<" ";
}
}
}
cout<<endl;
cout<<"Head2-Head1"<<endl;
for (Link m=head2->next;m!=NULL;m=m->next)
{
for (Link n=head1->next;n!=NULL;n=n->next)
{
if (m->data!=n->data)
{
cout<<m->data<<" ";
}
}
}
cout<<endl;
}
void main()
{
Link head1;
Link head2;
cout<<"创建列表Head1:"<<endl;
CreateList(head1);
cout<<"创建列表Head2:"<<endl;
CreateList(head2);
cout<<"集合的交集:"<<endl;
SetIntersection(head1,head2);
cout<<"集合的并集:"<<endl;
SetAltogeter(head1,head2);
cout<<"集合的差:"<<endl;
SetMission(head1,head2);
}