第十二周 项目二--操作用邻接表存储的图

main.cpp

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

graph.h

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #ifndef GRAPH_H_INCLUDED  
  2. #define GRAPH_H_INCLUDED  
  3.   
  4. #define MAXV 100                //最大顶点个数  
  5. #define INF 32767       //INF表示∞  
  6. typedef int InfoType;  
  7.   
  8. //以下定义邻接矩阵类型  
  9. typedef struct  
  10. {  
  11.     int no;                     //顶点编号  
  12.     InfoType info;              //顶点其他信息,在此存放带权图权值  
  13. } VertexType;                   //顶点类型  
  14.   
  15. typedef struct                  //图的定义  
  16. {  
  17.     int edges[MAXV][MAXV];      //邻接矩阵  
  18.     int n,e;                    //顶点数,弧数  
  19.     VertexType vexs[MAXV];      //存放顶点信息  
  20. } MGraph;                       //图的邻接矩阵类型  
  21.   
  22. //以下定义邻接表类型  
  23. typedef struct ANode            //弧的结点结构类型  
  24. {  
  25.     int adjvex;                 //该弧的终点位置  
  26.     struct ANode *nextarc;      //指向下一条弧的指针  
  27.     InfoType info;              //该弧的相关信息,这里用于存放权值  
  28. } ArcNode;  
  29.   
  30. typedef int Vertex;  
  31.   
  32. typedef struct Vnode            //邻接表头结点的类型  
  33. {  
  34.     Vertex data;                //顶点信息  
  35.     int count;                  //存放顶点入度,只在拓扑排序中用  
  36.     ArcNode *firstarc;          //指向第一条弧  
  37. } VNode;  
  38.   
  39. typedef VNode AdjList[MAXV];    //AdjList是邻接表类型  
  40.   
  41. typedef struct  
  42. {  
  43.     AdjList adjlist;            //邻接表  
  44.     int n,e;                    //图中顶点数n和边数e  
  45. } ALGraph;                      //图的邻接表类型  
  46.   
  47. //功能:由一个反映图中顶点邻接关系的二维数组,构造出用邻接矩阵存储的图  
  48. //参数:Arr - 数组名,由于形式参数为二维数组时必须给出每行的元素个数,在此将参数Arr声明为一维数组名(指向int的指针)  
  49. //      n - 矩阵的阶数  
  50. //      g - 要构造出来的邻接矩阵数据结构  
  51. void ArrayToMat(int *Arr, int n, MGraph &g); //用普通数组构造图的邻接矩阵  
  52. void ArrayToList(int *Arr, int n, ALGraph *&); //用普通数组构造图的邻接表  
  53. void MatToList(MGraph g,ALGraph *&G);//将邻接矩阵g转换成邻接表G  
  54. void ListToMat(ALGraph *G,MGraph &g);//将邻接表G转换成邻接矩阵g  
  55. void DispMat(MGraph g);//输出邻接矩阵g  
  56. void DispAdj(ALGraph *G);//输出邻接表G  
  57.   
  58. #endif // GRAPH_H_INCLUDED  

graph.cpp


[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "graph.h"  
  4.   
  5. //功能:由一个反映图中顶点邻接关系的二维数组,构造出用邻接矩阵存储的图  
  6. //参数:Arr - 数组名,由于形式参数为二维数组时必须给出每行的元素个数,在此将参数Arr声明为一维数组名(指向int的指针)  
  7. //      n - 矩阵的阶数  
  8. //      g - 要构造出来的邻接矩阵数据结构  
  9. void ArrayToMat(int *Arr, int n, MGraph &g)  
  10. {  
  11.     int i,j,count=0;  //count用于统计边数,即矩阵中非0元素个数  
  12.     g.n=n;  
  13.     for (i=0; i<g.n; i++)  
  14.         for (j=0; j<g.n; j++)  
  15.         {  
  16.             g.edges[i][j]=Arr[i*n+j]; //将Arr看作n×n的二维数组,Arr[i*n+j]即是Arr[i][j],计算存储位置的功夫在此应用  
  17.             if(g.edges[i][j]!=0 && g.edges[i][j]!=INF)  
  18.                 count++;  
  19.         }  
  20.     g.e=count;  
  21. }  
  22.   
  23. void ArrayToList(int *Arr, int n, ALGraph *&G)  
  24. {  
  25.     int i,j,count=0;  //count用于统计边数,即矩阵中非0元素个数  
  26.     ArcNode *p;  
  27.     G=(ALGraph *)malloc(sizeof(ALGraph));  
  28.     G->n=n;  
  29.     for (i=0; i<n; i++)                 //给邻接表中所有头节点的指针域置初值  
  30.         G->adjlist[i].firstarc=NULL;  
  31.     for (i=0; i<n; i++)                 //检查邻接矩阵中每个元素  
  32.         for (j=n-1; j>=0; j--)  
  33.             if (Arr[i*n+j]!=0)      //存在一条边,将Arr看作n×n的二维数组,Arr[i*n+j]即是Arr[i][j]  
  34.             {  
  35.                 p=(ArcNode *)malloc(sizeof(ArcNode));   //创建一个节点*p  
  36.                 p->adjvex=j;  
  37.                 p->info=Arr[i*n+j];  
  38.                 p->nextarc=G->adjlist[i].firstarc;      //采用头插法插入*p  
  39.                 G->adjlist[i].firstarc=p;  
  40.             }  
  41.   
  42.     G->e=count;  
  43. }  
  44.   
  45. void MatToList(MGraph g, ALGraph *&G)  
  46. //将邻接矩阵g转换成邻接表G  
  47. {  
  48.     int i,j;  
  49.     ArcNode *p;  
  50.     G=(ALGraph *)malloc(sizeof(ALGraph));  
  51.     for (i=0; i<g.n; i++)                   //给邻接表中所有头节点的指针域置初值  
  52.         G->adjlist[i].firstarc=NULL;  
  53.     for (i=0; i<g.n; i++)                   //检查邻接矩阵中每个元素  
  54.         for (j=g.n-1; j>=0; j--)  
  55.             if (g.edges[i][j]!=0)       //存在一条边  
  56.             {  
  57.                 p=(ArcNode *)malloc(sizeof(ArcNode));   //创建一个节点*p  
  58.                 p->adjvex=j;  
  59.                 p->info=g.edges[i][j];  
  60.                 p->nextarc=G->adjlist[i].firstarc;      //采用头插法插入*p  
  61.                 G->adjlist[i].firstarc=p;  
  62.             }  
  63.     G->n=g.n;  
  64.     G->e=g.e;  
  65. }  
  66.   
  67. void ListToMat(ALGraph *G,MGraph &g)  
  68. //将邻接表G转换成邻接矩阵g  
  69. {  
  70.     int i,j;  
  71.     ArcNode *p;  
  72.     g.n=G->n;   //根据一楼同学“举报”改的。g.n未赋值,下面的初始化不起作用  
  73.     g.e=G->e;  
  74.     for (i=0; i<g.n; i++)   //先初始化邻接矩阵  
  75.         for (j=0; j<g.n; j++)  
  76.             g.edges[i][j]=0;  
  77.     for (i=0; i<G->n; i++)  //根据邻接表,为邻接矩阵赋值  
  78.     {  
  79.         p=G->adjlist[i].firstarc;  
  80.         while (p!=NULL)  
  81.         {  
  82.             g.edges[i][p->adjvex]=p->info;  
  83.             p=p->nextarc;  
  84.         }  
  85.     }  
  86. }  
  87.   
  88. void DispMat(MGraph g)  
  89. //输出邻接矩阵g  
  90. {  
  91.     int i,j;  
  92.     for (i=0; i<g.n; i++)  
  93.     {  
  94.         for (j=0; j<g.n; j++)  
  95.             if (g.edges[i][j]==INF)  
  96.                 printf("%3s","∞");  
  97.             else  
  98.                 printf("%3d",g.edges[i][j]);  
  99.         printf("\n");  
  100.     }  
  101. }  
  102.   
  103. void DispAdj(ALGraph *G)  
  104. //输出邻接表G  
  105. {  
  106.     int i;  
  107.     ArcNode *p;  
  108.     for (i=0; i<G->n; i++)  
  109.     {  
  110.         p=G->adjlist[i].firstarc;  
  111.         printf("%3d: ",i);  
  112.         while (p!=NULL)  
  113.         {  
  114.             printf("-->%d/%d ",p->adjvex,p->info);  
  115.             p=p->nextarc;  
  116.         }  
  117.         printf("\n");  
  118.     }  
  119.  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值