数据结构 图操作Word格式文档下载.docx

上传人:b****5 文档编号:21778863 上传时间:2023-02-01 格式:DOCX 页数:20 大小:126.98KB
下载 相关 举报
数据结构 图操作Word格式文档下载.docx_第1页
第1页 / 共20页
数据结构 图操作Word格式文档下载.docx_第2页
第2页 / 共20页
数据结构 图操作Word格式文档下载.docx_第3页
第3页 / 共20页
数据结构 图操作Word格式文档下载.docx_第4页
第4页 / 共20页
数据结构 图操作Word格式文档下载.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

数据结构 图操作Word格式文档下载.docx

《数据结构 图操作Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《数据结构 图操作Word格式文档下载.docx(20页珍藏版)》请在冰豆网上搜索。

数据结构 图操作Word格式文档下载.docx

intadjvex;

//该弧所指向的顶点的位置

structArcNode*nextarc;

//指向下一条弧的指针

intweight;

//与弧相关的权值,无权则为0

}ArcNode;

typedefstructVNode//顶点结点的结构

intdegree,indegree;

//顶点的度,入度

VertexTypedata;

ArcNode*firstarc;

}VNode,AdjList[MAX_VERTEX_NUM];

typedefstruct

AdjListvertices;

intvexnum,arcnum;

//顶点的实际数,边的实际数

}ALGraph;

intLocateVex(ALGraph&

G,VertexTypeu)

inti;

for(i=0;

i<

G.vexnum;

++i)

{

if(u==G.vertices[i].data)

returni;

}

return-1;

}

voidCreateDG(ALGraph&

G)

VertexTypev1,v2;

inti,j;

ArcNode*p;

printf("

\ninputthegrah'

svexnumandarcnum:

"

);

scanf("

%d%d"

&

G.vexnum,&

G.arcnum);

\ninputvertectdatas:

i++)

scanf("

%d"

G.vertices[i].data);

G.vertices[i].firstarc=NULL;

for(intk=0;

k<

G.arcnum;

k++)

printf("

\ninput%dtharc'

sfirstarcnextarc:

\n"

k+1);

v1,&

v2);

i=LocateVex(G,v1);

j=LocateVex(G,v2);

//head

if(i<

0||j<

0)

{

printf("

\nERROR!

exit(0);

}

p=(ArcNode*)malloc(sizeof(ArcNode));

p->

adjvex=j;

nextarc=G.vertices[i].firstarc;

G.vertices[i].firstarc=p;

weight=0;

i,G.vertices[i].data);

p=G.vertices[i].firstarc;

while(p)

-->

p->

adjvex);

p=p->

nextarc;

intGraphExit(ALGraphG,inti,intj)

p=G.vertices[i].firstarc;

while(p&

&

p->

adjvex!

=j)

p=p->

if(p)return1;

elsereturn0;

voidDegree(ALGraph&

G,inti)

intj;

G.vertices[i].indegree=0;

G.vertices[i].degree=0;

for(j=0;

j<

j++)

if(GraphExit(G,j,i))

G.vertices[i].indegree++;

G.vertices[i].degree++;

while(p!

=NULL)

\n%d'

sindegree:

%d,degree:

%d"

G.vertices[i].data,G.vertices[i].indegree,G.vertices[i].degree);

voidDe_Ingree(ALGraph&

inti,n,m;

Degree(G,i);

//***********************栈*******************************

SElemType*base;

SElemType*top;

intstacksize;

}SqStack;

voidInitStack(SqStack&

S)

S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));

if(!

S.base)

\nfailtoallocatestorage!

exit(0);

S.top=S.base;

S.stacksize=STACK_INIT_SIZE;

\nstoragewasallocatedsuccessfully!

voidGetTop(SqStackS,SElemType&

e)

if(S.base==S.top)

\nit'

sanemptystack!

e=*(S.top-1);

intPush(SqStack&

S,SElemTypee)

if(S.top-S.base>

=S.stacksize)

S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));

if(!

S.top=S.base+S.stacksize;

S.stacksize+=STACKINCREMENT;

*S.top++=e;

return1;

voidPop(SqStack&

S,SElemType&

if(S.top==S.base)

e=*--S.top;

intStackEmpty(SqStack&

return1;

return0;

intDestroyStack(SqStackS)

S.top=NULL;

S.base=NULL;

delete[]S.base;

S.stacksize=0;

//*********************graph&

stack*****************

intTopologicalSort(ALGraphG)

SqStackS;

inti,k,j;

InitStack(S);

if(G.vertices[i].indegree==0)

Push(S,i);

intcount=0;

while(!

StackEmpty(S))

Pop(S,i);

%d"

G.vertices[i].data);

++count;

j=LocateVex(G,G.vertices[i].data);

for(p=G.vertices[j].firstarc;

p;

p=p->

nextarc)

{

k=p->

adjvex;

G.vertices[k].indegree--;

if(G.vertices[k].indegree==0)

Push(S,k);

}

if(count<

G.vexnum)

\ntherisaloopinthisgraph!

return0;

else

stopologicalsortshowedover!

return1;

DestroyStack(S);

intmain()

ALGraphG;

CreateDG(G);

De_Ingree(G);

TopologicalSort(G);

2.无向图

(1)建立一个无向图的邻接表,并输出该邻接表。

(2)采用邻接表存储实现无向图的深度优先遍历。

(3)采用邻接表存储实现无向图的广度优先遍历。

typedefintQElemType;

#defineMAXSIZE100

//****************队列操作*******************************

typedefstruct

QElemType*base;

intfront;

intrear;

}SqQueue;

intInitQueue(SqQueue&

Q)

Q.base=(QElemType*)malloc(MAXSIZE*sizeof(QElemType));

Q.base)

\nfailtoallocstorage!

Q.front=Q.rear=0;

intEnQueue(SqQueue&

Q,QElemTypee)

if((Q.rear+1)%MAXSIZE==Q.front)

\nthequeueisfull!

Q.base[Q.rear]=e;

Q.rear=(Q.rear+1)%MAXSIZE;

intDeQueue(SqQueue&

Q,QElemType&

if(Q.front==Q.rear)

sanemptyqueue!

e=Q.base[Q.front];

Q.front=(Q.front+1)%MAXSIZE;

boolQueueEmpty(SqQueueQ)

if(Q.rear==Q.front)

//****************无向图*******************************

inti=0;

while((i<

G.vexnum)&

(G.vertices[i].data!

=u))

i++;

if(i<

returni;

else

return-1;

voidCreateUDG(ALGraph&

ArcNode*p,*s;

stwodata'

numbers:

s=(ArcNode*)malloc(sizeof(ArcNode));

s->

G.vertices[i].firstarc=s;

adjvex=i;

nextarc=G.vertices[j].firstarc;

G.vertices[j].firstarc=p;

--%d"

//****************遍历*******************************

boolvisited[MAXSIZE];

voidDFS(ALGraphG,intv)

visited[v]=true;

%3d"

G.vertices[v].data);

p=G.vertices[v].firstarc;

while(p)

visited[p->

adjvex])

DFS(G,p->

voidDFSTraverse(ALGraphG)

\nshowDFStraverresult:

for(intv=0;

v<

v++)

visited[v]=false;

for(v=0;

visited[v])

DFS(G,v);

intFirstAdjvex(ALGraphG,VNodev)

if(v.firstarc!

returnv.firstarc->

intNextAdjvex(ALGraphG,VNodev,intw)

p=v.firstarc;

while(p!

=NULL&

=w)

if(p->

adjvex==w&

nextarc!

returnp->

nextarc==NULL)

voidBFSTraverse(ALGraphG)

SqQueueQ;

\nshowBFStraverresult:

inti,e;

visited[i]=false;

InitQueue(Q);

visited[i])

visited[i]=true;

EnQueue(Q,i);

while(!

QueueEmpty(Q))

DeQueue(Q,e);

for(intu=FirstAdjvex(G,G.vertices[e]);

u>

=0;

u=NextAdjvex(G,G.vertices[e],u))

{

if(!

visited[u])

{

visited[u]=true;

printf("

G.vertices[u].data);

EnQueue(Q,u);

}

}

//****************主函数*******************************

CreateUDG(G);

DFSTraverse(G);

BFSTraverse(G);

三、实验说明:

1.类型定义(邻接表存储)#defineMAX_VERTEX_NUM8//顶点最大个数typedefstructArcNode

{intadjvex;

//边的权

}ArcNode;

//表结点#defineVertexTypeint//顶点元素类型

typedefstructVNode

{intdegree,indegree;

//顶点的度,入度VertexTypedata;

}VNode/*头结点*/,AdjList[MAX_VERTEX_NUM];

typedefstruct{AdjListvertices;

intvexnum,arcnu

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 广告传媒

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1