集合的交并补.docx
《集合的交并补.docx》由会员分享,可在线阅读,更多相关《集合的交并补.docx(32页珍藏版)》请在冰豆网上搜索。
集合的交并补
课程设计1线性表课程设计
一、组长:
二、组员:
三、实验日期:
2010/4/10
四、实验任务:
集合操作(2分)
基本功能要求:
(1)从文件中读入集合数据建立单链表。
(2)分别求出集合的交、并、差。
五、实验原理:
。
(具体思路看注释)
(1)定义单链表结点类型;
(2)建立函数,将数组元素以尾插法的方法插入到单链表中;
(3)建立函数以指针移动的方式将单链表显示出来;
(4)建立求并集的函数,以指针移动的方式将L1和L2中的元素放入L3中;
(5)建立求交集的函数,以指针移动的方式将L1和L2中相同的元素放到L4中;
(6)建立求差集的函数,以指针移动的方式将L1有而L2中无的元素放到L5中;
(7)建立主函数,实现以上功能;
六、实验源程序:
#include
typedefstructLNode//定义单链表结点类型
{
chardata;
structLNode*next;
}LinkList;
LinkList*CreatList(LinkList*L,chara[],intn)/*尾插法插入元素*/
LinkList*p,*r;
inti;
L=(LinkList*)malloc(sizeof(LinkList));
L->next=NULL;
r=L;
for(i=0;i{p=(LinkList*)malloc(sizeof(LinkList));p->data=a[i];r->next=p;r=p;}r->next=NULL;return(L);}voidDispList(LinkList*L)/*显示链表*/{inti=0;LinkList*p;p=L->next;while(p!=NULL){printf("%c",p->data);p=p->next;}printf("\n");}LinkList*BingJi(LinkList*L1,LinkList*L2,LinkList*L3)/*求两个集合的并集*/{LinkList*p1=L1->next,*p2=L2->next,*s,*t;L3=(LinkList*)malloc(sizeof(LinkList));t=L3;while(p1!=NULL&&p2!=NULL)/*当L1和L2都不为空的情况下比较各自数值的大小*/{if(p1->datadata)/*先记录较小的数值,并移动其指针*/{s=(LinkList*)malloc(sizeof(LinkList));s->data=p1->data;t->next=s;t=s;p1=p1->next;}elseif(p1->data>p2->data){s=(LinkList*)malloc(sizeof(LinkList));s->data=p2->data;t->next=s;t=s;p2=p2->next;}else/*当两个数值相同时仅记录一个,并移动各自的指针*/{s=(LinkList*)malloc(sizeof(LinkList));s->data=p1->data;t->next=s;t=s;p1=p1->next;p2=p2->next;}}while(p1==NULL&&p2!=NULL)/*当有一个单链表已经比较完毕时,将另外一个单链表的剩余数值全部记录下来*/{s=(LinkList*)malloc(sizeof(LinkList));s->data=p2->data;t->next=s;t=s;p2=p2->next;}while(p2==NULL&&p1!=NULL){s=(LinkList*)malloc(sizeof(LinkList));s->data=p1->data;t->next=s;t=s;p1=p1->next;}t->next=NULL;return(L3);}LinkList*JiaoJi(LinkList*L1,LinkList*L2,LinkList*L4)/*求两集合的交集*/{LinkList*p1=L1->next,*p2,*s,*t;L4=(LinkList*)malloc(sizeof(LinkList));t=L4;while(p1!=NULL)/*在L1不为空时求交集,否则交集为空*/{p2=L2->next;while(p2!=NULL&&p2->datadata)/*按顺序比较各自数值的大小,若不相等就移动较小数值的指针*/{p2=p2->next;}if(p2!=NULL&&p2->data==p1->data)/*若相等就记录下来*/{s=(LinkList*)malloc(sizeof(LinkList));s->data=p1->data;t->next=s;t=s;}p1=p1->next;}t->next=NULL;return(L4);}LinkList*ChaJi(LinkList*L1,LinkList*L2,LinkList*L5)/*求两集合的差集*/{LinkList*p1=L1->next,*p2,*s,*t;L5=(LinkList*)malloc(sizeof(LinkList));t=L5;while(p1!=NULL)/*仅在L1不为空的情况下求差集,否则差集为空*/{p2=L2->next;while(p2!=NULL&&p2->datadata)/*若L2的数值小于L1的数值就移动指针*/{p2=p2->next;}if(!(p2!=NULL&&p2->data==p1->data))/*记录L1中有而L2中无的数值*/{s=(LinkList*)malloc(sizeof(LinkList));s->data=p1->data;t->next=s;t=s;}p1=p1->next;}t->next=NULL;return(L5);}voidmain()/*主函数,实现上述功能*/{chara[]={'2','4','5','8'};charb[]={'1','2','3','6','8','9'};LinkList*L1,*L2,*L3,*L4,*L5;L1=(LinkList*)malloc(sizeof(LinkList));L2=(LinkList*)malloc(sizeof(LinkList));L3=(LinkList*)malloc(sizeof(LinkList));printf("集合L1为:");L1=CreatList(L1,a,4);DispList(L1);printf("集合L2为:");L2=CreatList(L2,b,6);DispList(L2);L3=BingJi(L1,L2,L3);printf("两个集合的并集为:");DispList(L3);L4=(LinkList*)malloc(sizeof(LinkList));L4=JiaoJi(L1,L2,L4);printf("两个集合的交集为:");DispList(L4);L5=(LinkList*)malloc(sizeof(LinkList));L5=ChaJi(L1,L2,L5);printf("两个集合的差集为:");DispList(L5);}七、实验结果截图:八、实验任务分工: #include#include//////////////////////////////////#defineTRUE1#defineFALSE0#defineOK1#defineERROR0#defineOVERFLOW-2#defineINFEASIBLE-1#defineLIST_INIT_SIZE100#defineLISTINCREMENT10/////////////////定义节点/////////////////structSqList{char*elem;intlength;intlistsize;};//////////////////定义类/////////////////////////////classlist{public:intInitList(SqList&L);//初始化intListInsert(SqList&L,inti,chare);//元素插入函数intListDelete(SqList&L,inti,char&e);//元素删除函数intequal(chara1,chara2);//比较函数,元素之间比较intLocateElem(SqListL,chare);//比较函数,元素与表中元素比较intListLength(SqListL);//表的长度intGetElem(SqListL,inti,char&e);//获取元素voidFirstView()//登陆界面{cout<<"====================<<说明>>===================="<cout<<"1.输入的元素都为字符并且限定在{a,b,c….x,y,z}中."<cout<<"2.要正确输入,例如A={a,s,d,f,g,h}。"<cout<<"3.输入完成后按Enter键。"<cout<<"================================================"<cout<}voidUnionSet(SqListA,SqListB,SqList&C);//求集合A,B的并集CvoidInterSet(SqListA,SqListB,SqList&D);//求集合A,B的交集DvoidSubSet(SqListA,SqListB,SqList&E);//求集合A,B的差集E};//////////////////////////////////////////////////////////////////////////////////////////////////类实现///////////////////////////////////////intlist::InitList(SqList&L){L.elem=(char*)malloc(LIST_INIT_SIZE*sizeof(char));if(!L.elem)return(OVERFLOW);L.length=0;L.listsize=LIST_INIT_SIZE;returnOK;}intlist::ListInsert(SqList&L,inti,chare){char*newbase;if(i<1||i>L.length+1)returnERROR;if(L.length>=L.listsize){newbase=(char*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(char));if(!newbase)return(OVERFLOW);L.elem=newbase;L.listsize+=LISTINCREMENT;}char*q=&(L.elem[i-1]);for(char*p=&(L.elem[L.length-1]);p>=q;--p)*(p+1)=*p;*q=e;L.length++;returnOK;}intlist::ListDelete(SqList&L,inti,char&e){if(i<1||i>L.length)returnERROR;char*p=&(L.elem[i-1]);e=*p;char*q=L.elem+L.length-1;for(++p;p<=q;++p)*(p-1)=*p;--L.length;returnOK;}intlist::LocateElem(SqListL,chare){for(inti=1;i<=L.length;i++)if(!equal(e,L.elem[i-1]))returni;return0;}intlist::ListLength(SqListL){returnL.length;}intlist::GetElem(SqListL,inti,char&e){e=L.elem[i-1];returnOK;}//比较函数intlist::equal(chara1,chara2){if(a1==a2)return0;elseif(a1>a2)return1;elsereturn-1;}//求集合A,B的并集Cvoidlist::UnionSet(SqListA,SqListB,SqList&C){intn=0;chare;//将集合A插入集合Cfor(inti=1;i<=ListLength(A);i++){GetElem(A,i,e);ListInsert(C,++n,e);}//将集合B中的不同元素插入集合Cfor(i=1;i<=ListLength(B);i++){GetElem(B,i,e);intk=LocateElem(C,e);if(k==0)ListInsert(C,++n,e);}}//求集合A,B的交集Dvoidlist::InterSet(SqListA,SqListB,SqList&D){intn=0;for(inti=1;i<=ListLength(B);i++){chare;GetElem(B,i,e);//获得集合B中当前元素intk=LocateElem(A,e);//如果集合A中存在相同元素,则插入集合Dif(k>0)ListInsert(D,++n,e);}}//求集合A,B的差集Evoidlist::SubSet(SqListA,SqListB,SqList&E){intn=0;for(inti=1;i<=ListLength(A);i++){chare;GetElem(A,i,e);//获得集合A中当前元素intk=LocateElem(B,e);//如果集合B中不存在相同元素,则插入集合Eif(k==0)ListInsert(E,++n,e);}}////////////////////////////////////////////////////////////////////////////////////主函数/////////////////////////////////intmain(){listW;//定义list的对象WSqListA,B,C,D,E;//节点对象///////初始化//////////////////////W.InitList(A);////调用的函数初始化节点W.InitList(B);W.InitList(C);W.InitList(D);W.InitList(E);W.FirstView();///////////元素输入////////////////inti=0;charc;cout<<"PleaseinputSetA={";cin>>c;while((c)!=125)//连续输入字符,以'}'结束{if((c)!=44)W.ListInsert(A,++i,c);cin>>c;}cout<<"\n";i=0;cout<<"PleaseinputSetB={";cin>>c;while((c)!=125)//连续输入字符,以'}'结束{if((c)!=44)W.ListInsert(B,++i,c);cin>>c;}cout<<"\n\n";///////////////运算法则////////////////W.UnionSet(A,B,C);//并集W.InterSet(A,B,D);//交集W.SubSet(A,B,E);//差集//////////结果输出///////////////////cout<<"并集A∪B={";for(i=1;i<=W.ListLength(C);i++)//输出元素{cout<if(i!=W.ListLength(C))cout<<',';}cout<<"}\n\n";////---------------------------------cout<<"交集A∩B={";for(i=1;i<=W.ListLength(D);i++)//输出元素{cout<if(i!=W.ListLength(D))cout<<',';}cout<<"}\n\n";////-----------------------------------cout<<"差集A-B={";for(i=1;i<=W.ListLength(E);i++)//输出元素{cout<if(i!=W.ListLength(E))cout<<',';}cout<<"}\n\n";////-----------------------------------return0;} #include#include#includeusingnamespacestd;#defineElemTypechartypedefstructElemNode{ElemTypeelem;structElemNode*next;}ElemNode,*Set;//-------------FunctionList------------------------------//---------------函数原型--------------------------------intLengthOf(Setsrc);//返回一个集合的长度voidCreateSet(Setdest);//创建一个新的字母集合,限定a-zvoidEmptySet(Setdest);//清空一个集合,保留头结点voidDestroySet(Setdest);//销毁集合voidSortSet(Setdest);//对一个集合进行从小到大的排序voidDisplaySet(Setsrc);//打印集合的所有元素intExistElem(Setdest,ElemTypee);//判断元素是否存在于集合中voidDelElem(Setdest,ElemTypee);//删除集合中的一个元素一次voidAddElem(Setdest,ElemTypee);//在链表尾部追加一个元素voidContactSet(Setdest,Setsrc);//连接一个集合到另一个集合voidAddSet(Setdest,Setsrc1,Setsrc2);//集合并运算voidMulSet(Setdest,Setsrc1,Setsrc2);//集合交运算voidSubSet(Setdest,Setsrc1,Setsrc2);//集合差运算intExistSubset(Setdest,Setsrc);//子集判断voidNegSet(Setdest,Setsrc);//求补集 intmain(){Setdest=(Set)malloc(sizeof(ElemNode));Setsrc1=(Set)malloc(sizeof(ElemNode));Setsrc2=(Set)malloc(sizeof(ElemNode));dest->next=NULL;cout<<"输入两个集合:"<CreateSet(src1);CreateSet(src2);cout<cout<<"Set1=";DisplaySet(src1);cout<<"\t";cout<<"Set2=";DisplaySet(src2);cout<intitem;cout<<"1->集合并"<<""<<"2-
p=(LinkList*)malloc(sizeof(LinkList));
p->data=a[i];
r->next=p;
r=p;
}
r->next=NULL;
return(L);
voidDispList(LinkList*L)/*显示链表*/
inti=0;
LinkList*p;
p=L->next;
while(p!
=NULL)
printf("%c",p->data);
p=p->next;
printf("\n");
LinkList*BingJi(LinkList*L1,LinkList*L2,LinkList*L3)/*求两个集合的并集*/
LinkList*p1=L1->next,*p2=L2->next,*s,*t;
L3=(LinkList*)malloc(sizeof(LinkList));
t=L3;
while(p1!
=NULL&&p2!
=NULL)/*当L1和L2都不为空的情况下比较各自数值的大小*/
if(p1->datadata)/*先记录较小的数值,并移动其指针*/
s=(LinkList*)malloc(sizeof(LinkList));
s->data=p1->data;
t->next=s;
t=s;
p1=p1->next;
elseif(p1->data>p2->data)
s->data=p2->data;
p2=p2->next;
else/*当两个数值相同时仅记录一个,并移动各自的指针*/
while(p1==NULL&&p2!
=NULL)/*当有一个单链表已经比较完毕时,将另外一个单链表的剩余数值全部记录下来*/
while(p2==NULL&&p1!
t->next=NULL;
return(L3);
LinkList*JiaoJi(LinkList*L1,LinkList*L2,LinkList*L4)/*求两集合的交集*/
LinkList*p1=L1->next,*p2,*s,*t;
L4=(LinkList*)malloc(sizeof(LinkList));
t=L4;
=NULL)/*在L1不为空时求交集,否则交集为空*/
p2=L2->next;
while(p2!
=NULL&&p2->datadata)/*按顺序比较各自数值的大小,若不相等就移动较小数值的指针*/
if(p2!
=NULL&&p2->data==p1->data)/*若相等就记录下来*/
return(L4);
LinkList*ChaJi(LinkList*L1,LinkList*L2,LinkList*L5)/*求两集合的差集*/
L5=(LinkList*)malloc(sizeof(LinkList));
t=L5;
=NULL)/*仅在L1不为空的情况下求差集,否则差集为空*/
=NULL&&p2->datadata)/*若L2的数值小于L1的数值就移动指针*/
if(!
(p2!
=NULL&&p2->data==p1->data))/*记录L1中有而L2中无的数值*/
t->next=s;t=s;
return(L5);
voidmain()/*主函数,实现上述功能*/
chara[]={'2','4','5','8'};
charb[]={'1','2','3','6','8','9'};
LinkList*L1,*L2,*L3,*L4,*L5;
L1=(LinkList*)malloc(sizeof(LinkList));
L2=(LinkList*)malloc(sizeof(LinkList));
printf("集合L1为:
");
L1=CreatList(L1,a,4);
DispList(L1);
printf("集合L2为:
L2=CreatList(L2,b,6);
DispList(L2);
L3=BingJi(L1,L2,L3);
printf("两个集合的并集为:
DispList(L3);
L4=JiaoJi(L1,L2,L4);
printf("两个集合的交集为:
DispList(L4);
L5=ChaJi(L1,L2,L5);
printf("两个集合的差集为:
DispList(L5);
七、实验结果截图:
八、实验任务分工:
//////////////////////////////////
#defineTRUE1
#defineFALSE0
#defineOK1
#defineERROR0
#defineOVERFLOW-2
#defineINFEASIBLE-1
#defineLIST_INIT_SIZE100
#defineLISTINCREMENT10
/////////////////定义节点/////////////////
structSqList
char*elem;
intlength;
intlistsize;
};
//////////////////定义类/////////////////////////////
classlist
public:
intInitList(SqList&L);//初始化
intListInsert(SqList&L,inti,chare);//元素插入函数
intListDelete(SqList&L,inti,char&e);//元素删除函数
intequal(chara1,chara2);//比较函数,元素之间比较
intLocateElem(SqListL,chare);//比较函数,元素与表中元素比较
intListLength(SqListL);//表的长度
intGetElem(SqListL,inti,char&e);//获取元素
voidFirstView()//登陆界面
cout<<"====================<<说明>>===================="<cout<<"1.输入的元素都为字符并且限定在{a,b,c….x,y,z}中."<cout<<"2.要正确输入,例如A={a,s,d,f,g,h}。"<cout<<"3.输入完成后按Enter键。"<cout<<"================================================"<cout<}voidUnionSet(SqListA,SqListB,SqList&C);//求集合A,B的并集CvoidInterSet(SqListA,SqListB,SqList&D);//求集合A,B的交集DvoidSubSet(SqListA,SqListB,SqList&E);//求集合A,B的差集E};//////////////////////////////////////////////////////////////////////////////////////////////////类实现///////////////////////////////////////intlist::InitList(SqList&L){L.elem=(char*)malloc(LIST_INIT_SIZE*sizeof(char));if(!L.elem)return(OVERFLOW);L.length=0;L.listsize=LIST_INIT_SIZE;returnOK;}intlist::ListInsert(SqList&L,inti,chare){char*newbase;if(i<1||i>L.length+1)returnERROR;if(L.length>=L.listsize){newbase=(char*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(char));if(!newbase)return(OVERFLOW);L.elem=newbase;L.listsize+=LISTINCREMENT;}char*q=&(L.elem[i-1]);for(char*p=&(L.elem[L.length-1]);p>=q;--p)*(p+1)=*p;*q=e;L.length++;returnOK;}intlist::ListDelete(SqList&L,inti,char&e){if(i<1||i>L.length)returnERROR;char*p=&(L.elem[i-1]);e=*p;char*q=L.elem+L.length-1;for(++p;p<=q;++p)*(p-1)=*p;--L.length;returnOK;}intlist::LocateElem(SqListL,chare){for(inti=1;i<=L.length;i++)if(!equal(e,L.elem[i-1]))returni;return0;}intlist::ListLength(SqListL){returnL.length;}intlist::GetElem(SqListL,inti,char&e){e=L.elem[i-1];returnOK;}//比较函数intlist::equal(chara1,chara2){if(a1==a2)return0;elseif(a1>a2)return1;elsereturn-1;}//求集合A,B的并集Cvoidlist::UnionSet(SqListA,SqListB,SqList&C){intn=0;chare;//将集合A插入集合Cfor(inti=1;i<=ListLength(A);i++){GetElem(A,i,e);ListInsert(C,++n,e);}//将集合B中的不同元素插入集合Cfor(i=1;i<=ListLength(B);i++){GetElem(B,i,e);intk=LocateElem(C,e);if(k==0)ListInsert(C,++n,e);}}//求集合A,B的交集Dvoidlist::InterSet(SqListA,SqListB,SqList&D){intn=0;for(inti=1;i<=ListLength(B);i++){chare;GetElem(B,i,e);//获得集合B中当前元素intk=LocateElem(A,e);//如果集合A中存在相同元素,则插入集合Dif(k>0)ListInsert(D,++n,e);}}//求集合A,B的差集Evoidlist::SubSet(SqListA,SqListB,SqList&E){intn=0;for(inti=1;i<=ListLength(A);i++){chare;GetElem(A,i,e);//获得集合A中当前元素intk=LocateElem(B,e);//如果集合B中不存在相同元素,则插入集合Eif(k==0)ListInsert(E,++n,e);}}////////////////////////////////////////////////////////////////////////////////////主函数/////////////////////////////////intmain(){listW;//定义list的对象WSqListA,B,C,D,E;//节点对象///////初始化//////////////////////W.InitList(A);////调用的函数初始化节点W.InitList(B);W.InitList(C);W.InitList(D);W.InitList(E);W.FirstView();///////////元素输入////////////////inti=0;charc;cout<<"PleaseinputSetA={";cin>>c;while((c)!=125)//连续输入字符,以'}'结束{if((c)!=44)W.ListInsert(A,++i,c);cin>>c;}cout<<"\n";i=0;cout<<"PleaseinputSetB={";cin>>c;while((c)!=125)//连续输入字符,以'}'结束{if((c)!=44)W.ListInsert(B,++i,c);cin>>c;}cout<<"\n\n";///////////////运算法则////////////////W.UnionSet(A,B,C);//并集W.InterSet(A,B,D);//交集W.SubSet(A,B,E);//差集//////////结果输出///////////////////cout<<"并集A∪B={";for(i=1;i<=W.ListLength(C);i++)//输出元素{cout<if(i!=W.ListLength(C))cout<<',';}cout<<"}\n\n";////---------------------------------cout<<"交集A∩B={";for(i=1;i<=W.ListLength(D);i++)//输出元素{cout<if(i!=W.ListLength(D))cout<<',';}cout<<"}\n\n";////-----------------------------------cout<<"差集A-B={";for(i=1;i<=W.ListLength(E);i++)//输出元素{cout<if(i!=W.ListLength(E))cout<<',';}cout<<"}\n\n";////-----------------------------------return0;} #include#include#includeusingnamespacestd;#defineElemTypechartypedefstructElemNode{ElemTypeelem;structElemNode*next;}ElemNode,*Set;//-------------FunctionList------------------------------//---------------函数原型--------------------------------intLengthOf(Setsrc);//返回一个集合的长度voidCreateSet(Setdest);//创建一个新的字母集合,限定a-zvoidEmptySet(Setdest);//清空一个集合,保留头结点voidDestroySet(Setdest);//销毁集合voidSortSet(Setdest);//对一个集合进行从小到大的排序voidDisplaySet(Setsrc);//打印集合的所有元素intExistElem(Setdest,ElemTypee);//判断元素是否存在于集合中voidDelElem(Setdest,ElemTypee);//删除集合中的一个元素一次voidAddElem(Setdest,ElemTypee);//在链表尾部追加一个元素voidContactSet(Setdest,Setsrc);//连接一个集合到另一个集合voidAddSet(Setdest,Setsrc1,Setsrc2);//集合并运算voidMulSet(Setdest,Setsrc1,Setsrc2);//集合交运算voidSubSet(Setdest,Setsrc1,Setsrc2);//集合差运算intExistSubset(Setdest,Setsrc);//子集判断voidNegSet(Setdest,Setsrc);//求补集 intmain(){Setdest=(Set)malloc(sizeof(ElemNode));Setsrc1=(Set)malloc(sizeof(ElemNode));Setsrc2=(Set)malloc(sizeof(ElemNode));dest->next=NULL;cout<<"输入两个集合:"<CreateSet(src1);CreateSet(src2);cout<cout<<"Set1=";DisplaySet(src1);cout<<"\t";cout<<"Set2=";DisplaySet(src2);cout<intitem;cout<<"1->集合并"<<""<<"2-
cout<<"1.输入的元素都为字符并且限定在{a,b,c….x,y,z}中."<cout<<"2.要正确输入,例如A={a,s,d,f,g,h}。"<cout<<"3.输入完成后按Enter键。"<cout<<"================================================"<cout<}voidUnionSet(SqListA,SqListB,SqList&C);//求集合A,B的并集CvoidInterSet(SqListA,SqListB,SqList&D);//求集合A,B的交集DvoidSubSet(SqListA,SqListB,SqList&E);//求集合A,B的差集E};//////////////////////////////////////////////////////////////////////////////////////////////////类实现///////////////////////////////////////intlist::InitList(SqList&L){L.elem=(char*)malloc(LIST_INIT_SIZE*sizeof(char));if(!L.elem)return(OVERFLOW);L.length=0;L.listsize=LIST_INIT_SIZE;returnOK;}intlist::ListInsert(SqList&L,inti,chare){char*newbase;if(i<1||i>L.length+1)returnERROR;if(L.length>=L.listsize){newbase=(char*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(char));if(!newbase)return(OVERFLOW);L.elem=newbase;L.listsize+=LISTINCREMENT;}char*q=&(L.elem[i-1]);for(char*p=&(L.elem[L.length-1]);p>=q;--p)*(p+1)=*p;*q=e;L.length++;returnOK;}intlist::ListDelete(SqList&L,inti,char&e){if(i<1||i>L.length)returnERROR;char*p=&(L.elem[i-1]);e=*p;char*q=L.elem+L.length-1;for(++p;p<=q;++p)*(p-1)=*p;--L.length;returnOK;}intlist::LocateElem(SqListL,chare){for(inti=1;i<=L.length;i++)if(!equal(e,L.elem[i-1]))returni;return0;}intlist::ListLength(SqListL){returnL.length;}intlist::GetElem(SqListL,inti,char&e){e=L.elem[i-1];returnOK;}//比较函数intlist::equal(chara1,chara2){if(a1==a2)return0;elseif(a1>a2)return1;elsereturn-1;}//求集合A,B的并集Cvoidlist::UnionSet(SqListA,SqListB,SqList&C){intn=0;chare;//将集合A插入集合Cfor(inti=1;i<=ListLength(A);i++){GetElem(A,i,e);ListInsert(C,++n,e);}//将集合B中的不同元素插入集合Cfor(i=1;i<=ListLength(B);i++){GetElem(B,i,e);intk=LocateElem(C,e);if(k==0)ListInsert(C,++n,e);}}//求集合A,B的交集Dvoidlist::InterSet(SqListA,SqListB,SqList&D){intn=0;for(inti=1;i<=ListLength(B);i++){chare;GetElem(B,i,e);//获得集合B中当前元素intk=LocateElem(A,e);//如果集合A中存在相同元素,则插入集合Dif(k>0)ListInsert(D,++n,e);}}//求集合A,B的差集Evoidlist::SubSet(SqListA,SqListB,SqList&E){intn=0;for(inti=1;i<=ListLength(A);i++){chare;GetElem(A,i,e);//获得集合A中当前元素intk=LocateElem(B,e);//如果集合B中不存在相同元素,则插入集合Eif(k==0)ListInsert(E,++n,e);}}////////////////////////////////////////////////////////////////////////////////////主函数/////////////////////////////////intmain(){listW;//定义list的对象WSqListA,B,C,D,E;//节点对象///////初始化//////////////////////W.InitList(A);////调用的函数初始化节点W.InitList(B);W.InitList(C);W.InitList(D);W.InitList(E);W.FirstView();///////////元素输入////////////////inti=0;charc;cout<<"PleaseinputSetA={";cin>>c;while((c)!=125)//连续输入字符,以'}'结束{if((c)!=44)W.ListInsert(A,++i,c);cin>>c;}cout<<"\n";i=0;cout<<"PleaseinputSetB={";cin>>c;while((c)!=125)//连续输入字符,以'}'结束{if((c)!=44)W.ListInsert(B,++i,c);cin>>c;}cout<<"\n\n";///////////////运算法则////////////////W.UnionSet(A,B,C);//并集W.InterSet(A,B,D);//交集W.SubSet(A,B,E);//差集//////////结果输出///////////////////cout<<"并集A∪B={";for(i=1;i<=W.ListLength(C);i++)//输出元素{cout<if(i!=W.ListLength(C))cout<<',';}cout<<"}\n\n";////---------------------------------cout<<"交集A∩B={";for(i=1;i<=W.ListLength(D);i++)//输出元素{cout<if(i!=W.ListLength(D))cout<<',';}cout<<"}\n\n";////-----------------------------------cout<<"差集A-B={";for(i=1;i<=W.ListLength(E);i++)//输出元素{cout<if(i!=W.ListLength(E))cout<<',';}cout<<"}\n\n";////-----------------------------------return0;} #include#include#includeusingnamespacestd;#defineElemTypechartypedefstructElemNode{ElemTypeelem;structElemNode*next;}ElemNode,*Set;//-------------FunctionList------------------------------//---------------函数原型--------------------------------intLengthOf(Setsrc);//返回一个集合的长度voidCreateSet(Setdest);//创建一个新的字母集合,限定a-zvoidEmptySet(Setdest);//清空一个集合,保留头结点voidDestroySet(Setdest);//销毁集合voidSortSet(Setdest);//对一个集合进行从小到大的排序voidDisplaySet(Setsrc);//打印集合的所有元素intExistElem(Setdest,ElemTypee);//判断元素是否存在于集合中voidDelElem(Setdest,ElemTypee);//删除集合中的一个元素一次voidAddElem(Setdest,ElemTypee);//在链表尾部追加一个元素voidContactSet(Setdest,Setsrc);//连接一个集合到另一个集合voidAddSet(Setdest,Setsrc1,Setsrc2);//集合并运算voidMulSet(Setdest,Setsrc1,Setsrc2);//集合交运算voidSubSet(Setdest,Setsrc1,Setsrc2);//集合差运算intExistSubset(Setdest,Setsrc);//子集判断voidNegSet(Setdest,Setsrc);//求补集 intmain(){Setdest=(Set)malloc(sizeof(ElemNode));Setsrc1=(Set)malloc(sizeof(ElemNode));Setsrc2=(Set)malloc(sizeof(ElemNode));dest->next=NULL;cout<<"输入两个集合:"<CreateSet(src1);CreateSet(src2);cout<cout<<"Set1=";DisplaySet(src1);cout<<"\t";cout<<"Set2=";DisplaySet(src2);cout<intitem;cout<<"1->集合并"<<""<<"2-
cout<<"2.要正确输入,例如A={a,s,d,f,g,h}。
"<cout<<"3.输入完成后按Enter键。"<cout<<"================================================"<cout<}voidUnionSet(SqListA,SqListB,SqList&C);//求集合A,B的并集CvoidInterSet(SqListA,SqListB,SqList&D);//求集合A,B的交集DvoidSubSet(SqListA,SqListB,SqList&E);//求集合A,B的差集E};//////////////////////////////////////////////////////////////////////////////////////////////////类实现///////////////////////////////////////intlist::InitList(SqList&L){L.elem=(char*)malloc(LIST_INIT_SIZE*sizeof(char));if(!L.elem)return(OVERFLOW);L.length=0;L.listsize=LIST_INIT_SIZE;returnOK;}intlist::ListInsert(SqList&L,inti,chare){char*newbase;if(i<1||i>L.length+1)returnERROR;if(L.length>=L.listsize){newbase=(char*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(char));if(!newbase)return(OVERFLOW);L.elem=newbase;L.listsize+=LISTINCREMENT;}char*q=&(L.elem[i-1]);for(char*p=&(L.elem[L.length-1]);p>=q;--p)*(p+1)=*p;*q=e;L.length++;returnOK;}intlist::ListDelete(SqList&L,inti,char&e){if(i<1||i>L.length)returnERROR;char*p=&(L.elem[i-1]);e=*p;char*q=L.elem+L.length-1;for(++p;p<=q;++p)*(p-1)=*p;--L.length;returnOK;}intlist::LocateElem(SqListL,chare){for(inti=1;i<=L.length;i++)if(!equal(e,L.elem[i-1]))returni;return0;}intlist::ListLength(SqListL){returnL.length;}intlist::GetElem(SqListL,inti,char&e){e=L.elem[i-1];returnOK;}//比较函数intlist::equal(chara1,chara2){if(a1==a2)return0;elseif(a1>a2)return1;elsereturn-1;}//求集合A,B的并集Cvoidlist::UnionSet(SqListA,SqListB,SqList&C){intn=0;chare;//将集合A插入集合Cfor(inti=1;i<=ListLength(A);i++){GetElem(A,i,e);ListInsert(C,++n,e);}//将集合B中的不同元素插入集合Cfor(i=1;i<=ListLength(B);i++){GetElem(B,i,e);intk=LocateElem(C,e);if(k==0)ListInsert(C,++n,e);}}//求集合A,B的交集Dvoidlist::InterSet(SqListA,SqListB,SqList&D){intn=0;for(inti=1;i<=ListLength(B);i++){chare;GetElem(B,i,e);//获得集合B中当前元素intk=LocateElem(A,e);//如果集合A中存在相同元素,则插入集合Dif(k>0)ListInsert(D,++n,e);}}//求集合A,B的差集Evoidlist::SubSet(SqListA,SqListB,SqList&E){intn=0;for(inti=1;i<=ListLength(A);i++){chare;GetElem(A,i,e);//获得集合A中当前元素intk=LocateElem(B,e);//如果集合B中不存在相同元素,则插入集合Eif(k==0)ListInsert(E,++n,e);}}////////////////////////////////////////////////////////////////////////////////////主函数/////////////////////////////////intmain(){listW;//定义list的对象WSqListA,B,C,D,E;//节点对象///////初始化//////////////////////W.InitList(A);////调用的函数初始化节点W.InitList(B);W.InitList(C);W.InitList(D);W.InitList(E);W.FirstView();///////////元素输入////////////////inti=0;charc;cout<<"PleaseinputSetA={";cin>>c;while((c)!=125)//连续输入字符,以'}'结束{if((c)!=44)W.ListInsert(A,++i,c);cin>>c;}cout<<"\n";i=0;cout<<"PleaseinputSetB={";cin>>c;while((c)!=125)//连续输入字符,以'}'结束{if((c)!=44)W.ListInsert(B,++i,c);cin>>c;}cout<<"\n\n";///////////////运算法则////////////////W.UnionSet(A,B,C);//并集W.InterSet(A,B,D);//交集W.SubSet(A,B,E);//差集//////////结果输出///////////////////cout<<"并集A∪B={";for(i=1;i<=W.ListLength(C);i++)//输出元素{cout<if(i!=W.ListLength(C))cout<<',';}cout<<"}\n\n";////---------------------------------cout<<"交集A∩B={";for(i=1;i<=W.ListLength(D);i++)//输出元素{cout<if(i!=W.ListLength(D))cout<<',';}cout<<"}\n\n";////-----------------------------------cout<<"差集A-B={";for(i=1;i<=W.ListLength(E);i++)//输出元素{cout<if(i!=W.ListLength(E))cout<<',';}cout<<"}\n\n";////-----------------------------------return0;} #include#include#includeusingnamespacestd;#defineElemTypechartypedefstructElemNode{ElemTypeelem;structElemNode*next;}ElemNode,*Set;//-------------FunctionList------------------------------//---------------函数原型--------------------------------intLengthOf(Setsrc);//返回一个集合的长度voidCreateSet(Setdest);//创建一个新的字母集合,限定a-zvoidEmptySet(Setdest);//清空一个集合,保留头结点voidDestroySet(Setdest);//销毁集合voidSortSet(Setdest);//对一个集合进行从小到大的排序voidDisplaySet(Setsrc);//打印集合的所有元素intExistElem(Setdest,ElemTypee);//判断元素是否存在于集合中voidDelElem(Setdest,ElemTypee);//删除集合中的一个元素一次voidAddElem(Setdest,ElemTypee);//在链表尾部追加一个元素voidContactSet(Setdest,Setsrc);//连接一个集合到另一个集合voidAddSet(Setdest,Setsrc1,Setsrc2);//集合并运算voidMulSet(Setdest,Setsrc1,Setsrc2);//集合交运算voidSubSet(Setdest,Setsrc1,Setsrc2);//集合差运算intExistSubset(Setdest,Setsrc);//子集判断voidNegSet(Setdest,Setsrc);//求补集 intmain(){Setdest=(Set)malloc(sizeof(ElemNode));Setsrc1=(Set)malloc(sizeof(ElemNode));Setsrc2=(Set)malloc(sizeof(ElemNode));dest->next=NULL;cout<<"输入两个集合:"<CreateSet(src1);CreateSet(src2);cout<cout<<"Set1=";DisplaySet(src1);cout<<"\t";cout<<"Set2=";DisplaySet(src2);cout<intitem;cout<<"1->集合并"<<""<<"2-
cout<<"3.输入完成后按Enter键。
"<cout<<"================================================"<cout<}voidUnionSet(SqListA,SqListB,SqList&C);//求集合A,B的并集CvoidInterSet(SqListA,SqListB,SqList&D);//求集合A,B的交集DvoidSubSet(SqListA,SqListB,SqList&E);//求集合A,B的差集E};//////////////////////////////////////////////////////////////////////////////////////////////////类实现///////////////////////////////////////intlist::InitList(SqList&L){L.elem=(char*)malloc(LIST_INIT_SIZE*sizeof(char));if(!L.elem)return(OVERFLOW);L.length=0;L.listsize=LIST_INIT_SIZE;returnOK;}intlist::ListInsert(SqList&L,inti,chare){char*newbase;if(i<1||i>L.length+1)returnERROR;if(L.length>=L.listsize){newbase=(char*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(char));if(!newbase)return(OVERFLOW);L.elem=newbase;L.listsize+=LISTINCREMENT;}char*q=&(L.elem[i-1]);for(char*p=&(L.elem[L.length-1]);p>=q;--p)*(p+1)=*p;*q=e;L.length++;returnOK;}intlist::ListDelete(SqList&L,inti,char&e){if(i<1||i>L.length)returnERROR;char*p=&(L.elem[i-1]);e=*p;char*q=L.elem+L.length-1;for(++p;p<=q;++p)*(p-1)=*p;--L.length;returnOK;}intlist::LocateElem(SqListL,chare){for(inti=1;i<=L.length;i++)if(!equal(e,L.elem[i-1]))returni;return0;}intlist::ListLength(SqListL){returnL.length;}intlist::GetElem(SqListL,inti,char&e){e=L.elem[i-1];returnOK;}//比较函数intlist::equal(chara1,chara2){if(a1==a2)return0;elseif(a1>a2)return1;elsereturn-1;}//求集合A,B的并集Cvoidlist::UnionSet(SqListA,SqListB,SqList&C){intn=0;chare;//将集合A插入集合Cfor(inti=1;i<=ListLength(A);i++){GetElem(A,i,e);ListInsert(C,++n,e);}//将集合B中的不同元素插入集合Cfor(i=1;i<=ListLength(B);i++){GetElem(B,i,e);intk=LocateElem(C,e);if(k==0)ListInsert(C,++n,e);}}//求集合A,B的交集Dvoidlist::InterSet(SqListA,SqListB,SqList&D){intn=0;for(inti=1;i<=ListLength(B);i++){chare;GetElem(B,i,e);//获得集合B中当前元素intk=LocateElem(A,e);//如果集合A中存在相同元素,则插入集合Dif(k>0)ListInsert(D,++n,e);}}//求集合A,B的差集Evoidlist::SubSet(SqListA,SqListB,SqList&E){intn=0;for(inti=1;i<=ListLength(A);i++){chare;GetElem(A,i,e);//获得集合A中当前元素intk=LocateElem(B,e);//如果集合B中不存在相同元素,则插入集合Eif(k==0)ListInsert(E,++n,e);}}////////////////////////////////////////////////////////////////////////////////////主函数/////////////////////////////////intmain(){listW;//定义list的对象WSqListA,B,C,D,E;//节点对象///////初始化//////////////////////W.InitList(A);////调用的函数初始化节点W.InitList(B);W.InitList(C);W.InitList(D);W.InitList(E);W.FirstView();///////////元素输入////////////////inti=0;charc;cout<<"PleaseinputSetA={";cin>>c;while((c)!=125)//连续输入字符,以'}'结束{if((c)!=44)W.ListInsert(A,++i,c);cin>>c;}cout<<"\n";i=0;cout<<"PleaseinputSetB={";cin>>c;while((c)!=125)//连续输入字符,以'}'结束{if((c)!=44)W.ListInsert(B,++i,c);cin>>c;}cout<<"\n\n";///////////////运算法则////////////////W.UnionSet(A,B,C);//并集W.InterSet(A,B,D);//交集W.SubSet(A,B,E);//差集//////////结果输出///////////////////cout<<"并集A∪B={";for(i=1;i<=W.ListLength(C);i++)//输出元素{cout<if(i!=W.ListLength(C))cout<<',';}cout<<"}\n\n";////---------------------------------cout<<"交集A∩B={";for(i=1;i<=W.ListLength(D);i++)//输出元素{cout<if(i!=W.ListLength(D))cout<<',';}cout<<"}\n\n";////-----------------------------------cout<<"差集A-B={";for(i=1;i<=W.ListLength(E);i++)//输出元素{cout<if(i!=W.ListLength(E))cout<<',';}cout<<"}\n\n";////-----------------------------------return0;} #include#include#includeusingnamespacestd;#defineElemTypechartypedefstructElemNode{ElemTypeelem;structElemNode*next;}ElemNode,*Set;//-------------FunctionList------------------------------//---------------函数原型--------------------------------intLengthOf(Setsrc);//返回一个集合的长度voidCreateSet(Setdest);//创建一个新的字母集合,限定a-zvoidEmptySet(Setdest);//清空一个集合,保留头结点voidDestroySet(Setdest);//销毁集合voidSortSet(Setdest);//对一个集合进行从小到大的排序voidDisplaySet(Setsrc);//打印集合的所有元素intExistElem(Setdest,ElemTypee);//判断元素是否存在于集合中voidDelElem(Setdest,ElemTypee);//删除集合中的一个元素一次voidAddElem(Setdest,ElemTypee);//在链表尾部追加一个元素voidContactSet(Setdest,Setsrc);//连接一个集合到另一个集合voidAddSet(Setdest,Setsrc1,Setsrc2);//集合并运算voidMulSet(Setdest,Setsrc1,Setsrc2);//集合交运算voidSubSet(Setdest,Setsrc1,Setsrc2);//集合差运算intExistSubset(Setdest,Setsrc);//子集判断voidNegSet(Setdest,Setsrc);//求补集 intmain(){Setdest=(Set)malloc(sizeof(ElemNode));Setsrc1=(Set)malloc(sizeof(ElemNode));Setsrc2=(Set)malloc(sizeof(ElemNode));dest->next=NULL;cout<<"输入两个集合:"<CreateSet(src1);CreateSet(src2);cout<cout<<"Set1=";DisplaySet(src1);cout<<"\t";cout<<"Set2=";DisplaySet(src2);cout<intitem;cout<<"1->集合并"<<""<<"2-
cout<<"================================================"<cout<}voidUnionSet(SqListA,SqListB,SqList&C);//求集合A,B的并集CvoidInterSet(SqListA,SqListB,SqList&D);//求集合A,B的交集DvoidSubSet(SqListA,SqListB,SqList&E);//求集合A,B的差集E};//////////////////////////////////////////////////////////////////////////////////////////////////类实现///////////////////////////////////////intlist::InitList(SqList&L){L.elem=(char*)malloc(LIST_INIT_SIZE*sizeof(char));if(!L.elem)return(OVERFLOW);L.length=0;L.listsize=LIST_INIT_SIZE;returnOK;}intlist::ListInsert(SqList&L,inti,chare){char*newbase;if(i<1||i>L.length+1)returnERROR;if(L.length>=L.listsize){newbase=(char*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(char));if(!newbase)return(OVERFLOW);L.elem=newbase;L.listsize+=LISTINCREMENT;}char*q=&(L.elem[i-1]);for(char*p=&(L.elem[L.length-1]);p>=q;--p)*(p+1)=*p;*q=e;L.length++;returnOK;}intlist::ListDelete(SqList&L,inti,char&e){if(i<1||i>L.length)returnERROR;char*p=&(L.elem[i-1]);e=*p;char*q=L.elem+L.length-1;for(++p;p<=q;++p)*(p-1)=*p;--L.length;returnOK;}intlist::LocateElem(SqListL,chare){for(inti=1;i<=L.length;i++)if(!equal(e,L.elem[i-1]))returni;return0;}intlist::ListLength(SqListL){returnL.length;}intlist::GetElem(SqListL,inti,char&e){e=L.elem[i-1];returnOK;}//比较函数intlist::equal(chara1,chara2){if(a1==a2)return0;elseif(a1>a2)return1;elsereturn-1;}//求集合A,B的并集Cvoidlist::UnionSet(SqListA,SqListB,SqList&C){intn=0;chare;//将集合A插入集合Cfor(inti=1;i<=ListLength(A);i++){GetElem(A,i,e);ListInsert(C,++n,e);}//将集合B中的不同元素插入集合Cfor(i=1;i<=ListLength(B);i++){GetElem(B,i,e);intk=LocateElem(C,e);if(k==0)ListInsert(C,++n,e);}}//求集合A,B的交集Dvoidlist::InterSet(SqListA,SqListB,SqList&D){intn=0;for(inti=1;i<=ListLength(B);i++){chare;GetElem(B,i,e);//获得集合B中当前元素intk=LocateElem(A,e);//如果集合A中存在相同元素,则插入集合Dif(k>0)ListInsert(D,++n,e);}}//求集合A,B的差集Evoidlist::SubSet(SqListA,SqListB,SqList&E){intn=0;for(inti=1;i<=ListLength(A);i++){chare;GetElem(A,i,e);//获得集合A中当前元素intk=LocateElem(B,e);//如果集合B中不存在相同元素,则插入集合Eif(k==0)ListInsert(E,++n,e);}}////////////////////////////////////////////////////////////////////////////////////主函数/////////////////////////////////intmain(){listW;//定义list的对象WSqListA,B,C,D,E;//节点对象///////初始化//////////////////////W.InitList(A);////调用的函数初始化节点W.InitList(B);W.InitList(C);W.InitList(D);W.InitList(E);W.FirstView();///////////元素输入////////////////inti=0;charc;cout<<"PleaseinputSetA={";cin>>c;while((c)!=125)//连续输入字符,以'}'结束{if((c)!=44)W.ListInsert(A,++i,c);cin>>c;}cout<<"\n";i=0;cout<<"PleaseinputSetB={";cin>>c;while((c)!=125)//连续输入字符,以'}'结束{if((c)!=44)W.ListInsert(B,++i,c);cin>>c;}cout<<"\n\n";///////////////运算法则////////////////W.UnionSet(A,B,C);//并集W.InterSet(A,B,D);//交集W.SubSet(A,B,E);//差集//////////结果输出///////////////////cout<<"并集A∪B={";for(i=1;i<=W.ListLength(C);i++)//输出元素{cout<if(i!=W.ListLength(C))cout<<',';}cout<<"}\n\n";////---------------------------------cout<<"交集A∩B={";for(i=1;i<=W.ListLength(D);i++)//输出元素{cout<if(i!=W.ListLength(D))cout<<',';}cout<<"}\n\n";////-----------------------------------cout<<"差集A-B={";for(i=1;i<=W.ListLength(E);i++)//输出元素{cout<if(i!=W.ListLength(E))cout<<',';}cout<<"}\n\n";////-----------------------------------return0;} #include#include#includeusingnamespacestd;#defineElemTypechartypedefstructElemNode{ElemTypeelem;structElemNode*next;}ElemNode,*Set;//-------------FunctionList------------------------------//---------------函数原型--------------------------------intLengthOf(Setsrc);//返回一个集合的长度voidCreateSet(Setdest);//创建一个新的字母集合,限定a-zvoidEmptySet(Setdest);//清空一个集合,保留头结点voidDestroySet(Setdest);//销毁集合voidSortSet(Setdest);//对一个集合进行从小到大的排序voidDisplaySet(Setsrc);//打印集合的所有元素intExistElem(Setdest,ElemTypee);//判断元素是否存在于集合中voidDelElem(Setdest,ElemTypee);//删除集合中的一个元素一次voidAddElem(Setdest,ElemTypee);//在链表尾部追加一个元素voidContactSet(Setdest,Setsrc);//连接一个集合到另一个集合voidAddSet(Setdest,Setsrc1,Setsrc2);//集合并运算voidMulSet(Setdest,Setsrc1,Setsrc2);//集合交运算voidSubSet(Setdest,Setsrc1,Setsrc2);//集合差运算intExistSubset(Setdest,Setsrc);//子集判断voidNegSet(Setdest,Setsrc);//求补集 intmain(){Setdest=(Set)malloc(sizeof(ElemNode));Setsrc1=(Set)malloc(sizeof(ElemNode));Setsrc2=(Set)malloc(sizeof(ElemNode));dest->next=NULL;cout<<"输入两个集合:"<CreateSet(src1);CreateSet(src2);cout<cout<<"Set1=";DisplaySet(src1);cout<<"\t";cout<<"Set2=";DisplaySet(src2);cout<intitem;cout<<"1->集合并"<<""<<"2-
cout<}voidUnionSet(SqListA,SqListB,SqList&C);//求集合A,B的并集CvoidInterSet(SqListA,SqListB,SqList&D);//求集合A,B的交集DvoidSubSet(SqListA,SqListB,SqList&E);//求集合A,B的差集E};//////////////////////////////////////////////////////////////////////////////////////////////////类实现///////////////////////////////////////intlist::InitList(SqList&L){L.elem=(char*)malloc(LIST_INIT_SIZE*sizeof(char));if(!L.elem)return(OVERFLOW);L.length=0;L.listsize=LIST_INIT_SIZE;returnOK;}intlist::ListInsert(SqList&L,inti,chare){char*newbase;if(i<1||i>L.length+1)returnERROR;if(L.length>=L.listsize){newbase=(char*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(char));if(!newbase)return(OVERFLOW);L.elem=newbase;L.listsize+=LISTINCREMENT;}char*q=&(L.elem[i-1]);for(char*p=&(L.elem[L.length-1]);p>=q;--p)*(p+1)=*p;*q=e;L.length++;returnOK;}intlist::ListDelete(SqList&L,inti,char&e){if(i<1||i>L.length)returnERROR;char*p=&(L.elem[i-1]);e=*p;char*q=L.elem+L.length-1;for(++p;p<=q;++p)*(p-1)=*p;--L.length;returnOK;}intlist::LocateElem(SqListL,chare){for(inti=1;i<=L.length;i++)if(!equal(e,L.elem[i-1]))returni;return0;}intlist::ListLength(SqListL){returnL.length;}intlist::GetElem(SqListL,inti,char&e){e=L.elem[i-1];returnOK;}//比较函数intlist::equal(chara1,chara2){if(a1==a2)return0;elseif(a1>a2)return1;elsereturn-1;}//求集合A,B的并集Cvoidlist::UnionSet(SqListA,SqListB,SqList&C){intn=0;chare;//将集合A插入集合Cfor(inti=1;i<=ListLength(A);i++){GetElem(A,i,e);ListInsert(C,++n,e);}//将集合B中的不同元素插入集合Cfor(i=1;i<=ListLength(B);i++){GetElem(B,i,e);intk=LocateElem(C,e);if(k==0)ListInsert(C,++n,e);}}//求集合A,B的交集Dvoidlist::InterSet(SqListA,SqListB,SqList&D){intn=0;for(inti=1;i<=ListLength(B);i++){chare;GetElem(B,i,e);//获得集合B中当前元素intk=LocateElem(A,e);//如果集合A中存在相同元素,则插入集合Dif(k>0)ListInsert(D,++n,e);}}//求集合A,B的差集Evoidlist::SubSet(SqListA,SqListB,SqList&E){intn=0;for(inti=1;i<=ListLength(A);i++){chare;GetElem(A,i,e);//获得集合A中当前元素intk=LocateElem(B,e);//如果集合B中不存在相同元素,则插入集合Eif(k==0)ListInsert(E,++n,e);}}////////////////////////////////////////////////////////////////////////////////////主函数/////////////////////////////////intmain(){listW;//定义list的对象WSqListA,B,C,D,E;//节点对象///////初始化//////////////////////W.InitList(A);////调用的函数初始化节点W.InitList(B);W.InitList(C);W.InitList(D);W.InitList(E);W.FirstView();///////////元素输入////////////////inti=0;charc;cout<<"PleaseinputSetA={";cin>>c;while((c)!=125)//连续输入字符,以'}'结束{if((c)!=44)W.ListInsert(A,++i,c);cin>>c;}cout<<"\n";i=0;cout<<"PleaseinputSetB={";cin>>c;while((c)!=125)//连续输入字符,以'}'结束{if((c)!=44)W.ListInsert(B,++i,c);cin>>c;}cout<<"\n\n";///////////////运算法则////////////////W.UnionSet(A,B,C);//并集W.InterSet(A,B,D);//交集W.SubSet(A,B,E);//差集//////////结果输出///////////////////cout<<"并集A∪B={";for(i=1;i<=W.ListLength(C);i++)//输出元素{cout<if(i!=W.ListLength(C))cout<<',';}cout<<"}\n\n";////---------------------------------cout<<"交集A∩B={";for(i=1;i<=W.ListLength(D);i++)//输出元素{cout<if(i!=W.ListLength(D))cout<<',';}cout<<"}\n\n";////-----------------------------------cout<<"差集A-B={";for(i=1;i<=W.ListLength(E);i++)//输出元素{cout<if(i!=W.ListLength(E))cout<<',';}cout<<"}\n\n";////-----------------------------------return0;} #include#include#includeusingnamespacestd;#defineElemTypechartypedefstructElemNode{ElemTypeelem;structElemNode*next;}ElemNode,*Set;//-------------FunctionList------------------------------//---------------函数原型--------------------------------intLengthOf(Setsrc);//返回一个集合的长度voidCreateSet(Setdest);//创建一个新的字母集合,限定a-zvoidEmptySet(Setdest);//清空一个集合,保留头结点voidDestroySet(Setdest);//销毁集合voidSortSet(Setdest);//对一个集合进行从小到大的排序voidDisplaySet(Setsrc);//打印集合的所有元素intExistElem(Setdest,ElemTypee);//判断元素是否存在于集合中voidDelElem(Setdest,ElemTypee);//删除集合中的一个元素一次voidAddElem(Setdest,ElemTypee);//在链表尾部追加一个元素voidContactSet(Setdest,Setsrc);//连接一个集合到另一个集合voidAddSet(Setdest,Setsrc1,Setsrc2);//集合并运算voidMulSet(Setdest,Setsrc1,Setsrc2);//集合交运算voidSubSet(Setdest,Setsrc1,Setsrc2);//集合差运算intExistSubset(Setdest,Setsrc);//子集判断voidNegSet(Setdest,Setsrc);//求补集 intmain(){Setdest=(Set)malloc(sizeof(ElemNode));Setsrc1=(Set)malloc(sizeof(ElemNode));Setsrc2=(Set)malloc(sizeof(ElemNode));dest->next=NULL;cout<<"输入两个集合:"<CreateSet(src1);CreateSet(src2);cout<cout<<"Set1=";DisplaySet(src1);cout<<"\t";cout<<"Set2=";DisplaySet(src2);cout<intitem;cout<<"1->集合并"<<""<<"2-
voidUnionSet(SqListA,SqListB,SqList&C);//求集合A,B的并集C
voidInterSet(SqListA,SqListB,SqList&D);//求集合A,B的交集D
voidSubSet(SqListA,SqListB,SqList&E);//求集合A,B的差集E
////////////////////////////////////////////////////////////////////////
//////////////////////////类实现///////////////////////////////////////
intlist:
:
InitList(SqList&L)
L.elem=(char*)malloc(LIST_INIT_SIZE*sizeof(char));
L.elem)return(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
returnOK;
ListInsert(SqList&L,inti,chare)
char*newbase;
if(i<1||i>L.length+1)returnERROR;
if(L.length>=L.listsize)
newbase=(char*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(char));
newbase)return(OVERFLOW);
L.elem=newbase;
L.listsize+=LISTINCREMENT;
char*q=&(L.elem[i-1]);
for(char*p=&(L.elem[L.length-1]);p>=q;--p)*(p+1)=*p;
*q=e;
L.length++;
ListDelete(SqList&L,inti,char&e)
if(i<1||i>L.length)returnERROR;
char*p=&(L.elem[i-1]);
e=*p;
char*q=L.elem+L.length-1;
for(++p;p<=q;++p)
*(p-1)=*p;
--L.length;
LocateElem(SqListL,chare)
for(inti=1;i<=L.length;i++)
equal(e,L.elem[i-1]))
returni;
return0;
ListLength(SqListL)
returnL.length;
GetElem(SqListL,inti,char&e)
e=L.elem[i-1];
//比较函数
equal(chara1,chara2)
if(a1==a2)
elseif(a1>a2)
return1;
else
return-1;
//求集合A,B的并集C
voidlist:
UnionSet(SqListA,SqListB,SqList&C)
intn=0;
chare;
//将集合A插入集合C
for(inti=1;i<=ListLength(A);i++)
GetElem(A,i,e);
ListInsert(C,++n,e);
//将集合B中的不同元素插入集合C
for(i=1;i<=ListLength(B);i++)
GetElem(B,i,e);
intk=LocateElem(C,e);
if(k==0)ListInsert(C,++n,e);
//求集合A,B的交集D
InterSet(SqListA,SqListB,SqList&D)
for(inti=1;i<=ListLength(B);i++)
GetElem(B,i,e);//获得集合B中当前元素
intk=LocateElem(A,e);
//如果集合A中存在相同元素,则插入集合D
if(k>0)ListInsert(D,++n,e);
//求集合A,B的差集E
SubSet(SqListA,SqListB,SqList&E)
GetElem(A,i,e);//获得集合A中当前元素
intk=LocateElem(B,e);
//如果集合B中不存在相同元素,则插入集合E
if(k==0)ListInsert(E,++n,e);
///////////////////////////////////////////////////////////////
/////////////////////主函数/////////////////////////////////
intmain()
listW;//定义list的对象W
SqListA,B,C,D,E;//节点对象
///////初始化//////////////////////
W.InitList(A);////调用的函数初始化节点
W.InitList(B);
W.InitList(C);
W.InitList(D);
W.InitList(E);
W.FirstView();
///////////元素输入////////////////
charc;
cout<<"PleaseinputSetA={";
cin>>c;
while((c)!
=125)//连续输入字符,以'}'结束
if((c)!
=44)W.ListInsert(A,++i,c);
cout<<"\n";
i=0;
cout<<"PleaseinputSetB={";
=44)W.ListInsert(B,++i,c);
cout<<"\n\n";
///////////////运算法则////////////////
W.UnionSet(A,B,C);//并集
W.InterSet(A,B,D);//交集
W.SubSet(A,B,E);//差集
//////////结果输出///////////////////
cout<<"并集A∪B={";
for(i=1;i<=W.ListLength(C);i++)//输出元素
cout<if(i!=W.ListLength(C))cout<<',';}cout<<"}\n\n";////---------------------------------cout<<"交集A∩B={";for(i=1;i<=W.ListLength(D);i++)//输出元素{cout<if(i!=W.ListLength(D))cout<<',';}cout<<"}\n\n";////-----------------------------------cout<<"差集A-B={";for(i=1;i<=W.ListLength(E);i++)//输出元素{cout<if(i!=W.ListLength(E))cout<<',';}cout<<"}\n\n";////-----------------------------------return0;} #include#include#includeusingnamespacestd;#defineElemTypechartypedefstructElemNode{ElemTypeelem;structElemNode*next;}ElemNode,*Set;//-------------FunctionList------------------------------//---------------函数原型--------------------------------intLengthOf(Setsrc);//返回一个集合的长度voidCreateSet(Setdest);//创建一个新的字母集合,限定a-zvoidEmptySet(Setdest);//清空一个集合,保留头结点voidDestroySet(Setdest);//销毁集合voidSortSet(Setdest);//对一个集合进行从小到大的排序voidDisplaySet(Setsrc);//打印集合的所有元素intExistElem(Setdest,ElemTypee);//判断元素是否存在于集合中voidDelElem(Setdest,ElemTypee);//删除集合中的一个元素一次voidAddElem(Setdest,ElemTypee);//在链表尾部追加一个元素voidContactSet(Setdest,Setsrc);//连接一个集合到另一个集合voidAddSet(Setdest,Setsrc1,Setsrc2);//集合并运算voidMulSet(Setdest,Setsrc1,Setsrc2);//集合交运算voidSubSet(Setdest,Setsrc1,Setsrc2);//集合差运算intExistSubset(Setdest,Setsrc);//子集判断voidNegSet(Setdest,Setsrc);//求补集 intmain(){Setdest=(Set)malloc(sizeof(ElemNode));Setsrc1=(Set)malloc(sizeof(ElemNode));Setsrc2=(Set)malloc(sizeof(ElemNode));dest->next=NULL;cout<<"输入两个集合:"<CreateSet(src1);CreateSet(src2);cout<cout<<"Set1=";DisplaySet(src1);cout<<"\t";cout<<"Set2=";DisplaySet(src2);cout<intitem;cout<<"1->集合并"<<""<<"2-
if(i!
=W.ListLength(C))cout<<',';
cout<<"}\n\n";
////---------------------------------
cout<<"交集A∩B={";
for(i=1;i<=W.ListLength(D);i++)//输出元素
cout<if(i!=W.ListLength(D))cout<<',';}cout<<"}\n\n";////-----------------------------------cout<<"差集A-B={";for(i=1;i<=W.ListLength(E);i++)//输出元素{cout<if(i!=W.ListLength(E))cout<<',';}cout<<"}\n\n";////-----------------------------------return0;} #include#include#includeusingnamespacestd;#defineElemTypechartypedefstructElemNode{ElemTypeelem;structElemNode*next;}ElemNode,*Set;//-------------FunctionList------------------------------//---------------函数原型--------------------------------intLengthOf(Setsrc);//返回一个集合的长度voidCreateSet(Setdest);//创建一个新的字母集合,限定a-zvoidEmptySet(Setdest);//清空一个集合,保留头结点voidDestroySet(Setdest);//销毁集合voidSortSet(Setdest);//对一个集合进行从小到大的排序voidDisplaySet(Setsrc);//打印集合的所有元素intExistElem(Setdest,ElemTypee);//判断元素是否存在于集合中voidDelElem(Setdest,ElemTypee);//删除集合中的一个元素一次voidAddElem(Setdest,ElemTypee);//在链表尾部追加一个元素voidContactSet(Setdest,Setsrc);//连接一个集合到另一个集合voidAddSet(Setdest,Setsrc1,Setsrc2);//集合并运算voidMulSet(Setdest,Setsrc1,Setsrc2);//集合交运算voidSubSet(Setdest,Setsrc1,Setsrc2);//集合差运算intExistSubset(Setdest,Setsrc);//子集判断voidNegSet(Setdest,Setsrc);//求补集 intmain(){Setdest=(Set)malloc(sizeof(ElemNode));Setsrc1=(Set)malloc(sizeof(ElemNode));Setsrc2=(Set)malloc(sizeof(ElemNode));dest->next=NULL;cout<<"输入两个集合:"<CreateSet(src1);CreateSet(src2);cout<cout<<"Set1=";DisplaySet(src1);cout<<"\t";cout<<"Set2=";DisplaySet(src2);cout<intitem;cout<<"1->集合并"<<""<<"2-
=W.ListLength(D))cout<<',';
////-----------------------------------
cout<<"差集A-B={";
for(i=1;i<=W.ListLength(E);i++)//输出元素
cout<if(i!=W.ListLength(E))cout<<',';}cout<<"}\n\n";////-----------------------------------return0;} #include#include#includeusingnamespacestd;#defineElemTypechartypedefstructElemNode{ElemTypeelem;structElemNode*next;}ElemNode,*Set;//-------------FunctionList------------------------------//---------------函数原型--------------------------------intLengthOf(Setsrc);//返回一个集合的长度voidCreateSet(Setdest);//创建一个新的字母集合,限定a-zvoidEmptySet(Setdest);//清空一个集合,保留头结点voidDestroySet(Setdest);//销毁集合voidSortSet(Setdest);//对一个集合进行从小到大的排序voidDisplaySet(Setsrc);//打印集合的所有元素intExistElem(Setdest,ElemTypee);//判断元素是否存在于集合中voidDelElem(Setdest,ElemTypee);//删除集合中的一个元素一次voidAddElem(Setdest,ElemTypee);//在链表尾部追加一个元素voidContactSet(Setdest,Setsrc);//连接一个集合到另一个集合voidAddSet(Setdest,Setsrc1,Setsrc2);//集合并运算voidMulSet(Setdest,Setsrc1,Setsrc2);//集合交运算voidSubSet(Setdest,Setsrc1,Setsrc2);//集合差运算intExistSubset(Setdest,Setsrc);//子集判断voidNegSet(Setdest,Setsrc);//求补集 intmain(){Setdest=(Set)malloc(sizeof(ElemNode));Setsrc1=(Set)malloc(sizeof(ElemNode));Setsrc2=(Set)malloc(sizeof(ElemNode));dest->next=NULL;cout<<"输入两个集合:"<CreateSet(src1);CreateSet(src2);cout<cout<<"Set1=";DisplaySet(src1);cout<<"\t";cout<<"Set2=";DisplaySet(src2);cout<intitem;cout<<"1->集合并"<<""<<"2-
=W.ListLength(E))cout<<',';
usingnamespacestd;
#defineElemTypechar
typedefstructElemNode
ElemTypeelem;
structElemNode*next;
}ElemNode,*Set;
//-------------FunctionList------------------------------
//---------------函数原型--------------------------------
intLengthOf(Setsrc);//返回一个集合的长度
voidCreateSet(Setdest);//创建一个新的字母集合,限定a-z
voidEmptySet(Setdest);//清空一个集合,保留头结点
voidDestroySet(Setdest);//销毁集合
voidSortSet(Setdest);//对一个集合进行从小到大的排序
voidDisplaySet(Setsrc);//打印集合的所有元素
intExistElem(Setdest,ElemTypee);//判断元素是否存在于集合中
voidDelElem(Setdest,ElemTypee);//删除集合中的一个元素一次
voidAddElem(Setdest,ElemTypee);//在链表尾部追加一个元素
voidContactSet(Setdest,Setsrc);//连接一个集合到另一个集合
voidAddSet(Setdest,Setsrc1,Setsrc2);//集合并运算
voidMulSet(Setdest,Setsrc1,Setsrc2);//集合交运算
voidSubSet(Setdest,Setsrc1,Setsrc2);//集合差运算
intExistSubset(Setdest,Setsrc);//子集判断
voidNegSet(Setdest,Setsrc);//求补集
Setdest=(Set)malloc(sizeof(ElemNode));
Setsrc1=(Set)malloc(sizeof(ElemNode));
Setsrc2=(Set)malloc(sizeof(ElemNode));
dest->next=NULL;
cout<<"输入两个集合:
"<CreateSet(src1);CreateSet(src2);cout<cout<<"Set1=";DisplaySet(src1);cout<<"\t";cout<<"Set2=";DisplaySet(src2);cout<intitem;cout<<"1->集合并"<<""<<"2-
CreateSet(src1);CreateSet(src2);
cout<cout<<"Set1=";DisplaySet(src1);cout<<"\t";cout<<"Set2=";DisplaySet(src2);cout<intitem;cout<<"1->集合并"<<""<<"2-
cout<<"Set1=";DisplaySet(src1);
cout<<"\t";
cout<<"Set2=";DisplaySet(src2);
cout<intitem;cout<<"1->集合并"<<""<<"2-
intitem;
cout<<"1->集合并"<<""<<"2-
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1