graph.cpp文件以邻接矩阵和邻接表的形式来表示图的存储

    1. #include<stdio.h>
    2. #include<stdlib.h>
    3. #include<math.h>
    4. #define INF 32767 //定义∞
    5. #define MAXV 100 //最大顶点个数
    6. typedef char InfoType;
    7. //-----------------------------定义邻接矩阵类型------------------------------------------------
    8. typedef struct{
    9. int no; //顶点编号
    10. InfoType info; //顶点其它信息
    11. }VertexType; //顶点类型
    12. typedef struct{
    13. int edges[MAXV][MAXV]; //邻接矩阵数组
    14. int n,e; //顶点数,边数
    15. VertexType vex[MAXV]; //存放顶点信息
    16. }MatGraph; //图的邻接矩阵类型
    17. //------------------------------定义邻接表类型-------------------------------------------------
    18. typedef struct ArcNode{
    19. int adjvex; //该边邻接点编号
    20. ArcNode* nextarc; //指向下一条边的指针
    21. int weight; //该边的相关信息:权重
    22. }ArcNode; //边节点类型
    23. typedef struct VNode{
    24. InfoType info; //顶点信息
    25. int count; //顶点入度
    26. ArcNode * firstarc; //指向第一条边
    27. }VNode; //邻接表表头类型
    28. typedef struct{
    29. VNode adjlist[MAXV]; //邻接表表头数组
    30. int n,e; //图的顶点数和边数
    31. }AdjGraph; //图的邻接表类型
    32. //-------------------------邻接矩阵的基本操作------------------------------------------------
    33. void CreateMatGraph(MatGraph &g,int A[MAXV][MAXV],int n,int e){ //创建邻接矩阵
    34. g.n=n;
    35. g.e=e;
    36. for(int i=0;i<g.n;i++){
    37. for(int j=0;j<g.n;j++){
    38. g.edges[i][j]=A[i][j];
    39. }
    40. }
    41. }
    42. void DispMatGraph(MatGraph g) //输出邻接矩阵
    43. {
    44. for(int i=0;i<g.n;i++){
    45. for(int j=0;j<g.n;j++)
    46. {
    47. if(g.edges[i][j]!=INF)
    48. printf("%4d",g.edges[i][j]);
    49. else
    50. printf("%4s"," ∞");
    51. }
    52. printf("\n");
    53. }
    54. }
    55. //-------------------------邻接表的基本操作--------------------------------------------------
    56. void CreateAdjGraph(AdjGraph *&G,int A[MAXV][MAXV],int n,int e) //创建图的邻接表
    57. {
    58. ArcNode *p;
    59. G=(AdjGraph*)malloc(sizeof(AdjGraph));
    60. for(int i=0;i<n;i++){
    61. G->adjlist[i].firstarc=NULL; //给邻接表中所有头结点的额指针域设置初值
    62. }
    63. for(int i=0;i<n;i++){
    64. for(int j=n-1;j>=0;j--){ //存在一条有向图边<i,j>,从i->j做一遍,如果是无向图的边(i,j),必须从j->i再做一边
    65. if(A[i][j]!=0&&A[i][j]!=INF){
    66. p=(ArcNode*)malloc(sizeof(ArcNode)); //创建一个节点P
    67. p->adjvex=j;
    68. p->weight=A[i][j];
    69. p->nextarc=G->adjlist[i].firstarc; //采用头插法插入节点P,插到i结点后面
    70. G->adjlist[i].firstarc=p;
    71. }
    72. }
    73. }
    74. G->n=n;
    75. G->e=n;
    76. }
    77. void DispAdjGraph(AdjGraph *G) //输出邻接表
    78. {
    79. ArcNode *p;
    80. for(int i=0;i<G->n;i++){
    81. p=G->adjlist[i].firstarc;
    82. printf("%3d",i);
    83. while(p!=NULL){
    84. printf("%3d[%d]->",p->adjvex,p->weight);
    85. p=p->nextarc;
    86. }
    87. printf("^\n");
    88. }
    89. }
    90. void DestroyAdjGraph(AdjGraph *&G){ //销毁图的邻接表
    91. ArcNode * pre,*p;
    92. for(int i=0;i<G->n;i++){ //扫描所有的单链表
    93. pre=G->adjlist[i].firstarc; //p指向第i个单链表的首结点
    94. if(pre!=NULL){
    95. p=pre->nextarc;
    96. while(p!=NULL){
    97. free(pre); //释放第i个单链表的所有边结点
    98. pre=p;
    99. p=p->nextarc;
    100. }
    101. free(pre);
    102. }
    103. }
    104. free(G); //释放头结点数组
    105. }

    操作:

    #include"graph.cpp"
    int main(){
        MatGraph g;
        AdjGraph * G;
        int A[MAXV][MAXV]={
            {0,5,INF,7,INF,INF},
            {INF,0,4,INF,INF,INF},
            {8,INF,0,INF,INF,9},
            {INF,INF,5,0,INF,6},
            {INF,INF,INF,5,0,INF},
            {3,INF,INF,INF,1,0}
        };
        int n=6,e=10;
        CreateMatGraph(g,A,n,e);
        printf("(1)图G的邻接矩阵:\n");
        DispMatGraph(g);
        CreateAdjGraph(G,A,n,e);
        printf("(2)图G的邻接表:\n");
        DispAdjGraph(G);
        printf("(3)销毁图G的邻接表\n");
        DestroyAdjGraph(G);
        return 1;   
    }
    

    运行结果:
    image.png