資源描述:
《圖的頭文件(C語(yǔ)言).docx》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫(kù)。
1、#include#includetypedefintAdjType;typedefstruct{charinfo;intmark;}VexType;typedefVexType*Vertex;typedefAdjType*Adj;typedefstruct{intn;Vertexvexs;Adj*arcs;}GraMatrix;typedefGraMatrix*Graph;GraphcreateGraph(intn){Graphg;inti,j;charx;g=(Graph)malloc(sizeof(GraMatrix));g->n=n;g->
2、vexs=(Vertex)malloc(sizeof(VexType)*n);printf("輸入頂點(diǎn)");for(i=0;ivexs[i].info);g->vexs[i].mark=0;}g->arcs=(Adj*)malloc(sizeof(Adj)*n);for(i=0;iarcs[i]=(Adj)malloc(sizeof(AdjType)*n);printf("輸入矩陣第%d行",i);for(j=0;jarcs[i][j]);}
3、returng;}intisNullGraph(Graphg){return(g->n==0);}VertexfirstVertex(Graphg){return(g->vexs);}VertexnextVertex(Graphg,Vertexvi){if(vi-g->vexs==g->n-1)returnNULL;elsereturn(vi+1);}VertexsearchVertex(Graphg,charvalue){inti;for(i=0;in;i++){if(g->vexs->info==value)return(g->vexs);g->vexs++;}}Vert
4、exfirstAdjacent(Graphg,Vertexv){inti,j;i=v-g->vexs;for(j=0;jn;j++)if(g->arcs[i][j]!=0&&g->arcs[i][j]!=10000)return(g->vexs+j);}VertexnextAdjacent(Graphg,Vertexvi,Vertexvj){inti,j,k;i=vi-g->vexs;j=vj-g->vexs;for(k=j+1;kn;k++)if(g->arcs[i][k]!=0&&g->arcs[i][k]!=10000)return(g->vexs+k);ret
5、urnNULL;}voiddfs(Graphg,Vertexv){Vertexv1;v->mark=1;printf("%c",v->info);for(v1=firstAdjacent(g,v);v1!=NULL;v1=nextAdjacent(g,v,v1))if(v1->mark==0)dfs(g,v1);}voiddft(Graphg){Vertexv;intx;for(v=firstVertex(g);v!=NULL;v=nextVertex(g,v)){x=v->mark;if(v->mark==0)dfs(g,v);}printf("");}