第十一周项目一

来源:互联网 发布:linux虚拟机kvm怎么用 编辑:程序博客网 时间:2024/06/04 17:42
  1. 烟台大学计算机学院  
  2.     
  3. 作者:王雪行
  4.   
  5. 问题描述:定义图的邻接矩阵和邻接表存储结构,实现其基本运算,并完成测试。 
  6.   
  7. 输入描述:无 
  8.   
  9. 输出描述:输出邻接矩阵,邻接表。 
  10.   
  11. */   
  12.   
  13.   
  14.   
  15. //graph.h:  
  16.   
  17.   
  18. #include <stdio.h>  
  19. #define MAXV 100  
  20. #define INF 32767  
  21. typedef int InfoType;  
  22. typedef struct  
  23. {  
  24.   
  25.     int no;  
  26.   
  27.   InfoType info;  
  28.   
  29.   
  30. }VertexType;  
  31.   
  32. typedef struct  
  33. {  
  34.     int edges[MAXV][MAXV];  
  35.   
  36.     int n,e;  
  37.   
  38.     VertexType vexs[MAXV];  
  39. }MGraph;//邻接矩阵  
  40.   
  41. typedef struct ANode  
  42. {  
  43.     int adjvex;  
  44.   
  45.     struct ANode *nextarc;  
  46.   
  47.      InfoType info;  
  48. }ArcNode;  
  49.   
  50. typedef struct Vnode  
  51. {  
  52.     int data;  
  53.     int count;  
  54.   
  55.     ArcNode *firstarc;  
  56.   
  57. }VNode;  
  58.   
  59. typedef VNode AdjList[MAXV];  
  60.   
  61. typedef struct  
  62. {  
  63.   
  64.     AdjList adjlist;  
  65.   
  66.     int n,e;  
  67. }ALGraph;  
  68.   
  69. void ArrayToMat(int *Arr, int n, MGraph &g); //用普通数组构造图的邻接矩阵  
  70. void ArrayToList(int *Arr, int n, ALGraph *&); //用普通数组构造图的邻接表  
  71. void MatToList(MGraph g,ALGraph *&G);//将邻接矩阵g转换成邻接表G  
  72. void ListToMat(ALGraph *G,MGraph &g);//将邻接表G转换成邻接矩阵g  
  73. void DispMat(MGraph g);//输出邻接矩阵g  
  74. void DispAdj(ALGraph *G);//输出邻接表G  
  75.   
  76.   
  77. //graph.cpp:  
  78.   
  79.   
  80. #include <stdio.h>  
  81. #include <malloc.h>  
  82. #include "graph.h"  
  83.   
  84. //功能:由一个反映图中顶点邻接关系的二维数组,构造出用邻接矩阵存储的图  
  85. //参数:Arr - 数组名,由于形式参数为二维数组时必须给出每行的元素个数,在此将参数Arr声明为一维数组名(指向int的指针)  
  86. //      n - 矩阵的阶数  
  87. //      g - 要构造出来的邻接矩阵数据结构  
  88. void ArrayToMat(int *Arr, int n, MGraph &g)  
  89. {  
  90.     int i,j,count=0;  //count用于统计边数,即矩阵中非0元素个数  
  91.     g.n=n;  
  92.     for (i=0; i<g.n; i++)  
  93.         for (j=0; j<g.n; j++)  
  94.         {  
  95.             g.edges[i][j]=Arr[i*n+j]; //将Arr看作n×n的二维数组,Arr[i*n+j]即是Arr[i][j]  
  96.             if(g.edges[i][j]!=0 && g.edges[i][j]!=INF)  
  97.                 count++;  
  98.         }  
  99.     g.e=count;  
  100. }  
  101.   
  102. void ArrayToList(int *Arr, int n, ALGraph *&G)  
  103. {  
  104.     int i,j,count=0;  //count用于统计边数,即矩阵中非0元素个数  
  105.     ArcNode *p;  
  106.     G=(ALGraph *)malloc(sizeof(ALGraph));  
  107.     G->n=n;  
  108.     for (i=0; i<n; i++)                 //给邻接表中所有头节点的指针域置初值  
  109.         G->adjlist[i].firstarc=NULL;  
  110.     for (i=0; i<n; i++)                 //检查邻接矩阵中每个元素  
  111.         for (j=n-1; j>=0; j--)  
  112.             if (Arr[i*n+j]!=0)      //存在一条边,将Arr看作n×n的二维数组,Arr[i*n+j]即是Arr[i][j]  
  113.             {  
  114.                 p=(ArcNode *)malloc(sizeof(ArcNode));   //创建一个节点*p  
  115.                 p->adjvex=j;  
  116.                 p->info=Arr[i*n+j];  
  117.                 p->nextarc=G->adjlist[i].firstarc;      //采用头插法插入*p  
  118.                 G->adjlist[i].firstarc=p;  
  119.             }  
  120.   
  121.     G->e=count;  
  122. }  
  123.   
  124. void MatToList(MGraph g, ALGraph *&G)  
  125. //将邻接矩阵g转换成邻接表G  
  126. {  
  127.     int i,j;  
  128.     ArcNode *p;  
  129.     G=(ALGraph *)malloc(sizeof(ALGraph));  
  130.     for (i=0; i<g.n; i++)                   //给邻接表中所有头节点的指针域置初值  
  131.         G->adjlist[i].firstarc=NULL;  
  132.     for (i=0; i<g.n; i++)                   //检查邻接矩阵中每个元素  
  133.         for (j=g.n-1; j>=0; j--)  
  134.             if (g.edges[i][j]!=0)       //存在一条边  
  135.             {  
  136.                 p=(ArcNode *)malloc(sizeof(ArcNode));   //创建一个节点*p  
  137.                 p->adjvex=j;  
  138.                 p->info=g.edges[i][j];  
  139.                 p->nextarc=G->adjlist[i].firstarc;      //采用头插法插入*p  
  140.                 G->adjlist[i].firstarc=p;  
  141.             }  
  142.     G->n=g.n;  
  143.     G->e=g.e;  
  144. }  
  145.   
  146. void ListToMat(ALGraph *G,MGraph &g)  
  147. //将邻接表G转换成邻接矩阵g  
  148. {  
  149.     int i,j;  
  150.     ArcNode *p;  
  151.     g.n=G->n;   //根据一楼同学“举报”改的。g.n未赋值,下面的初始化不起作用  
  152.     g.e=G->e;  
  153.     for (i=0; i<g.n; i++)   //先初始化邻接矩阵  
  154.         for (j=0; j<g.n; j++)  
  155.             g.edges[i][j]=0;  
  156.     for (i=0; i<G->n; i++)  //根据邻接表,为邻接矩阵赋值  
  157.     {  
  158.         p=G->adjlist[i].firstarc;  
  159.         while (p!=NULL)  
  160.         {  
  161.             g.edges[i][p->adjvex]=p->info;  
  162.             p=p->nextarc;  
  163.         }  
  164.     }  
  165. }  
  166.   
  167. void DispMat(MGraph g)  
  168. //输出邻接矩阵g  
  169. {  
  170.     int i,j;  
  171.     for (i=0; i<g.n; i++)  
  172.     {  
  173.         for (j=0; j<g.n; j++)  
  174.             if (g.edges[i][j]==INF)  
  175.                 printf("%3s","∞");  
  176.             else  
  177.                 printf("%3d",g.edges[i][j]);  
  178.         printf("\n");  
  179.     }  
  180. }  
  181.   
  182. void DispAdj(ALGraph *G)  
  183. //输出邻接表G  
  184. {  
  185.     int i;  
  186.     ArcNode *p;  
  187.     for (i=0; i<G->n; i++)  
  188.     {  
  189.         p=G->adjlist[i].firstarc;  
  190.         printf("%3d: ",i);  
  191.         while (p!=NULL)  
  192.         {  
  193.             printf("-->%d/%d ",p->adjvex,p->info);  
  194.             p=p->nextarc;  
  195.         }  
  196.         printf("\n");  
  197.     }  
  198. }  
  199.   
  200.   
  201. //main函数:  
  202.   
  203. #include <stdio.h>  
  204. #include <malloc.h>  
  205. #include "graph.h"  
  206.   
  207. int main()  
  208. {  
  209.     MGraph g1,g2;  
  210.     ALGraph *G1,*G2;  
  211.     int A[6][6]=  
  212.     {  
  213.         {0,5,0,7,0,0},  
  214.         {0,0,4,0,0,0},  
  215.         {8,0,0,0,0,9},  
  216.         {0,0,5,0,0,6},  
  217.         {0,0,0,5,0,0},  
  218.         {3,0,0,0,1,0}  
  219.     };  
  220.   
  221.     ArrayToMat(A[0], 6, g1);  //取二维数组的起始地址作实参,用A[0],因其实质为一维数组地址,与形参匹配  
  222.     printf(" 有向图g1的邻接矩阵:\n");  
  223.     DispMat(g1);  
  224.   
  225.     ArrayToList(A[0], 6, G1);  
  226.     printf(" 有向图G1的邻接表:\n");  
  227.     DispAdj(G1);  
  228.   
  229.     MatToList(g1,G2);  
  230.     printf(" 图g1的邻接矩阵转换成邻接表G2:\n");  
  231.     DispAdj(G2);  
  232.   
  233.     ListToMat(G1,g2);  
  234.     printf(" 图G1的邻接表转换成邻接邻阵g2:\n");  
  235.     DispMat(g2);  
  236.     printf("\n");  
  237.     return 0;  
  238. }  



运行结果:


原创粉丝点击