版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
實(shí)驗名稱:數(shù)據(jù)結(jié)構(gòu)實(shí)驗五
實(shí)驗內(nèi)容:1.使用鄰接矩陣速立一個圖,深度遍歷。2.使
用鄰接表建立一個圖,廣度遍歷。3.建立一個圖,存儲結(jié)構(gòu)自
己擬定,并進(jìn)行拓?fù)渑判颉?/p>
實(shí)驗代碼:
1.#inc1ude"stdio.h”
#defineInfinity100
#defineMaxVertexNum20
typedefenum{DGDN,UDG,UDN}GraphKind;
typedefintVRType;
typedefcharVertexType;
boolVisit[MaxVertexNum];
typedefstructArcCell
(
VRTypeadj;
}ArcCe11,AdjMatrix[MaxVertexNum][MaxVertexNum];
typedefstruct
(
VertexTypevexs[MaxVertexNum];
AdjMatrixares;//鄰接矩陣
intvexnum,arcnum;//圖的當(dāng)前頂點(diǎn)數(shù)和弧數(shù)
。GraphKindkind;
)MGraph;
intLocateVex(MGraphG,VertexTypev)
for(inti=0;i<G.vexnum;++i)
wif(v==G.vexsEi])
0returni;
)
if(i=Gvexnum)
oprintf("輸入的頂點(diǎn)不合法\n”);
return0;
)
VertexTypevl,v2;
VRTypew;
voidCreateUDG(MGraph&G)
(
ointi,j,k;
printf("請輸入頂點(diǎn)數(shù):\n");
scanf("%d”,&G.vexnum);
叩rintf("請輸入弧數(shù):\n”);
scanf("%d",&G.arcnum);
。i=0;
owhile(i<Gvexnum)
printf("請輸入第%d個頂點(diǎn)\n",i);
getchar();
scanf(n%cn,&Gvexs[i]);
。++i;
。}
?for(i=0;i<G.vexnum;++i)
for(j=0:j<G.vexnum;++j)
??Garcs[i][j].adj=0;
0)
ofor(k=0;k<G.arcnum;++k)
°{
“printf("請輸入一條邊依附的頂點(diǎn)及權(quán)值(vlv2w)\n");
egetchar();
oscanf("%c%c%d",&vl,&v2,&w);
Qi=LocateVex(G,v1);
何=LocateVex(G,v2);
?G.arcs[i][j].adj=w;
^G.arcs[j][i]=G.arcs[i][j];
°)
6return;
)
voidDFSTraverse(MGraph&Qinti)
printf(*'%cn,G.vexs[i]);
Visit[i]=true;
for(intj=0;j<Gvexnum;j++)
°{
時f(Garcs[i][j].adj==l&&!Visit[jl)
(
DFSTraverse(G,j);
00}
")
)
voidDFS(MGraph&G)
(
?inti;
for(i=0;i<G.vexnum;i++)Visil[i]=faIse;
?for(i=0;i<G.vexnum;i++)
(
。if(!Visit[i])
0(
。DFSTraverse(G,i);
)
voidmain()
<>MGraphgraph;
*>CreateUDG(graph);
中rintf("頂點(diǎn)集合為::");
fbr(inti=0;i<graph.vexnum;++i)
?>printf(n%c",graph.vexsLi]);
printf("\n深度遍歷結(jié)果是:");
eDFS(graph);
叩rintf(”\n”);
^return;
)
2.
#include"stdio.h1'
#include"stdlib.h"
#defineMaxVertexNum20
typedefintInfbType;
typedefcharVertexType;
typedefVertexTypeQElemType;
boo1visited[MaxVertexNum];
typedefstructArcNode
(
°intadjvex;//該弧指向的頂點(diǎn)位置
^structArcNode*nextarc;//指向下一條弧的指針
InfoType*info;
}ArcNode;
typedefstructVNode
aVertexTypedata;//頂點(diǎn)信息
ArcNode*firstare;//指向第一條依附該頂點(diǎn)的弧的指針
}VNode,AdjList[MaxVertexNumJ;
typedefstruct
!
AdjListvertices;
“ntvexnum,arenum;//圖的當(dāng)前頂點(diǎn)數(shù)和弧數(shù)
)ALGraph;
typedefstructQNode
(
QElemTypedata;
?structQNode*next;
}QNode,*Queueptr;
typedefstruet
(
Queueptrfront;
Queueptrrear;
}LinkQueue;
voidInitQueue(LinkQueue&Q)
(
Q.front=Q.rear=(Queueptr)ma1loc(sizeof(QNode));
if(!Q.front)return;
Q.front—>nextNULL;
?return;
voidEnQueue(LinkQueue&Q,QElemTypee)
(
oQueueptrp=NULL;
p=(Queueptr)malloc(sizeof(QNode));
if(!p)return;
?p->data=e;
p->next=NULL;
Q.rear—>next=p;
Q.rear=p;
return;
)
QE1emTypeDeQueue(LinkQueue&Q,QElemType&e)
(
Queueptrp;
“f(Q.front==Q.rear)return'’;
p=Q.front->next;
笛=p->data;
oQ.front—>next=p->next;
“f(Q.rear==p)
Q.rear=Q.front;
ofree(p);
oreturne;
)
intQueueEmpty(LinkQueueQ)
if(Q.front==Q.rear)
return1;
oelse
。return0;
)
intLocate(ALGraphG,VertexTypev)
(
ofor(intk=0;k<G.vexnum;++k)
°(
if(v==Gvertices[k].data)
returnk;
)
if(k=Gvexnum)
Printf("輸入的頂點(diǎn)不合法\n");
6return0;
)
voidCreateALGraph(ALGraph&G)
(
VertexTypevl,v2;
°inti,j,k;
?ArcNode*p,*r;
printf("請輸入頂點(diǎn)數(shù)和弧數(shù)(以空格分開):”);
scanf("%d%(!",&G,vexnum,&G.arcnum);
ofor(i=0;i<G.vexnum;++i)
(
getchar();
pr血f(”請輸入第%d個結(jié)點(diǎn):”,i);
。scanf(H%cn,&G,verticesLi],data);
oG.vertices[i].firstarc=NULL;
。}
?for(i=0;i<G.arcnum;++i)
(
。printf(”請輸入第%d條弧(格式:頂點(diǎn)頂點(diǎn)(以空格隔開)):口);
ogetchar();
。scanf("%c%c",&vl,&v2);
8k=Locale(G,vl);
。。j=Locate(G,v2);
op=(ArcNode)malloc(sizeof(ArcNode));
。?!付?AreNode*)ma11oc(sizeof(ArcNode));
p->adjvex=j;
。叩?>info=NULL;
r->adjvex=k;
or—>info=NULL;
p->nextarc=G.vertices[k].firstarc;
Gvertices[k].firstarc=p;
or->nextarc=G.vertices[j].firstarc;
G.vertices[j].firstarc=r;
return;
)
voidBFSTraverse(ALGraphG,QElemTypex)
(
Anti,v;
oArcNode*p;
oQElemTypev1;
for(v=0;v<G.vexnum;++v)
visited[v]=false;
?LinkQueueQ;
nitQueue(Q);
aEnQueue(Q,x);
A=Loeate(G,x);
^visited[i]=true;
for(v=0;v<G.vexnum;++v)
°{
while(!QueueEmpty(Q))
oooDeQueue(Q,v1);
。。printf(M%c”,vl);
gi=Locate(G,vl);
8P=G.vertices[i].firstarc;
。while(p!=NULL)
000if(!visited[p—>adjvex])
00
00visited[p—>adjvex]=tru
oEnQueue(Q,G.vertices[p->adjvex].data);
000}
。。p=p->nextarc;
000j0
0)
?if(!visited[v])
(
gvisited[v]=true;
“oEnQueue(Q,G.vertices[v].data);
voidmain()
(
ocharf1agl;
ALGraphgraph2;
oQElemTypex;
aCreateALGraph(graph2);
flag1='¥*;
while(flag1=='YIIflagl=='y,)
°{
printf(”請輸入遍歷的起點(diǎn):”);
ggetchar();
scanf(u%cn,&x);
gprintf("廣度遍歷結(jié)果是:\n");
“BFSTraverse(graph2,x);
sprintf(n\n繼續(xù)遍歷(Y/N):");
^getchar();
oscanf("%c'\&flagl);
0)
?return;
)
3.
#include”stdio.hu
#include"stdlib.h"
#defineStackInitSize20
#defineStackincrement5
#defineMaxVertexNum20
typedefintInfoType;
typedefcharVertexType;
typedefVertexTypeSE1emType;
typedefstruct
(
oSElemType*base;
SE1emType*top;
intstacksize;
}SqStack;
typedefstructArcNode
intadjvex;//該弧指向的頂點(diǎn)位置
struetArcNode*nextarc;//指向下一條弧的指針
InfoType*info;
}ArcNode;
typedefstructVNode
(
?intindegree;
WertexTypedata;〃頂點(diǎn)信息
ArcNode*firstare;//指向第一條依附該頂點(diǎn)的弧的指針
}VNode,AdjList[MaxVertexNum];
typedefstruct
(
AdjListvertices;
intvexnum,arenum;//圖的當(dāng)前頂點(diǎn)數(shù)和弧數(shù)
}ALGraph;
boo1InitStack(SqStack&s)
(
s.base=(SElemType*)ma1loc(StacklnitSize*sizeof(SEIemType));
if(Js.base)returnfalse;
as.top=s.base;
os.stacksize=StackInitSize;
returntrue;
)
boo1Pop(SqStack&s,int&e)
(
if(s.top==s.base)
sreturnfalse;
e=*-s.top;
oreturntrue;
)
boolPush(SqStack&s,inte)
(
oif(s.top-s.base>=s.stacksize)
°(
s.base=(SE1emType*)rea11oc(s.base,(s.stacksize+Stackinere
men。*sizeof(SElemType));
if(!s.base)
??returnfalse;
?s.top=s.base+s.stacksize;
8s.stacksize+=StackIncrement;
°)
*s.top++=e;
oreturntrue;
)
boolStackEmpty(SqStacks)
if(s.top==s,base)
8returntrue;
else
。returnfalse;
)
intLocate(ALGraphG,VertexTypev)
(
ofor(intk=0;k<G.vexnum;++k)
(
if(v==G.vertices[k].data)
。。returnk;
。if(k=G.vexnum)
。printf("輸入的頂點(diǎn)不合法\n”);
return0;
)
voidCreateALGraph(ALGraph&G)//鄰接表存儲
(
VertexTypev1,v2;
“nti,j,k;
AreNode*p;
叩rintf(”請輸入頂點(diǎn)數(shù)和弧數(shù)(以空格分開):”);
seanf("%d%d",&G.vexnum,&Garcnum);
for(i=0;i<G.vexnum;++i)
°(
。getchar();
-printf("請輸入第%d個結(jié)點(diǎn):”,i);
。scanf("%c”,&G.vertices[i].data);
。G.vertices[i].firstare=NULL;
oG.vertices[i].indegree=0;
)
ofor(i=0;i<G.arcnum;++i)
°{
printf("請輸入第%d條有向弧弧(格式:頂點(diǎn)頂點(diǎn)(以空格隔開)):\i);
。getchar();
scanf("%c%c",&v1,&v2);
k=Locate(G,v1);
j=Locate(G,v2);
p=(ArcNode*)malloc(sizeof(ArcNode));
。叩->adjvex=j;
p->info=NULL;
?p->nextarc=G.vertices[k].firstarc;
G.vertices[k].firstarc=p;
°}
return;
)
voidFindInDegree(ALGraphG,inta[MaxVertexNum])
inti,k:
ArcNode*p;
ofor(i=0;i<G.vexnum;++i)
(
。for(p=G.verticesLi].firstarc;p;p=p->nextarc)
00|
oak=p->adjvex;
。a[k]=++G,vertices[k].indegree;
00}
return;
)
voidTopologicalSort(ALGraphG)//拓?fù)渑判蛩惴?/p>
(
inti,j,count;
oArcNode*p;
SqStacks;
intindegree[MaxVertexNum];
6for(i=0;i<MaxVertexNum;++i)
?indegree[i]=0;
FindinDegree(G,indegree);
InitStack(s);
for(i=0;i<G.vexnum;++i)
°(
eif(!indegree[i])
?Push(s,i);
)
count=0;
?whi1e(!StackEmpty(s))
(
Pop(s,i);
3Printf("%cM,G.vertices[i].data);
葉+count;
for(p=G.vertices[i].firstarc;p;p=p->nextarc)
00I
。j=p->adjvex;
°?if(!(-indegree[j]))Push(s,j);
00I
°}
oif(count<Gvexnum)
。printf("錯誤!該有向圖有回路!\nn);
eelsereturn;
)
voidmain()
(
ALGraphgraph3;
CreateALGraph(graph3);
叩rintf("拓?fù)渑判虻慕Y(jié)果是:\n”);
TopologicalSort(graph3);
printf(H\nn);
?return;
3■D:\VC++6.旭天中文精簡黑色版(D\VC6\MyProjects\Graph\Debug\Graph.exe*1
___________________—一——一,1
請輸入頂點(diǎn)數(shù):
8
請輸入弧數(shù):
:青輸入第。個頂點(diǎn)
,輸入第1個頂點(diǎn)
福輸入第2個頂點(diǎn)
:青輸入第3個頂點(diǎn)
;青輸入第4個頂點(diǎn)
:青輸入第5個頂點(diǎn)
:青輸入第6個頂點(diǎn)
箱輸入第7個頂點(diǎn)
請輸入一條邊依附的頂點(diǎn)及權(quán)值(viv2w)
請輸入一條邊依附的頂點(diǎn)及權(quán)值(""2w)
ac1
睛輸入一條邊依附的頂點(diǎn)及權(quán)值目
3"D:\VC++6.0創(chuàng)天中文精簡景&?l)\VC6\MyProjects\Graph\Debug\Graph.exe'
請輸入一條邊依附的頂點(diǎn)及權(quán)值(以
bd1
請輸入一條邊依附的頂點(diǎn)及權(quán)值(UW)
be1
請輸入一條邊依附的頂點(diǎn)及權(quán)值(皿?)
eh1
請輸入一條邊依附的頂點(diǎn)及權(quán)值(以W)
Idh1
情輸入一條邊依附的頂點(diǎn)及權(quán)值(MW)
lcf1
請輸入一條邊依附的頂點(diǎn)及權(quán)值(”M)
pg1
請輸入一條邊依附的頂點(diǎn)及權(quán)值(U1?)
fg1
頂點(diǎn)集合為::abcdefgh
深度遍歷結(jié)果是:abdhecfg
Pressanykeytocontinue.
I
-,?D:\VC++6.0s|天中文精簡螟色版⑴\VC6\MyProjects\Graph3\Debug\Gr叩h3.exe,
方
頂
明1215
■
結(jié)
第
0點(diǎn)A
第
結(jié)
1點(diǎn)B
第
結(jié)
2點(diǎn)C
第
結(jié)D
3點(diǎn)
第E
4結(jié)
點(diǎn)
第5F
結(jié)
點(diǎn)
第6G
結(jié)
點(diǎn)
第7H
結(jié)
點(diǎn)I
第8
結(jié)
點(diǎn)J
第9
結(jié)
點(diǎn)
:』K
第1
甚
《
卜n
占
第1L
有
一R
空
弋
以
占
頂
鬲開
第0_n
曲
有
我
弋
以
瓜
鬲開
頂
第1f
_.空
同
向\
有.
點(diǎn)
弋
以
瓜
鬲開
頂
第2_f.
空
向x
有.
點(diǎn)
瓜
弋
以
鬲開
頂
第3_f.)
向\.
有
空
/占
瓜
弋
以
鬲開
頂
第4_l.)
向
南\.
有i
瓜
空
弋
以
點(diǎn)
鬲開
頂
第5_Z.)
響N(.
有
瓜
弋
以
6鬲開
頂
第_.)
點(diǎn)
空
響.
有
瓜
弋
以
7鬲開
第_
頂
向z.)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度P2P出借平臺投資者教育與服務(wù)合同3篇
- 二零二五年度企業(yè)破產(chǎn)財產(chǎn)清算協(xié)議2篇
- 個性化條款:20249A文離婚合同案例分析版
- 二零二五版房屋征收拆遷補(bǔ)償協(xié)議書3篇
- 二零二五年度建筑工程招投標(biāo)與合同質(zhì)量保證金管理協(xié)議書3篇
- 物業(yè)管理處與2025年度收費(fèi)員服務(wù)協(xié)議3篇
- 2025年度門衛(wèi)人員崗位職責(zé)優(yōu)化聘用協(xié)議3篇
- 2025年度內(nèi)蒙古自治區(qū)農(nóng)業(yè)廢棄物資源化利用承包合同3篇
- 二零二五年度城鄉(xiāng)汽車租賃及售后服務(wù)合同4篇
- 二零二五年度外賣配送員勞動爭議預(yù)防與調(diào)解合同
- 2025年長沙穗城軌道交通有限公司招聘筆試參考題庫含答案解析
- 人教版物理八年級下冊 專項訓(xùn)練卷 (一)力、運(yùn)動和力(含答案)
- 山東省房屋市政工程安全監(jiān)督機(jī)構(gòu)人員業(yè)務(wù)能力考試題庫-中(多選題)
- 2024年中考語文滿分作文6篇(含題目)
- 北師大版 2024-2025學(xué)年四年級數(shù)學(xué)上冊典型例題系列第三單元:行程問題“拓展型”專項練習(xí)(原卷版+解析)
- 2023年譯林版英語五年級下冊Units-1-2單元測試卷-含答案
- 施工管理中的文檔管理方法與要求
- DL∕T 547-2020 電力系統(tǒng)光纖通信運(yùn)行管理規(guī)程
- 種子輪投資協(xié)議
- 執(zhí)行依據(jù)主文范文(通用4篇)
- 浙教版七年級數(shù)學(xué)下冊全冊課件
評論
0/150
提交評論