C++实现字符串求交集、并集、差集

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includemalloc.h#includeiostream#includectype.htypedefboolStatus;typedefcharElemType;typedefstructNodeType{ElemTypedata;NodeType*next;}NodeType,*LinkType;//½áµãÀàÐÍ£¬Ö¸ÕëÀàÐÍtypedefstruct{LinkTypehead,tail;//·Ö±ðÖ¸ÏòÏßÐÔÁ´±íµÄÍ·½áµãºÍβ½áµãintsize;//ָʾÁ´±íµÄµ±Ç°³¤¶È}OrderedList;//ÓÐÐòÁ´±íÀàÐÍtypedefOrderedListOrderedSet;OrderedSetSet1,Set2,Set3;//±ØÐëÒª°ÑÈý¸ö¼¯ºÏÉùÃ÷Ϊȫ¾Ö±äÁ¿£¬ÒòΪÕâÑù²ÅÄܱ£´æCreatSetd¶ÔÆäµÄ¸ü¸Ä¡£Ê¹ÆäÔÚËùÓк¯ÊýÖж¼ÓÐЧ¡£StatusMakeNode(LinkType&p,ElemTypee){//·ÖÅäÓÉpÖ¸ÏòµÄÊý¾ÝÔªËØe¡¢ºó¼ÌΪ¿ÕµÄ½áµã£¬²¢·µ»ØTRUE£¬//Èô·ÖÅäʧ°Ü£¬Ôò·µ»Øfalsep=(LinkType)malloc(sizeof(NodeType));if(!p)returnfalse;p-data=e;p-next=NULL;returntrue;}voidFreeNode(LinkType&p){//ÊÍ·ÅpËùÖ¸½áµã}LinkTypeCopy(LinkTypep){//¸´ÖÆÉú³ÉºÍÖ¸ÕëpËùÖ¸½áµãÓÐֵͬԪËصÄнڵ㲢·µ»Ø£¬//Èô·ÖÅäʧ°Ü£¬Ôò·µ»Ø¿ÕÖ¸Õ룬нڵãµÄÖ¸ÕëÓòΪNULLLinkTypes;s=(LinkType)malloc(sizeof(NodeType));if(!s)returnNULL;s-data=p-data;s-next=p-next;returns;}ElemTypeElem(LinkTypep){//ÈôÖ¸Õëp!=NULL,Ôò·µ»ØpËùÖ¸½áµãµÄÊý¾ÝÔªËØ£¬·ñÔò·µ»Ø'#'returnp-data;}voidWriteElem(ElemTypee){printf(%c,e);}LinkTypeSuccNode(LinkTypep){//ÈôÖ¸Õëp£¡=NULL,Ôò·µ»ØÖ¸ÏòpËùÖ¸½áµãµÄºó¼ÌÔªËØÖ¸Õ룬//·ñÔò·µ»ØNULLreturnp-next;}boolInitList(OrderedList&L){//¹¹ÔìÒ»¸ö´øÍ·½áµãµÄ¿ÕµÄÓÐÐòÁ´±íL£¬²¢·µ»ØTrue£»//Èô·ÖÅä¿Õ¼äʧ°Ü£¬ÔòÁîL.headΪNULL£¬²¢·µ»Øfalseif(MakeNode(L.head,'')){L.tail=L.head;L.size=0;returntrue;}else{L.head=NULL;returnfalse;}}voidDestroyList(OrderedList&L){//Ïú»ÙÓÐÐòÁ´±íLinkTypeq,p=L.head;while(p){q=p;p=SuccNode(p);FreeNode(p);}L.head=L.tail=NULL;}intListLength(OrderedListL){//·µ»ØÁ´±íµÄ³¤¶ÈLinkTypep=L.head;intcount=0;while(p){p=SuccNode(p);count++;}returncount;}boolListEmpty(OrderedListL){//ÈôL²»´æÔÚ»òΪ¿Õ±í£¬·µ»ØTrue£¬·ñÔò·µ»ØFalseif(!L.head||!L.size)returntrue;elsereturnfalse;}LinkTypeGetElemPos(OrderedListL,intPos){//ÈôL´æÔÚÇÒ0PosL.size+1,Ôò·µ»ØÖ¸ÏòµÚPos¸öÔªËصÄÖ¸Õëif(Pos1||PosL.size||!L.head)returnNULL;LinkTypep=L.head;while(Pos--){p=SuccNode(p);}returnp;}boolLocateElem(OrderedListL,ElemTypee,LinkType&p){//ÈôÓÐÐò±íL´æÔÚÇÒ±íÖдæÔÚÔªËØe£¬ÔòqָʾLÖеĵÚÒ»¸öΪeµÄ//½áµãµÄλÖ㬲¢·µ»Øtrue£»·ñÔòqָʾµÚÒ»¸ö´óÓÚeµÄÔªËصÄÇ°ÇýµÄ//λÖ㬲¢·µ»ØfalseLinkTypepre;if(L.head){pre=L.head;p=pre-next;//preÖ¸Ïò*pµÄÇ°Çý£¬pÖ¸ÏòµÚÒ»¸öÔªËؽáµãwhile(p&&p-datae){pre=p;p=SuccNode(p);}if(p&&p-data==e)returntrue;else{p=pre;returnfalse;}}elsereturnfalse;}voidAppend(OrderedList&L,LinkTypes){//ÔÚÒÑ´æÔÚµÄÓÐÐòÁ´±íLÖÐĩβ²åÈëÖ¸ÕësËùÖ¸½áµãif(L.head&&s){if(L.tail!=L.head)L.tail-next=s;//Èç¹ûÓÐÐòÁ´±í·Ç¿Õ£¬Ôò½«Î²½áµãÖ¸ÕëÖ¸ÏòµÄ½áµãºó¼ÌÖ¸Õ븳¸øselseL.head-next=s;//ÈôÓÐÐòÁ´±í¿Õ£¬ÔòÍ·½áµãºó¼Ì¸³¸øsL.tail=s;//s´ú±íβ½áµãµÄµØÖ·L.size++;}}voidInsertAfter(OrderedList&L,LinkTypeq,LinkTypes){//ÔÚÒÑ´æÔÚµÄÓÐÐòÁ´±íLÖÐqËùÖ¸µÄ½áµãÖ®ºó²åÈëÖ¸ÕësËùÖ¸½áµãif(L.head&&q&&s)//²åÈëµÄÌõ¼þÊÇL.head¡¢p¡¢s¾ùÊÇÓÐЧָÕë{s-next=q-next;q-next=s;}if(L.tail==q)L.tail=s;//Èç¹ûqÊÇβ½áµã£¬ÄÇôβ½áµãµÄµØÖ·»á¸Ä±äL.size++;}voidListTraverse(LinkTypep,void(*visit)(LinkTypeq)){//´Óp£¨p!=NULL)ָʾµÄ½áµã¿ªÊ¼£¬ÒÀ´Î¶Ôÿ¸ö½áµãµ÷ÓÃvisitwhile(p){visit(p);p=SuccNode(p);}}//3.¼¯ºÏSetÀûÓÃÓÐÐòÁ´±íÀàÐÍOrderedListÀ´ÊµÏÖ£¬¶¨ÒåΪÓÐÐò¼¯OrderedSet£ºvoidCreateSet(OrderedSet&T,char*s){//Éú³ÉÓÉ´®sÖÐСд×Öĸ¹¹³ÉµÄ¼¯ºÏT£¬IsLowerÊÇСд×ÖĸÅбðº¯Êýintlength(char*);LinkTypep,q;if(InitList(T))//¹¹Ôì¿Õ¼¯Tfor(inti=0;ilength(s);i++)//length£¨char*)º¯ÊýÇó³ö×Ö·û´®µÄ³¤¶È¡£if(islower(s[i])&&!LocateElem(T,s[i],p))//¹ýÂËÖظ´ÔªËز¢°´ÕÕ×Öĸ´ÎÐò´óС²åÈëif(MakeNode(q,s[i]))InsertAfter(T,p,q);}intlength(char*p){intlen=0;while(*(p++))len++;returnlen;}voidDestroySet(OrderedSet&T){//Ïú»Ù¼¯ºÏTµÄ½á¹¹DestroyList(T);}voidUnion(OrderedSet&T,OrderedSetS1,OrderedSetS2){//ÇóÒѽ¨³É¼¯ºÏS1ºÍS2µÄ²¢¼¯T£¬LinkTypep1,p2;ElemTypec1,c2;if(InitList(T)){p1=GetElemPos(S1,1);p2=GetElemPos(S2,1);while(p1&&p2){c1=Elem(p1);c2=Elem(p2);if(c1=c2){Append(T,Copy(p1));p1=SuccNode(p1);if(c1==c2)p2=SuccNode(p2);}else{Append(T,Copy(p2));p2=SuccNode(p2);}}while(p1){Append(T,Copy(p1));p1=SuccNode(p1);}while(p2){Append(T,Copy(p2));p2=SuccNode(p2);}}}//UnionvoidIntersection(OrderedSet&T,OrderedSetS1,OrderedSetS2){//ÇóS1ºÍS2µÄ½»¼¯LinkTypep1,p2;ElemTypec1,c2;if(!InitList(T))T.head=NULL;else{p1=GetElemPos(S1,1);p2=GetElemPos(S2,1);while(p1&&p2){c1=Elem(p1);c2=Elem(p2);if(c1c2)p1=SuccNode(p1);elseif(c1c2)p2=SuccNode(p2);else{Append(T,Copy(p1));p1=SuccNode(p1);p2=SuccNode(p2);}}}}voidDifference(OrderedSet&T,OrderedSetS1,OrderedSetS2){//ÇóS1ºÍS2µÄ²î¼¯LinkTypep1,p2;ElemTypec1,c2;if(!InitList(T))T.head=NULL;else{p1=GetElemPos(S1,1);p2=GetElemPos(S2,1);while(p1&&p2){c1=Elem(p1);c2=Elem(p2);if(c1c2){Append(T,Copy(p1));p1=SuccNode(p1);}elseif(c1c2)p2=SuccNode(p2);else{p1=SuccNode(p1);p2=SuccNode(p2);}}}}voidWriteSetElem(LinkTypep){//ÏÔʾ¼¯ºÏÖеÄÒ»¸öÔªËØprintf(,);WriteElem(Elem(p));}voidPrintSet(OrderedSetT){LinkTypep;p=GetElemPos(T,1);printf([);if(p){WriteElem(Elem(p));p=SuccNode(p);}ListTraverse(p,WriteSetElem);printf(]);}voidInitialization(){OrderedSetSet1,Set2;system(CLS);//ÇåÆÁ//ÔÚÆÁĻϷ½ÏÔʾ²Ù×÷ÃüÁîÇåµ¥£ºMakeSet1--1MakeSet2--2Union--uIntersection--iDifference--dQuit--q;printf(MakeSet1--1MakeSet2--2Union--uIntersection--iD

1 / 9
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功