*计算机控制工程学院
*班级:计156-2
*姓名:陈飞
*问题及代码:
int main()
{
ALGraph *G;
int A[9][9]=
{
{0,1,1,0,0,0,0,0,0},
{0,0,0,1,1,0,0,0,0},
{0,0,0,0,1,1,0,0,0},
{0,0,0,0,0,0,1,0,0},
{0,0,0,0,0,1,1,0,0},
{0,0,0,0,0,0,0,1,0},
{0,0,0,0,0,0,0,1,1},
{0,0,0,0,0,0,0,0,1},
{0,0,0,0,0,0,0,0,0}
}; //请画出对应的有向图
ArrayToList(A[0], 9, G);
printf("离顶点0最远的顶点:%d",Maxdist(G,0));
return 0;
}
[cpp] view plain copy
int Maxdist(ALGraph *G,int v)
{
ArcNode *p;
int i,j,k;
int Qu[MAXV]; //环形队列
int visited[MAXV]; //访问标记数组
int front=0,rear=0; //队列的头、尾指针
for (i=0; i<G->n; i++) //初始化访问标志数组
visited[i]=0;
rear++;
Qu[rear]=v; //顶点v进队
visited[v]=1; //标记v已访问
while (rear!=front)
{
front=(front+1)%MAXV;
k=Qu[front]; //顶点k出队
p=G->adjlist[k].firstarc; //找第一个邻接点
while (p!=NULL) //所有未访问过的相邻点进队
{
j=p->adjvex; //邻接点为顶点j
if (visited[j]==0) //若j未访问过
{
visited[j]=1;
rear=(rear+1)%MAXV;
Qu[rear]=j; //进队
}
p=p->nextarc; //找下一个邻接点
}
}
return k;
}