2023年數(shù)據(jù)結(jié)構(gòu)鄰接矩陣鄰接表圖實(shí)驗報告_第1頁
2023年數(shù)據(jù)結(jié)構(gòu)鄰接矩陣鄰接表圖實(shí)驗報告_第2頁
2023年數(shù)據(jù)結(jié)構(gòu)鄰接矩陣鄰接表圖實(shí)驗報告_第3頁
2023年數(shù)據(jù)結(jié)構(gòu)鄰接矩陣鄰接表圖實(shí)驗報告_第4頁
2023年數(shù)據(jù)結(jié)構(gòu)鄰接矩陣鄰接表圖實(shí)驗報告_第5頁
已閱讀5頁,還剩16頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論