資源簡介
c++ , 鏈表實現 : 集合的交叉并運算。
并集
兩個集合可以相"加"。A和B的并集是將A和B的元素放到一起構成的新集合。給定集合A,B,定義運算∪如下:A∪B = {e|e∈A 或 e∈B}。A∪B稱為A和B的并集。
交集
一個新的集合也可以通過兩個集合"共"有的元素來構造。A和B的交集,寫作A∩B,是既屬于A的、又屬于B的所有元素組成的集合。若A∩B={\displaystyle \varnothing },則A和B稱作不相交。
差集
兩個集合也可以相"減"。A在B中的相對補集,寫作B?A,是屬于B的、但不屬于A的所有元素組成的集合。在特定情況下,所討論的所有集合是一個給定的全集U的子集。這樣,U?A稱作A的絕對補集,或簡稱補集(余集),寫作A′或CUA。補集可以看作兩個集合相減,有時也稱作差集。
代碼片段和文件信息
#include
using?namespace?std;
struct?Node?{
int??data;
Node?*next; //表示數據元素之間邏輯關系的指針
};
class?linkList?{
Node*head;??//鏈表只要知道頭在哪里,就可以通過指針找到全部的
public?:
//?生成帶頭結點的空鏈表
linkList()?{
head=new?Node;???//?動態分配一個結點空間
head->next=NULL;?//?生成帶頭結點的空鏈表
}
//創建鏈表
int??Create_List()?{
cout<<“\n---輸入元素的個數:“;
int?n;
cin>>n;
cout<<“\n---輸入元素:“;
Node?*tail=head;//tail記下當前鏈表的尾巴結點
for(int?i=1;?i<=n;?i++)?{
int??e;
cin>>e;
Node?*s=new?Node;
s->data=e;
s->next=NULL;
tail->next=s;
tail=s;
}
cout< }
void?Insert_first(int?x)?{
Node*s=new?Node;
s->data=x;
s->next=head->next;
head->next=s;
}
int?search(int?xNode?*&px)?{
Node*p=head->next;
int?count=1;
while(p!=NULL)?{
if(p->data?==?x?)?{
px=p;
return?count;
}
p=p->next;
count++;
}
return?0;
}
//顯示鏈表
void?display()?{
Node?*p=head->next;
if(p==NULL)?{
cout<<“空表\n“;
return;
}
while(p!=NULL)?{
cout<data<<“?“;
p=p->next;
}
cout< }
static?linkList??bin( linkList?A linkList?B)?{
linkList?result;
Node?*p=(A.head)->next;
while(p!=NULL)?{
result.Insert_first(p->data);
p=p->next;
}
p=(B.head)->next;
while(p!=NULL?)?{
Node?*?px;
if(A.search(p->datapx)==0?)
result.Insert_first(p->data);
p=p->next;
}
return?result;
}
static?linkList??jiao( linkList?A linkList?B)?{
linkList?result;
Node?*p=(A.head)->next;
while(p!=NULL)?{
Node?*?px;
if(B.search(p->datapx)!=0?)
result.Insert_first(p->data);
p=p->next;
}
return?result;
}
static?linkList??jian(linkList?A linkList?B)?{
linkList?result;
Node?*p=(A.head)->next;
while(p!=NULL)?{
Node?*?px;
if(B.search(p->datapx)==0?)
result.Insert_
- 上一篇:一元多項式運算器
- 下一篇:C語言實現的DES加密算法
評論
共有 條評論