第十一周项目一

来源:互联网 发布:linux查看组内用户 编辑:程序博客网 时间:2024/05/29 16:17
/*
Copyright (c++) 2017,烟台大学计算机与控制工程学院
文件名称:jcy
作 者:贾存钰
完成日期:2017年11月9日
问题描述:定义图的邻接矩阵和邻接表存储结构,实现其基本运算,并完成测试。 
头文件graph.h中定义相关的数据结构并声明用于完成基本运算的函数。
在graph.cpp中实现这些函数.用main.cpp中的main函数中完成测试。 

*/

[cpp] view plain copy
  1. #ifndef GRAPH_H_INCLUDED  
  2. #define GRAPH_H_INCLUDED  
  3. #define MAXV 100                //最大顶点个数  
  4. #define INF 32767  
  5.       //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.   
  59.   
  60. #endif // GRAPH_H_INCLUDED  

[cpp] view plain copy
  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. }  

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "graph.h"  
  4.   
  5. int main()  
  6. {  
  7.     MGraph g1,g2;  
  8.     ALGraph *G1,*G2;  
  9.     int A[6][6]=  
  10.     {  
  11.         {0,5,0,7,0,0},  
  12.         {0,0,4,0,0,0},  
  13.         {8,0,0,0,0,9},  
  14.         {0,0,5,0,0,6},  
  15.         {0,0,0,5,0,0},  
  16.         {3,0,0,0,1,0}  
  17.     };  
  18.   
  19.     ArrayToMat(A[0], 6, g1);  //取二维数组的起始地址作实参,用A[0],因其实质为一维数组地址,与形参匹配  
  20.     printf(" 有向图g1的邻接矩阵:\n");  
  21.     DispMat(g1);  
  22.   
  23.     ArrayToList(A[0], 6, G1);  
  24.     printf(" 有向图G1的邻接表:\n");  
  25.     DispAdj(G1);  
  26.   
  27.     MatToList(g1,G2);  
  28.     printf(" 图g1的邻接矩阵转换成邻接表G2:\n");  
  29.     DispAdj(G2);  
  30.   
  31.     ListToMat(G1,g2);  
  32.     printf(" 图G1的邻接表转换成邻接邻阵g2:\n");  
  33.     DispMat(g2);  
  34.     printf("\n");  
  35.     return 0;  
  36. }  

原创粉丝点击