?? 集合運算.cpp
字號:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct List//節點的定義
{
int data;
struct List *next;
}List,* Link;
Link CreateList(Link head)//創建鏈表
{
head=(Link)malloc(sizeof(List));
head->next=NULL;
//Link pointor;
while (1)
{
Link pointor=(Link)malloc(sizeof(List));
cin>>pointor->data;
if (pointor->data==0)
{
break;
}
pointor->next=head->next;
head->next=pointor;
}
return head;
}
void PrintList(Link head)//輸出集合
{
Link newhead=head->next;
do
{
cout<<newhead->data<<" ";
newhead=newhead->next;
}while (newhead!=NULL);
cout<<endl;
}
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&&q->next==NULL)
{
cout<<p->data<<" ";
}
if(p->data==q->data)
{
break;
}
}
}
PrintList(head2);
cout<<endl;
}
void SetMission(Link head1,Link head2)//集合的差
{
bool flag;
cout<<"Head1-Head2:"<<endl;
for (Link p=head1->next;p!=NULL;p=p->next)
{
flag = true;
for (Link q=head2->next;q!=NULL;q=q->next)
{
if(p->data==q->data)
{
flag = false;
break;
}
}
if(flag) cout<<p->data <<" "<<endl;
}
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&&n->next==NULL)
{
cout<<m->data<<" ";
}
if (m->data==n->data)
{
break;
}
}
}
cout<<endl;
}
void main()
{
freopen("result.txt","w+",stdout);
freopen("test.txt","r+",stdin);
/*for(int i=0;i<10000;i++)
cout<<rand()<<" ";
cout << 0<<endl;
for(i=0;i<10000;i++)
cout<<rand()<<" ";
cout<<0<<endl;*/
Link head1;
Link head2;
head1=CreateList(head1);
head2=CreateList(head2);
SetIntersection(head1,head2);
}//主函數結束
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -