11周项目2 - 操作用邻接表存储的图

  1. //*Copyright  (c)2017,烟台大学计算机与控制工程学院*                     
  2. //*All rights reservrd.*                     
  3. //*文件名称 :main.cpp*                     
  4. //*作者:刘浩                     
  5. //*版本号:v1.0*                  
  6. //*问题描述:测试函数*                     
  7. //*输入描述:无*                     
  8. //*程序输出:无*  
  9. #include <stdio.h>  
  10. #include <malloc.h>  
  11. #include "graph.h"  
  12.   
  13. //返回图G中编号为v的顶点的出度  
  14. int OutDegree(ALGraph *G,int v)  
  15. {  
  16.     ArcNode *p;  
  17.     int n=0;  
  18.     p=G->adjlist[v].firstarc;  
  19.     while (p!=NULL)  
  20.     {  
  21.         n++;  
  22.         p=p->nextarc;  
  23.     }  
  24.     return n;  
  25. }  
  26.   
  27. //输出图G中每个顶点的出度  
  28. void OutDs(ALGraph *G)  
  29. {  
  30.     int i;  
  31.     for (i=0; i<G->n; i++)  
  32.         printf("  顶点%d:%d\n",i,OutDegree(G,i));  
  33. }  
  34.   
  35. //输出图G中出度最大的一个顶点  
  36. void OutMaxDs(ALGraph *G)  
  37. {  
  38.     int maxv=0,maxds=0,i,x;  
  39.     for (i=0; i<G->n; i++)  
  40.     {  
  41.         x=OutDegree(G,i);  
  42.         if (x>maxds)  
  43.         {  
  44.             maxds=x;  
  45.             maxv=i;  
  46.         }  
  47.     }  
  48.     printf("顶点%d,出度=%d\n",maxv,maxds);  
  49. }  
  50. //输出图G中出度为0的顶点数  
  51. void ZeroDs(ALGraph *G)  
  52. {  
  53.     int i,x;  
  54.     for (i=0; i<G->n; i++)  
  55.     {  
  56.         x=OutDegree(G,i);  
  57.         if (x==0)  
  58.             printf("%2d",i);  
  59.     }  
  60.     printf("\n");  
  61. }  
  62.   
  63. //返回图G中是否存在边<i,j>  
  64. bool Arc(ALGraph *G, int i,int j)  
  65. {  
  66.     ArcNode *p;  
  67.     bool found = false;  
  68.     p=G->adjlist[i].firstarc;  
  69.     while (p!=NULL)  
  70.     {  
  71.         if(p->adjvex==j)  
  72.         {  
  73.             found = true;  
  74.             break;  
  75.         }  
  76.         p=p->nextarc;  
  77.     }  
  78.     return found;  
  79. }  
  80.   
  81. int main()  
  82. {  
  83.     ALGraph *G;  
  84.     int A[7][7]=  
  85.     {  
  86.         {0,1,1,1,0,0,0},  
  87.         {0,0,0,0,1,0,0},  
  88.         {0,0,0,0,1,1,0},  
  89.         {0,0,0,0,0,0,1},  
  90.         {0,0,0,0,0,0,0},  
  91.         {0,0,0,1,1,0,1},  
  92.         {0,1,0,0,0,0,0}  
  93.     };  
  94.     ArrayToList(A[0], 7, G);  
  95.     printf("(1)各顶点出度:\n");  
  96.     OutDs(G);  
  97.     printf("(2)最大出度的顶点信息:");  
  98.     OutMaxDs(G);  
  99.     printf("(3)出度为0的顶点:");  
  100.     ZeroDs(G);  
  101.     printf("(4)边<2,6>存在吗?");  
  102.     if(Arc(G,2,6))  
  103.         printf("是\n");  
  104.     else  
  105.         printf("否\n");  
  106.     printf("\n");  
  107.     return 0;  
  108. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值