第十一周项目一

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



运行结果:


学习心得:

学会了建立邻接矩阵,邻接表存储结构的基本运算。

原创粉丝点击