问题及代码:
/*
* Copyright(c) 2017,烟台大学计算机学院
* All rights reserved.
* 文件名称:cpp1.
* 作 者:薛瑞琪
* 完成日期:2017 年 11 月 9 日
* 版 本 号:v1.0
*
* 问题描述: 假设图G采用邻接表存储,分别设计实现以下要求的算法:
(1)输出出图G中每个顶点的出度;
(2)求出图G中出度最大的一个顶点,输出该顶点编号;
(3)计算图G中出度为0的顶点数;
(4)判断图G中是否存在边<i,j>。
利用下图作为测试用图,输出结果。
* 输入描述:无需输入
* 程序输出:实现各种算法的函数的测试结果
*/
#include <stdio.h>
#include <malloc.h>
#include "graph.h"
int OutDegree(ALGraph *G,int v) //返回图G中编号为v的顶点的出度
{
ArcNode *p;
int n=0;
p=G->adjlist[v].firstarc;
while (p!=NULL)
{
n++;
p=p->nextarc;
}
return n;
}
void OutDs(ALGraph *G) //输出图G中每个顶点的出度
{
int i;
for (i=0; i<G->n; i++)
printf(" 顶点%d:%d\n",i,OutDegree(G,i));
}
void OutMaxDs(ALGraph *G) //输出图G中出度最大的一个顶点
{
int maxv=0,maxds=0,i,x;
for (i=0; i<G->n; i++)
{
x=OutDegree(G,i);
if (x>maxds)
{
maxds=x;
maxv=i;
}
}
printf("顶点%d,出度=%d\n",maxv,maxds);
}
void ZeroDs(ALGraph *G) //输出图G中出度为0的顶点数
{
int i,x;
for (i=0; i<G->n; i++)
{
x=OutDegree(G,i)