版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1實(shí)驗(yàn)一鏈表的應(yīng)用(一)建立線性表e{intdata;2typedefstructLNode{intdata;structLNode*next;}LNode,*LinkList;voidCreateList_L(LinkList&L,intn){LNode*p;L=(LinkList)malloc(sizeof(LNode));L->next=NULL;for(i=n;i>0;--i){p=(LinkList)malloc(sizeof(LNode));cin>>p->data;p->next=L->next;L->next=p;}ndlelsecout"創(chuàng)建了一個(gè)空的線性表!"<<endl;}voidmain(){LinkListL;LNodeNumcout<<"CreateList_L.cpp"<<endl<<"================"<<endl;cin>>InitLNodeNum;CreateList_L(L,InitLNodeNum);cout<<"OK...!"<<endl;h}//GetElem_L.cpp3//GettheNO.iElementofLinkList#include<stdlib.h>#include<iostream.h>#include<conio.h>#defineElemTypeint#defineLIST_MAX_LENGTH100//限定單鏈表的最大長(zhǎng)度typedefstructLNode//定義節(jié)點(diǎn)數(shù)據(jù)類型{ElemTypedata;structLNode*next;}LNode,*LinkList;voidCreateList_L(LinkList&L,intn)//創(chuàng)建單鏈表{LNode*p;L=(LinkList)malloc(sizeof(LNode));//創(chuàng)建單鏈表頭結(jié)點(diǎn)L->next=NULL;for(i=n;i>0;--i){p=(LinkList)malloc(sizeof(LNode));cin>>p->data;p->next=L->next;L->next=p;}}intGetElemLLinkListLintiinte取元素函數(shù){LNode*p;p=L->next;while(p&&j<i)h}pdatareturn(e);//如果元素存在,返回所取得元素的值}4voidmain(){LinkListL;//e是從單鏈表中所要選取的元素tiLListNodeNumcout<<"GetElem_L.cpp"<<endl<<"==============="<<endl<<endl;cin>>LListNodeNum;cout<<"請(qǐng)輸入單鏈表中的數(shù)據(jù):<23,45,2,5,-2,34,...>"<<endl;CreateList_L(L,LListNodeNum);coutendl!"<<endl;cout<"想要選取單鏈表中的第幾個(gè)元素:<比如:3>";deNumcoutendlendlGetElem_L(L,LListNodeNum-i+1,e);coutendl"<<i<<"個(gè)元素是:"<<e;cout<<endl<<"...OK...!"<<endl;h}//ListTraverse_Sq.cpp#include<stdlib.h>#include<iostream.h>#include<conio.h>#include<stdio.h>efineefineefine{ElemTypeintLIST_INIT_SIZE100LISTINCREMENT105intInitListSqSqListL//線性表的初始化{L.elem=(int*)malloc(LIST_INIT_SIZE*sizeof(int));if(!L.elem)return(0);L.length=0;L.listsize=LIST_INIT_SIZE;return(1);}voidmain(){SqListL;cout<<"ListTraverse_Sq.cpp"<<endl<<"================="<<endl<<endl;ListSqLcin>>L.length;cout<<"請(qǐng)輸入線性表中的元素:<比如:{34,54,30,2,40,...}>"<<endl;for(j=0;j<L.length;j++)cin>>L.elem[j];cout表中的元素:";for(j=0;j<L.length;j++)cout<<L.elem[j]<<"";cout<<endl<<"...OK...!"<<endl;h}//ListInsert_L.cpp//ThisprogramistoinsertaelementintotheLNode6#include<stdlib.h>#include<malloc.h>#include<iostream.h>#include<conio.h>#defineINIT_LENGTH10#defineOK1#defineERROR0typedefstructLNode//定義鏈表節(jié)點(diǎn){intdata;structLNode*next;}LNode,*Linklist;intListInsertLLinklistLintiinte入函數(shù){LNode*p=L;while(p&&j<i-1)//尋找插入位置}{cout插入位置有誤!"<<endl;return(ERROR);}LNode*s;s=(Linklist)malloc(sizeof(LNode));//新建節(jié)點(diǎn)sdatae將待插入的元素存入新建節(jié)點(diǎn)中s>next=p->next;p->next=s;return(OK);}voidmain()//主函數(shù)LNodenode[10];LNode*L,*p;intarray[INIT_LENGTH+1]={6,18,72,19,25,30,87,41,51,49};Lnode;L=(Linklist)malloc(sizeof(LNode));L->next=NULL;for(i=10;i>0;i--)7{p=(Linklist)malloc(sizeof(LNode));p->data=array[i-1];p->next=L->next;L->next=p;}cout<<endl<<endl<<"ListInsert_L.cpp";cout<<endl<<"================";coutendlendl表節(jié)點(diǎn)是:";for(i=0;i<INIT_LENGTH;i++){p=p->next;cout<<p->data<<"";}cout<<endl<<endl<<"請(qǐng)輸入元素插入位置(1--11):";cout輸入待插入元素:(例如:58):";if(ListInsert_L(L,j,e)){coutendl<"新的鏈表節(jié)點(diǎn)是:";for(i=0;i<11;i++){p=p->next;cout<<p->data<<"";}}cout<<endl<<endl<<"...OK!...";h}//ListDelete_Sq.cpp//DeletetheNO.iElementofSq_Listandgetthevalue8#include<stdlib.h>#include<iostream.h>#include<conio.h>#include<stdio.h>#defineElemTypeint#defineLIST_INIT_SIZE100#defineLISTINCREMENT10typedefstruct{intInitListSqSqListL//線性表的初始化{L.elem=(int*)malloc(LIST_INIT_SIZE*sizeof(int));if(!L.elem)return(0);L.length=0;L.listsize=LIST_INIT_SIZE;return(1);}voidListDelete_Sq(SqList&L,inti,int&e)//刪除線性表元素函數(shù){coutiisOverFlow"<<endl;}p=&(L.elem[i-1]);q=L.elem+L.length-1;for(++p;p<=q;++p)*(p-1)=*p;--L.length;cout元素!"<<endl;}voidmain(){SqListL;9cout<<"ListDelete_Sq.cpp"<<endl<<"================="<<endl<<endl;ListSqLcin>>L.length;cout<<"請(qǐng)輸入線性表中的元素:<比如:{34,54,30,2,40,...}>"<<endl;for(j=0;j<L.length;j++)cin>>L.elem[j];coutendlcout<<endl;cout入要?jiǎng)h除線性表中的第幾個(gè)元素:<比如:3>";ListDelete_Sq(L,i,e);cout后的新的線性表為:";for(j=0;j<L.length;j++)cout<<L.elem[j]<<"";cout<<endl<<"...OK...!"<<endl;h}實(shí)驗(yàn)二鏈表的應(yīng)用(二)棧的應(yīng)用hmhh#defineOK1}rOK}1}troyQueuecppprogramistodestrpuSqQueuemallochiostreamhconioh#defineMAXQSIZE100#defineOK1#defineERROR0typedefstructQNode//定義隊(duì)列的節(jié)點(diǎn)結(jié)構(gòu)rrearQElemTypeeueuePtrmallocsizeofQNodewreturnERROR);}taetNULLreturn(OK);}rnextpreturn(OK);intDestroyQueue(LinkQueue&Q)//DestroyQueue()sub-functionttQrear}return(OK);ndvoidmain//main()functionQueueQeqQrearNULLlendlDestroyQueuecppcoutendl"<<endl<<endl;whileeEnQueueQe//callEnQueue()}coutendlThenewQNodeis:";frontqNULLqqnextcoutqdata";DestroyQueue(Q);//callDestroyQueue()utendlendlcoutendlendl.OK!...";}ueuecppThisprogramistocalculatethelengthoftheSqQueuemallochiostreamhconioh#defineMAXQSIZE100#defineLENGTH10#defineOK1#defineERROR0staticintarrayLENGTH,89};typedefstructSqQueue//定義隊(duì)列的鏈表結(jié)構(gòu)intEnQueue(SqQueue&Q,QElemTypee)//隊(duì)列的插入函數(shù)returnERROR);}eQreareearQrearMAXQSIZEreturn(OK);}QbaseQElemTypemallocMAXQSIZEsizeofQElemType;ntQrearlendlEnQueuecppcoutendlendlendl;whileeueQe}coutendl:";recoutQbasee";coutendlendl.OK!...";}ueuecppThisprogramistodeletethefirstelementoftheSqQueuemallochiostreamhconioh#defineMAXQSIZE100#defineOK1#defineERROR0typedefstructSqQueue//定義隊(duì)列的鏈表結(jié)構(gòu)intEnQueue(SqQueue&Q,QElemTypee)//隊(duì)列的插入函數(shù)returnERROR);}eQreareearQrearMAXQSIZEreturn(OK);}intDeQueue(SqQueue&Q,QElemType&e)//隊(duì)列的刪除函數(shù)ptyreturnERROR);}rontQfrontMAXQSIZEreturn(e);}QbaseQElemTypemallocMAXQSIZEsizeofQElemTypentQrearlendlDeQueuecppcoutendlendl<<endl;whileeueQe}coutendli;coutendl列為:";teQrearecoutQbasee";coutendlendl..OK!...";}toptStackcppThisprogramistoinitializeastackmallochiostreamhconioh#defineSTACK_INIT_SIZE100#defineSTACKINCREMENT10#defineOK1#defineERROR0typedefstruct//定義棧的結(jié)構(gòu)op{aseSElemTypemallocSTACKINITSIZEsizeofSElemTypereturnERROR);}S.top=S.base;zeSTACKINITSIZEreturn(OK);}{kSlendlInitStackcppoutendldlendlcoutendlendl.OK!...";}shLcpprogramistopushLinkStackmallochiostreamhconioh#defineStack_Length6#defineOK1#defineERROR0typedefstructSNode//定義棧節(jié)點(diǎn)結(jié)構(gòu)eemallocsizeofSNodereturnERROR);}extreturn(OK);}tackLengthnodeSElemTypearrayStackLength30,55};locsizeofSNodekLengthietaarrayipnexttopnext}dlendlPushLcppendlcoutendlendl原先的鏈棧(從棧頂?shù)綏5?:";while(p->next)coutpdata;}coutendl壓棧元素:";if(Push_L(top,e))//callGetTop_L()ecoutendl:";while(top->next)couttopdata;}coutendlendl.OK!...";}pLcpprogramistopopLinkStackmallochiostreamhconioh#defineStack_Length6#defineOK1#defineERROR0ructSNodeeereturnERROR);}return(OK);}tackLengthnodeSElemTypearrayStackLength,30,55};locsizeofSNodedlendlPopLcppndlkLengthi{inkStackmallocsizeofSNodetaarrayipnexttopnext}coutendlendl原先的鏈棧元素(從棧頂?shù)綏5?:";while(p->next)coutpdata;}coutendl"<<e;coutendl元素為:";while(top->next)couttopdata;}coutendlendl.OK!...";}tTopLcpphisprogramistogetthetopelementofLinkStackmallochiostreamhconioh#defineStack_Length6#defineOK1#defineERROR0typedefstructSNode//定義棧的鏈表結(jié)構(gòu)ktopSElemTypeereturnERROR);}return(OK);}}tackLengthnodeSElemTypearrayStackLength30,55};locsizeofSNodekLengthietaarrayipnexttopnext}dlendlGetTopLcpputendlcoutendlendlTheLinkStackis(toptobottom):";while(p->next)coutpdata";}if(GetTop_L(top,e))//callGetTop_L()tendlecoutendlendl.OK!...";}(表達(dá)式)/函數(shù)名()*/+-級(jí)1232dioh#definemax100treturnreturn1;}intfULL}else{returna元素}}NULLreturnsdatastop/返回棧頂元素}rintfelse{}}return1;return0;}intzh表達(dá)式printf表達(dá)式輸入錯(cuò)誤\n");return0;}else正確情況下while(in[i]!='#'){//表達(dá)式以'#'結(jié)束if(in[i-1]>47){outj='';pushini;pushini);}elseifini{//對(duì)于右括號(hào),使括號(hào)內(nèi)的仍停留在棧中的運(yùn)算符pop}pop號(hào)}else{}i}}}}while(!empty()){//把暫存在棧中的運(yùn)算符}}}whileout[i]!='\0'){//后綴表達(dá)式?jīng)]有結(jié)束時(shí)switch(out[i]){casebpopapopaabpushabreak/加法運(yùn)算casebpopapopaabpushabreak/減法運(yùn)算casebpopapopaabpushabreak/乘法運(yùn)算casebpopapopaabpushabreak/除法運(yùn)算}后一個(gè)時(shí)i=i-1;/為運(yùn)算符時(shí),進(jìn)行最后一步運(yùn)算}i++;}returntop();}voidmain(){printf的中綴表達(dá)式\:n");scanf("%s",&in)輸;中綴表達(dá)式zh();/把/中綴表達(dá)式轉(zhuǎn)換成相應(yīng)的后綴表達(dá)式printf("\綴表達(dá)式為:\n%s\n",out);printf("\果:%s=%d\n",in,js());}tdiohtdlibhtringhstintMAXSIZE{inttable轉(zhuǎn)換為后綴表達(dá)式//infix:staintsuffixintlengthvoidinitStacksta始化{定向到文件中//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);ckstaultharistrMAXSIZEprintf("請(qǐng)輸入以+-*/組成的四則運(yùn)算\n(注意負(fù)數(shù)需要在兩旁添加上括號(hào))\n");scanf("%s",istr);initsta間初始化infix_to_suffix(&sta,istr,sstr,&length);initsta//再次對(duì)??臻g初始化result=suffix_to_result(&sta,sstr,length);printf("%d\n",result);//fclose(stdin);//fclose(stdout);return0;}{intb=0;//當(dāng)數(shù)字是十位或以上的時(shí)候進(jìn)行記錄for(i=0;i<strlen(infix);){ifinfixiinfixi9'){b=0;//謹(jǐn)記每次都需重新賦值為零!while(infix[i]>='0'&&infix[i]<='9'){bb10+(infix[i]-'0');}}//如果是右括號(hào)的話,將棧中在左括號(hào)以上的所有運(yùn)算符彈出,然后continueif(infix[i]==41){while(sta->data[sta->top]!=40){suffix[j]=sta->data[sta->top];sta->data[sta->top]=0;sta->top--;}astatopsta->top--;須將新的棧頂元素的優(yōu)先級(jí)記錄下來priority=table[sta->data[sta->top]%10];},直接壓棧{statopinfixi須將新的棧頂元素的優(yōu)先級(jí)記錄下來priority=table[sta->data[sta->top]%10];}算符,則壓棧nfixiinfixi{級(jí)是否比入棧元素優(yōu)先級(jí)要大//如果是大于的話,則從棧頂將元素依次出棧后,把待入棧的元素if(priority>=table[infix[i]%10]){while(priority>=table[infix[i]10]&&sta->data[sta->top]!=40){suffix[j]=sta->data[sta->top];sta->data[sta->top]=0;priority=table[sta->data[sta->top]%10];}sta->data[sta->top]=infix[i];//注意壓棧后,須將新的棧頂元素的優(yōu)先級(jí)記錄下來priority=table[sta->data[sta->top]%10];}{提取finfixistadatastatop{b=0;while(infix[i+1]>='0'&&infix[i+1]<='9'){b=b*10+(infix[i+1]-'0');}suffixjb*-1;astatopsta->top--;priority=table[sta->data[sta->top]%10];}statopinfixi須將新的棧頂元素的優(yōu)先級(jí)記錄下來priority=table[sta->data[sta->top]%10];}}}素進(jìn)行彈出while(sta->top!=-1){stadatastatopsta->top--;}*length=j;}esultStackstaintsuffixintlength{fori0;i<length;i++){//循環(huán)遍歷后綴表達(dá)式,數(shù)字就直接壓棧,運(yùn)算符就取棧頂兩個(gè)元素出并將結(jié)果壓棧switch(suffix[i]){result=sta->data[sta->top-1]*sta->data[sta->top];sta->top-=1;sta->data[sta->top]=result;reakresult=sta->data[sta->top-1]+sta->data[sta->top];sta->top-=1;sta->data[sta->top]=result;reakresult=sta->data[sta->top-1]-sta->data[sta->top];sta->top-=1;sta->data[sta->top]=result;reakresultstadatastatop]/sta->data[sta->top];atoptffixi}}result}itStacksta{foriiMAXSIZE;i++){}atop}實(shí)驗(yàn)三鏈表的應(yīng)用(三)哈夫曼樹和哈夫曼編碼h說明***/{{printf("\n╔-----------------------------------------------╗");printf("\n┆退出程序---5printf("\n╚-----------------------------------------------╝\n");printf("請(qǐng)輸入所要進(jìn)行的操作序號(hào):");用戶的//接受用戶T=createbitree(&Tree);break;case2:Qtraversebitree(*T);break;case3:Ztraversebitree(*T);break;case4:Htraversebitree(*T);break;case5:break;default:printf("錯(cuò)誤選擇!請(qǐng)重選\n");break;}while(k!=5);return0;}/*******************創(chuàng)建二叉樹函數(shù)****************************/bitree*createbitree(bitree*T){charch;scanf("%d",&ch);if(!((*T)=(bitnode*)malloc(sizeof(bitnode))))tcreatebitreeT)->lchild);createbitreeT)->rchild);}returnT}3********先序遍歷函數(shù)****************************/versebitreebitreeT{rsebitreeTrchildreturnreturn}elsereturn;}********中序遍歷函數(shù)****************************/ersebitreebitreeT{printfdT>date);rsebitreeTrchildreturnreturn}elsereturn;}********后序遍歷函數(shù)****************************/HtraversebitreebitreeT){printf("%d",T->date);return1;return}elsereturn;}fmanCodingcppThisfunctionistocreateHuffmanTreecodestdiohmallochiostreamhconiohstringh#defineMAX_LENGTH100nCodetypedefstruct//definestructureHuffmanTreeignedintparentlchildrchildeHTkparentghtTweightvoidHuffmanCoding(HuffmanTree&HT,HuffmanCode&HC,int*w,intn)m=2*n-1;odefor(p=HT+1,i=1;i<=n;++i,++p,++w)//initialHT[1...n]ht}for(;i<=m;++i,++p)//initialHT[n+1...2*n1]}cout<<endl<<endl<<"哈夫曼樹按以下序列創(chuàng)建:";HTsparentiHTsparentiHTilchild=s1;HTirchilds;HTiweightHTs].weight+HT[s2].weight;coutendlHTsandHTs2<<"]創(chuàng)建";coutHTiweightHTiweight}HCHuffmanCodemalloc((n+1)*sizeof(char*));ocnsizeofcharcoutendlendl樹編碼:"<<endl;orcifHTiparentfcffHTfparentcHCichar)malloc((n-start)*sizeof(char));printfnHTd的哈夫曼編碼:%s",i,HC[i]);}endvoidmain//main()functionffmanCodeHClendlHuffmanCodingcppcoutendl="<<endl;}wWHuffmanCoding(HT,HC,w,n);//callHuffmanCoding()coutendlendl.OK!...";}diohefinendefinemn//結(jié)點(diǎn)總數(shù)definemaxval0.0definemaxsize夫曼編碼的最大位數(shù){{置voidhuffmanhufmtreetree夫曼樹voiddecodehufmtreetree入電文,根據(jù)哈夫曼樹譯碼{printf——哈夫曼編碼——\n");mtreetreemhuffmantree曼樹huffmancodecodetree夫曼樹求出哈夫曼編碼{printfc",code[i].ch);eistartjnjprintfccodeibitsj]);ntfn}codetree}voidhuffmanhufmtreetree曼樹{intij,p1,p2;//p1,p2分別記住每次合并時(shí)權(quán)值最小和次小的兩個(gè)根結(jié)點(diǎn)的floatsmall1,small2,f;harcforiimi//初始化{tree[i].parent=0;tree[i].lchild=-1;tree[i].rchild=-1;tree[i].weight=0.0;}{scanf"%c%f",&c,&f);char}{pforjjij值最小的根結(jié)點(diǎn){mall}{eight}treeilchildp孩子treeirchildp孩子httreepweighttreepweight}voidhuffmancode(codetypecode[],hufmtreetree[])//根據(jù)哈夫曼樹求出哈//codetypecode[]為求出的哈夫曼編碼//hufmtreetree[]為已知的哈夫曼樹{pcodetypecd/緩沖變量for(i=0;i<n;i++){cd.start=n;ci;//從葉結(jié)點(diǎn)出發(fā)向上回溯while(p!=0){cd.start--;cdstartptreepparent;treei代碼'0'ei}}voiddecodehufmtreetree依次讀入電文,根據(jù)哈夫曼樹譯碼{gprintf("輸入發(fā)送的編碼(以'2'為結(jié)束標(biāo)志):");printf");while(b[j]!='2'){ddtreei{printf("%c",tree[i].ch);}}printfniftreeilchildbj結(jié)點(diǎn)printfnERRORn//輸入電文有錯(cuò)}實(shí)驗(yàn)四鏈表的應(yīng)用(四)圖的最短路徑a---------#defineINFINITY0//#defineINFINITYINT_MAX最大值,#include<limits.h,例中我們用0表示#defineOK1#defineFALSE0#defineERROR0#defineTRUE1#defineOVERFLOW-2typedenum{DG,DN,UDG,UDN}GraphKind有;向/,{有向網(wǎng),無向圖,無向M{typedefstructVNode{VertexTypedata;//頂點(diǎn)信息ArcNode*firstarc;//指向第一條依附該頂點(diǎn)的弧的指針VNodeAdjListMAXVERTEXNUM頭結(jié)點(diǎn){AdjListverticesTypeu{返回-1orintiiGvexnumi{cesidatareturni;}return-1;}teUDGALGraphG{無向圖GarcnumiGvexnumi{idatarticesifirstarcNULL}printf("請(qǐng)依次輸入%d條弧各自依附的兩個(gè)頂點(diǎn)(輸入格式:頂點(diǎn)1頂VertexTypev1,v2;for(i=0;i<G.arcnum;++i){getchar();//去掉輸入流里的換行符scanf("%s%s",v1,v2);m=LocateVex(G,v1);4teVexGvmnRORArcNodep1,*p2;p1=(ArcNode*)malloc(sizeof(ArcNode));//開辟表結(jié)點(diǎn),存放頂點(diǎn)的弧信息p2=(ArcNode*)malloc(sizeof(ArcNode));if(!p1||!p2)exit(OVERFLOW);p1->adjvex=m;p1->info=NULL;/無權(quán)值,直接置其為空p1->nextarc=G.vertices[n].firstarc;//表頭插入與頂點(diǎn)相關(guān)G.vertices[n].firstarc=p1;p2->adjvex=n;p2->info=NULL;p2->nextarc=G.vertices[m].firstarc;G.vertices[m].firstarc=p2;}G.kind=UDG;returnOK;}VertexType&GetVex(ALGraph&G,intv){//返回序號(hào)為v的頂點(diǎn)的值if(v<0||v>=G.vexnum)exit(OVERFLOW);returnG.vertices[v].data;}StatusPutVex(ALGraph&G,VertexTypev,VertexTypevalue){inti=LocateVex(G,v);if(i<0)returnERROR;strcpy(G.vertices[i].data,value);returnOK;}intFirstAdjVex(ALGraph&G,VertexTypev){ArcNode*p;return-1;verticesifirstarcreturn-1;npadjvex}TypevVertexTypew{//返回v的(相對(duì)于w的)下一個(gè)鄰接頂點(diǎn),若w是v的最后一個(gè)鄰接點(diǎn),返回-1ArcNode*p;p=G.vertices[i].firstarc;while(p&&p->adjvex!=j){pp>nextarc;}return-1;returnp->nextarc->adjvex;}StatusInsertVex(ALGraph&G,VertexTypev){一個(gè)新頂點(diǎn),但并不增加關(guān)系,即弧strcpyGverticesGvexnumdatav);G.vertices[G.vexnum].firstarc=NULL;GvexnumeturnOK}StatusInsertArc(ALGraph&G,VertexTypev,VertexTypew){RORArcNode*p,*q;ArcNodemallocsizeofArcNodeallocsizeofArcNodeoNULLp->adjvex=i;qadjvexj;pnextarcGverticesjfirstarcGverticesjfirstarc=p;verticesifirstarcerticesifirstarcqnOK}voidDisplayALGraphG出圖{ArcNode*p;orintiiGvexnumi{printfds",i,G.vertices[i].data);verticesifirstarcwhilep{printfd,p->adjvex);xtarc}tfNULLn}}{ALGraphG;GGlayGertexTypeavalueVexGavaluelayGprintf的頂點(diǎn):");layGprintf的弧所依附的兩個(gè)頂點(diǎn):");layGreturn}iostreamallochusingnamespacestd;defineintmax0000fineinfdefinemax0 ………{x{AdjMatrixarcs;…………………{whileG.vexs[i]!=v){}returni;}phLG{cout<<"…………創(chuàng)建無向圖…………"<<endl<<"請(qǐng)輸入圖G頂點(diǎn)和弧的個(gè)數(shù):(46)不包括“()”"<<endl;cin>>G.vexnum>>G.arcnum;for(i=0;i!=G.vexnum;++i){cout<<"輸入頂點(diǎn)"<<i<<endl;cin>>G.vexs[i];}for(i=0;i!=G.vexnum;++i)for(j=0;j!=G.vexnum;++j){G.arcs[i][j].adj=int_max;G.arcs[i][j].info=NULL;}for(intk=0;k!=G.arcnum;++k){coutab3)不包括“()”"<<endl;cinvvw及權(quán)值GarcsijadjwGarcsjiadjw}vexnum}dljjzprintMGraphLG{xnumi{xnumjcoutGarcsijadj<"";}}cnode{extarcructvnode{arcnodefirstarc的弧的指針{icesmax……………隊(duì)列定義……{{…………{aMGraphLG{rctempxnumi{sidataGvexsiirstarcNULL}xnumi{xnumj{{exnum{cnodemallocsizeofarcnodeirstarcarcNULLwhile(G.arcs[i][j].adj!=int_max&&j!=G.vexnum){locsizeofarcnodeirstarctem}--j;}}{exnum{cnodemallocsizeofarcnodetarcarcNULL}}}}mumforiigravexnumi){outiraverticesifirstarcwhilep=NULL){xtarc}return1;}adjprintalgraphgra{vexnumi{outiraverticesifirstarcwhilep=NULL){xtarc}}}{Lrnvfirstarcadjvex}{{}{}rn}{returnLLreturn1;}{ueueptrmallocsizeofqnodereturntaetNULLreturn1;}{rreturnqfrontnextxtpnextreturn1;}{rreturn1;return}voidbfstraalgraphgra{vexnumiivexnumiticesidatawhilequeueempty(q)){//cout<<""<<e<<"";wefirstadjvexgragraverticesewewenextadjvexgragrav{{weticeswedata}}}}}{vexnumi{i}vexnumj{}return}grainti{5icoutivisitediendlticesidatandlforwefirstadjvexgragraverticesi;we>=0;we=nextadjvex(gra,gra.v{coutwevisitedweendlwewe;coutnextadjvexgragraverticesiwe<endl;outiweendlwewe;coutnextadjvexgragraverticesiwe<endl;}return12;}{tminimumclosedgepu{edgeavexGukendlxnumj{{exugeajlowcostGarcskjadj}xnumi{minimumclosedgeautclosedgeakadjvexGvexskendlowcostxnumjjlowcost{djvexGvexskgeajlowcostGarcskjadj}}}return}{{}returns;{//prevex[]存儲(chǔ)最短路徑在U中的結(jié)點(diǎn){prevexi點(diǎn)未加入到最小生成樹中}{mininf;flowcostjminlowcostj{inlowcostj}printfd,%d)%d/t",prevex[k]-1,k-1,min);{xjk}ntfn}return}intf{whileacrvisitedf]>0)returnf}voidkruscalarcMG
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 主題公園演員聘用合同
- 廣告牌制作焊接施工合同
- 資金籌集操作規(guī)程
- 城市綜合體改造委托書模板
- 島嶼探險(xiǎn)區(qū)防水施工安全協(xié)議
- 2025年度光伏發(fā)電項(xiàng)目安裝工程承包協(xié)議3篇
- 2024年集裝箱買賣合同模板
- 2025版?zhèn)€人區(qū)塊鏈技術(shù)應(yīng)用借款合同
- 2025版家具展會(huì)參展合同范本6篇
- 2025年1月山西、陜西、寧夏、青海普通高等學(xué)校招生考試適應(yīng)性測(cè)試(八省聯(lián)考)政治試題(含答案)
- 勞動(dòng)合同范本(2025年)
- 遼寧2025年高中學(xué)業(yè)水平合格性考試物理試卷試題(含答案詳解)
- 2024年人教版三年級(jí)上數(shù)學(xué)教學(xué)計(jì)劃和進(jìn)度安排
- 《電能計(jì)量知識(shí)介紹》課件
- 2025屆甘肅省武威市重點(diǎn)中學(xué)高三六校第一次聯(lián)考英語試卷含解析
- 工業(yè)項(xiàng)目投資估算及財(cái)務(wù)評(píng)價(jià)附表(有計(jì)算公式)
- 用所給詞的適當(dāng)形式填空(專項(xiàng)訓(xùn)練)人教PEP版英語六年級(jí)上冊(cè)
- 江蘇省常州市2023-2024學(xué)年八年級(jí)上學(xué)期期末道德與法治試題(含答案解析)
- 人教版 五年級(jí)上冊(cè)道德與法治全冊(cè)各課及單元同步檢測(cè)試卷【含答案】
- 烏克蘭胚胎干細(xì)胞全新獨(dú)創(chuàng)治療,世界先進(jìn)
- 經(jīng)典魚骨圖ppt模板PPT學(xué)習(xí)教案
評(píng)論
0/150
提交評(píng)論