如何用链表实现集合的交集,并集,差集

\u7528\u94fe\u8868\u5b9e\u73b0\u96c6\u5408\u7684\u4ea4\u96c6\u5e76\u96c6\u5dee\u96c6

#include
#include
#include
#include
using namespace std;

int main()
{
int a[]={1,5,8,12,5,-5,32};
int b[]={3,5,1,-3,10};

list set1(a,a+sizeof(a)/sizeof(int));
list set2(b,b+sizeof(b)/sizeof(int));
list result;

set1.sort();
set2.sort();

//\u4ea4\u96c6
set_intersection(set1.begin(),set1.end(),set2.begin(),set2.end(),back_inserter(result));
copy(result.begin(),result.end(),ostream_iterator(cout," "));
cout<<endl;
result.clear();

//\u5e76\u96c6
set_union(set1.begin(),set1.end(),set2.begin(),set2.end(),back_inserter(result));
copy(result.begin(),result.end(),ostream_iterator(cout," "));
cout<<endl;
result.clear();


//\u5dee\u96c6
set_difference(set1.begin(),set1.end(),set2.begin(),set2.end(),back_inserter(result));
copy(result.begin(),result.end(),ostream_iterator(cout," "));

return 0;
}

C++\u4e2d\u94fe\u8868\u63d2\u5165\uff0c\u53ef\u4ee5\u8868\u793a\u96c6\u5408\u7684\u4ea4\u96c6\u548c\u5e76\u96c6\u3002
\u4f46\u6211\u5efa\u8bae\u4f60\u6839\u672c\u4e0d\u5e94\u8be5\u7528\u94fe\u8868\uff0c\u8868\u793a\u96c6\u5408\u7684\u5e76\u96c6\uff0c\u672c\u6765\u5c31\u6709\u4e00\u79cd\u53eb\u505a\u5e76\u67e5\u96c6\u7684\u6570\u636e\u7ed3\u6784\u3002
\u8868\u793a\u96c6\u5408\u7684\u4ea4\u96c6\uff0c\u672c\u6765\u5c31\u6709\u4e00\u79cd\u53eb\u54c8\u5e0c\u8868\u7684\u6570\u636e\u7ed3\u6784

#include<stdio.h>
#define maxsize 100
struct Sqlist
{
int elem[maxsize];
int length;
};

void difference(Sqlist &la,Sqlist lb)
{ //求A与B的并集
int i,j,e;
for(i=0;i<lb.length;i++)
{
e=lb.elem[i]; j=0;
while((j<la.length)&&(la.elem[j]!=e)) j++;
if(j==la.length){
la.elem[la.length]=e;la.length++;}
}
}

//求A与B的交集
void Intersection(Sqlist &la,Sqlist lb)
{int i,j;
static int n=0;
for(i=0;i<lb.length;i++)
{ j=0;
while(j<la.length&&la.elem[j]!=lb.elem[i])
j++;
if(la.elem[j]==lb.elem[i])
{
la.elem[n]=lb.elem[i];
n++;

}
}
void main()
{
int i;
struct Sqlist la,lb,lc;
printf("请输入集合A、集合B的元素个数\n");
scanf("%d %d",&la.length,&lb.length);
printf("请输入集合A的元素\n");
for(i=0;i<la.length;i++)
scanf("%d",&la.elem[i]);
printf("请输入集合B的元素\n");
for(i=0;i<lb.length;i++)
scanf("%d",&lb.elem[i]);
difference(la,lb);
printf("并集中的元素为:\n");
for(i=0;i<la.length;i++)
printf("%-4d",la.elem[i]);
printf("\n");

Intersection(la,lb);
printf("交集中的元素为:\n");
for(i=0;i<n;i++)
printf("%-4d",la.elem[i]);
printf("\n");
}

我只会顺序表的

#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
using namespace std;

int main()
{
int a[]={1,5,8,12,5,-5,32};
int b[]={3,5,1,-3,10};

list< int > set1(a,a+sizeof(a)/sizeof(int));
list< int > set2(b,b+sizeof(b)/sizeof(int));
list< int > result;

set1.sort();
set2.sort();

//交集
set_intersection(set1.begin(),set1.end(),set2.begin(),set2.end(),back_inserter(result));
copy(result.begin(),result.end(),ostream_iterator< int >(cout," "));
cout<<endl;
result.clear();

//并集
set_union(set1.begin(),set1.end(),set2.begin(),set2.end(),back_inserter(result));
copy(result.begin(),result.end(),ostream_iterator< int >(cout," "));
cout<<endl;
result.clear();

//差集
set_difference(set1.begin(),set1.end(),set2.begin(),set2.end(),back_inserter(result));
copy(result.begin(),result.end(),ostream_iterator< int >(cout," "));

return 0;
}

  • 閲囩敤杩愮畻绗﹂噸杞芥眰闆嗗悎鐨浜ゅ苟宸
    绛旓細void Delete(int x);//鍒犻櫎闆嗗悎鍏冪礌x void display();//杈撳嚭 闆嗗悎鍏冪礌x TSet operator*(const TSet & s1); //瀹炵幇涓や釜闆嗗悎鐨勪氦闆 TSet operator-(TSet & s1); //瀹炵幇涓や釜闆嗗悎鐨勫樊闆 TSet operator+(TSet & s1); //瀹炵幇涓や釜闆嗗悎鐨骞堕泦 TSet& operator=(TSet & s1); //瀹炵幇闆嗗悎璧嬪艰繍绠...
  • 鏁版嵁缁撴瀯閾捐〃鐨勫苟闆(鐩稿悓鍏冪礌涓嶄繚鐣)
    绛旓細if(head==NULL) //濡傛灉澶存寚閽堜负绌猴紝璇存槑姝ゆ椂閾捐〃涓虹┖ { head=p; //鐩存帴璧嬪煎嵆鍙 } else //鍚﹀垯 { p2=head; //p2鎸囧悜澶寸粨鐐 p1=head->next; //p1鎸囧悜澶寸粨鐐圭殑涓嬩竴浣 while(p1) //濡傛灉澶寸粨鐐圭殑涓嬩竴浣嶈繕鏈変笢瑗匡紙鍗充笉涓虹┖锛墈 p2=p1; //鍒欑敤p2鎸囧悜澶寸粨鐐圭殑涓嬩竴...
  • 涓閾捐〃鐨勫苟闆
    绛旓細{//鑻閾捐〃閲岄潰鏈夊拰e鐩哥瓑鐨勫垯杩斿洖绗竴涓拰e鐩哥瓑鍏冪礌鐨勪綅搴忥紝寮变笉鐩哥瓑鍒欒繑鍥0 struct sted *p=head;int m=1;while(p!=NULL){ if(p->num!=e.num){ m++;p=p->next;} else break;} if(m>n) m=0;return m;} struct sted * listinsert(struct sted *head,int m,struct sted e){...
  • ...闆嗗悎,鍏跺厓绱犻掑鎺掑垪,缂栧啓涓涓嚱鏁版眰鍑篈鍜孊鐨勪氦闆
    绛旓細鐢变簬闂涓庨棶棰樿ˉ鍏呬笉涓鑷达紝闂鏄姹浜ら泦锛鏄壘A鍜孊鐨勭浉鍚屽厓绱狅紝闂琛ュ厖閲屾槸姹骞堕泦锛屽己璋冧笉鏄綊骞讹紝鎴戞寜骞堕泦鍐欑殑锛屽鏋滄槸浜ら泦鍐嶇◢鍔犱慨鏀瑰氨鍙互浜嗐侺inkList *LinkMag(LinkList *ha, LinkList *hb)//鍋囪ha鍜宧b鏄甫澶寸粨鐐圭殑閾捐〃 { Link *p, *q, *r,*hc,*aa;hc->head = (Link*)malloc...
  • ...甯姪,鍒╃敤C璇█瀹炵幇:姹備换鎰忎袱涓闆嗗悎鐨勪氦闆銆骞堕泦銆佸樊闆,
    绛旓細浠ュ墠鍐欒繃涓涓函C鐨勶紝 鐢ㄧ殑鏄暟缁勶紝妯℃嫙C++ STL閲岄潰鐨剆et_intersection锛宻et_union鍜宻et_difference鐨瀹炵幇銆 绋嶄綔浜嗕慨鏀癸紝娣诲姞浜嗕簺娉ㄩ噴锛屽笇鏈涜兘甯埌浣犮傛敞鎰忥細蹇呴』鍏堝杈撳叆闆嗗悎鎺掑簭锛涜緭鍑虹粨鏋滃拰C++ STL鐨勬祴璇曠粨鏋滃惢鍚堛俰nclude <stdio.h>#include <stdlib.h>#include <string.h>int set_intersection (...
  • c璇█ 涓や釜闈為檷搴閾捐〃鐨勫苟闆
    绛旓細include <stdio.h>#include <stdlib.h>#include typedef int DataType;typedef struct node {DataType data;struct node *next;}*LinkList, *pNode;LinkList GetEmptyList() {LinkList head = (pNode)malloc(sizeof(struct node));head->data = 0;head->next = 0;return head;}int AddNod...
  • 璁捐涓涓闆嗗悎绫,鐢ㄦ潵澶勭悊鏁村瀷鏁
    绛旓細//璁捐涓涓泦鍚堢被锛岀敤鏉ュ鐞嗘暣鍨嬫暟锛岃姹傚叿鏈夊涓嬪姛鑳斤細//(1) 鍏锋湁涓洪泦鍚堝鍔犲厓绱犵殑鍔熻兘銆//(2) 鍏锋湁澶勭悊闆嗗悎鐨勪氦闆銆佸苟闆嗙殑鍔熻兘锛屽苟鐢 "+" 琛ㄧず骞堕泦锛 "*" 琛ㄧず浜ら泦銆//(3) 瑕佹眰鐢ㄩ摼琛鏉ュ瓨鍌ㄩ泦鍚堢殑鍏冪礌銆//(4) 缂栧啓涓涓猰ain()鍑芥暟锛屾祴璇曚綘鐨勯泦鍚堢被鐨勫悇绉嶅姛鑳姐俰nclude<iostream.h> //...
  • 涓や釜闈為檷搴閾捐〃鐨勫苟闆,1->2->3 鍜 2->3->5 骞朵负 1->2->3->5,鍙兘杈撳嚭...
    绛旓細p2鎸囬拡鍒嗗埆鎸囧悜浠栦滑鐨勫ご鎸囬拡锛屽啀瀹氫箟涓涓閾捐〃temp骞剁敤pt鎸囧悜瀹 姣旇緝p1,p2鎸囧悜鐨勫厓绱犲ぇ灏 濡傛灉鐩哥瓑 鍒欐彃鍏ヨ鍏冪礌锛屽苟涓攑1锛宲2锛宲t鎸囬拡鍧囧線鍚庣Щ鍔ㄤ竴涓紱鍚﹀垯 鎻掑叆涓や釜涔嬩腑杈冨皬鐨勫摢涓涓紝骞朵笖璇ユ寚閽堜笌pt鍧囧線鍚庣Щ鍔ㄤ竴涓 濡傛灉p1锛宲2閮芥寚鍚戜簡鏈鍚庝竴涓厓绱犲垯閫鍑猴紝杩斿洖閾捐〃pt锛涘惁鍒欒烦杞埌2 ...
  • C璇█姹備袱涓『搴忚〃鐨骞堕泦
    绛旓細printf("璇疯緭鍏闆嗗悎b鐨勫厓绱燶n");for(i=0;i<maxsize;i++){ scanf("%d%c",&lb.elem[i],&c);lb.last++;if(c=='\n')break;} unionlist(la,lb);printf("闆嗗悎a涓巄鐨勫苟闆涓猴細\n");for(i=0;i<la.last;i++)printf("%-4d",la.elem[i]);printf("\n");} 浣犵殑閾捐〃闀垮害娌℃湁...
  • 銆愰棶棰樻弿杩般 涓や釜闈為檷搴閾捐〃鐨勫苟闆,渚嬪灏嗛摼琛1->2->3 鍜 2->3->...
    绛旓細/*閿姣閾捐〃*/ void destoryLlist(Linklist *L){ Linklist *q;while(L){ q=L->next;free(L);L=q;} } /*骞堕泦*/ Linklist* ABList(Linklist *L1,Linklist *L2){ Linklist *head;head=(Linklist*)malloc(sizeof(Linklist));//浜х敓澶寸粨鐐 if(!head) exit(OVERFLOW);head->next=...
  • 扩展阅读:高一数学交集并集视频讲解 ... 同时成立取交集 ... 链表求交集 ... 有效沟通的四种方法 ... 两个有序列表的交集 ... 交集的表示图 ... 有效沟通的方式方法 ... python创建集合的方式 ... 求两个单链表的交集和并集 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网