?? 集合交和并.cpp
字號:
#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>
struct list
{
int first;
list *next;
};
list* setup(list *nn)
{
list *head;
int n;
cout<<"輸入集合的元素個數為"<<endl;
cin>>n;
head=nn=NULL;
list *current;
list *pend;
int i=1;
while(i<=n)
{
current=new list;
cin>>current->first;
int tt=current->first;
for(list*w=head;w!=NULL;w=w->next)
if(w->first==current->first)
exit(1);
if(head==NULL)
head=current;
else
pend->next=current;
pend=current;
pend->next=NULL;
i++;
}
return (head);
}
void put(list *dd)
{
cout<<"{";
cout<<dd->first<<" ";
list *h;
h=dd->next;
while(h!=NULL)
{
cout<<h->first<<" ";
h=h->next;
}
cout<<"}"<<endl;
}
list* B(list *dd,list *bb)
{
int k=0;
list *m;
list *nn;
list *tt=dd;
for(bb;bb!=NULL;bb=bb->next)
{
k=bb->first;
for(nn=dd;nn!=NULL;nn=nn->next)
{
if(k==nn->first)
break;
if(nn->next==NULL)
{
m=new list;
m->first=k;
m->next=nn->next;
nn->next=m;
}
}
}
return tt;
}
list*J(list *ss,list*bb)
{
int c=0;
list *head4=NULL;
list *pend;
list *t;
for(ss;ss!=NULL;ss=ss->next)
{
c=ss->first;
for(list *rr=bb;rr!=NULL;rr=rr->next)
if(c==rr->first)
{
t=new list;
t->first=c;
if(head4==NULL)
head4=t;
else pend->next=t;
pend=t;
pend->next=NULL;
}
}
return head4;
}
void main()
{
list *headA;
list *headB;
list *a1=setup(headA);
put(a1);
list *a2=setup(headB);
put(a2);
list *N=J(a1,a2);
cout<<"交集為"<<"AnB=";
put(N);
list *U=B(a1,a2);
cout<<"并集為"<<"AUB=";
put(U);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -