鱼C论坛

 找回密码
 立即注册
查看: 866|回复: 1

[已解决]求助

[复制链接]
发表于 2024-6-1 23:23:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAXV 50

  4. typedef int infoType;
  5. typedef int VertexType;
  6. typedef struct
  7. {
  8.         int edges[MAXV][MAXV];
  9.         int n,e;
  10. }Graph;

  11. typedef struct ANode
  12. {
  13.         int adjvex;
  14.         struct ANode *nextarc;
  15.         int weight;
  16. }ArcNode;
  17. typedef struct Vnode
  18. {
  19.         ArcNode *firstarc;
  20. }VNode;
  21. typedef VNode AdjList[MAXV];
  22. typedef struct
  23. {
  24.         AdjList adjlist;
  25.         int n,e;
  26. }ALGraph;

  27. void Matrix()
  28. {
  29.         Graph G;
  30.         printf("请输入顶点总数:\n");
  31.         scanf("%d",&(G.n));
  32.         printf("请输入边数总数:\n");
  33.         scanf("%d",&G.e);
  34.         for(int i=0;i<G.n;i++)
  35.                 for(int j=0;j<G.n;j++)
  36.                         G.edges[i][j]=0;
  37.         printf("请输入边的起始索引、终止索引和权值:\n");
  38.         for(int i=0;i<G.e;i++)
  39.         {
  40.                 int start;
  41.                 int end;
  42.                 int weight;
  43.                 scanf("%d %d %d",&start,&end,&weight);
  44.                 G.edges[start][end]=weight;
  45.         }
  46.         printf("输出有向带权图G的邻接矩阵:\n");
  47.         for(int i=0;i<G.n;i++)
  48.         {
  49.                 for(int j=0;j<G.n;j++)
  50.                 {
  51.                         printf("%d\t",G.edges[i][j]);
  52.                 }
  53.                 printf("\n");
  54.         }
  55. }

  56. void MatToList(Graph g,ALGraph *&G)    //创建邻接表
  57. {
  58.         int i,j,n=g.n;
  59.         ArcNode *p;
  60.         G=(ALGraph *)malloc(sizeof(ALGraph));
  61.         for(i=0;i<n;i++)
  62.                 G->adjlist[i].firstarc=NULL;
  63.         for(i=0;i<n;i++)
  64.                 for(j=n-1;j>=0;j--)
  65.                         if(g.edges[i][j]!=0)
  66.                         {
  67.                                 p=(ArcNode *)malloc(sizeof(ArcNode));
  68.                                 p->adjvex=j;
  69.                                 p->nextarc=G->adjlist[i].firstarc;
  70.                                 G->adjlist[i].firstarc=p;
  71.                         }
  72.         G->n=n;
  73.         G->e=g.e;
  74. }

  75. void DispAdj(ALGraph *G)
  76. {
  77.         int i;
  78.         ArcNode *p;
  79.         printf("\n-------------------------------");
  80.         printf("\n图的邻接表表示:\n");
  81.        
  82.         for(i=0;i<G->n;i++)
  83.         {
  84.                 printf("\n   AdjList[%d]",i);
  85.                 p=G->adjlist[i].firstarc;
  86.                
  87.                 while(p!=NULL)
  88.                 {
  89.                         printf("-->%d权值[%d]",p->adjvex,p->weight);
  90.                         p=p->nextarc;
  91.                 }
  92.                
  93.          }
  94.          printf("\n");
  95. }

  96. int main()
  97. {
  98.         Matrix();
  99.         Graph g;
  100.         ALGraph *G;
  101.         MatToList(g,G);
  102.         DispAdj(G);
  103. }
复制代码

求修改使得能输出邻接表
最佳答案
2024-6-1 23:24:17
你好!根据你的代码,我发现了一些问题并进行了修改。具体修改如下:

1. 首先,在主函数中声明了一个`Graph g`变量,但没有输入顶点数和边数的值。我将这部分代码迁移到了`Matrix`函数中。

2. 在`Matrix`函数中,你需要遍历图的边,使用`for`循环语句,从1到`G.e`。在循环中,接收每条边的起点、终点和权值,并创建对应的邻接表。

3. 接着,在`Matrix`函数中,你需要将邻接表存储到邻接表图`ALGraph`结构中。首先要为`G`动态分配内存空间,然后逐个初始化邻接表节点。

下面是修改后的代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAXV 50

  4. typedef int infoType;
  5. typedef int VertexType;
  6. typedef struct
  7. {
  8.     int edges;
  9.     int n, e;
  10. } Graph;

  11. typedef struct ANode
  12. {
  13.     int adjvex;
  14.     struct ANode *nextarc;
  15.     int weight;
  16. } ArcNode;
  17. typedef struct Vnode
  18. {
  19.     ArcNode *firstarc;
  20. } VNode;
  21. typedef VNode AdjList;
  22. typedef struct
  23. {
  24.     AdjList adjlist[MAXV];
  25.     int n, e;
  26. } ALGraph;

  27. void Matrix(Graph *G, ALGraph *AdjG)
  28. {
  29.     printf("请输入顶点总数:\n");
  30.     scanf("%d", &(G->n));
  31.     printf("请输入边数总数:\n");
  32.     scanf("%d", &(G->e));
  33.     for (int i = 0; i < G->n; i++)
  34.     {
  35.         AdjG->adjlist[i].firstarc = NULL;
  36.     }
  37.     printf("请输入每条边的起点、终点和权值:\n");
  38.     for (int k = 0; k < G->e; k++)
  39.     {
  40.         int start, end, weight;
  41.         scanf("%d%d%d", &start, &end, &weight);
  42.         ArcNode *newArc = (ArcNode *)malloc(sizeof(ArcNode));
  43.         newArc->adjvex = end;
  44.         newArc->weight = weight;
  45.         newArc->nextarc = AdjG->adjlist[start].firstarc;
  46.         AdjG->adjlist[start].firstarc = newArc;
  47.     }
  48. }

  49. void DispAdj(ALGraph *G)
  50. {
  51.     int i;
  52.     ArcNode *p;
  53.     printf("\n-------------------------------");
  54.     printf("\n图的邻接表表示:\n");

  55.     for (i = 0; i < G->n; i++)
  56.     {
  57.         printf("\n   AdjList[%d]", i);
  58.         p = G->adjlist[i].firstarc;

  59.         while (p != NULL)
  60.         {
  61.             printf("-->%d(权值:%d)", p->adjvex, p->weight);
  62.             p = p->nextarc;
  63.         }
  64.     }
  65.     printf("\n");
  66. }

  67. int main()
  68. {
  69.     Graph g;
  70.     ALGraph *G = (ALGraph *)malloc(sizeof(ALGraph));
  71.     Matrix(&g, G);
  72.     DispAdj(G);
  73.     return 0;
  74. }
复制代码


希望对你有所帮助!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-6-1 23:24:17 | 显示全部楼层    本楼为最佳答案   
你好!根据你的代码,我发现了一些问题并进行了修改。具体修改如下:

1. 首先,在主函数中声明了一个`Graph g`变量,但没有输入顶点数和边数的值。我将这部分代码迁移到了`Matrix`函数中。

2. 在`Matrix`函数中,你需要遍历图的边,使用`for`循环语句,从1到`G.e`。在循环中,接收每条边的起点、终点和权值,并创建对应的邻接表。

3. 接着,在`Matrix`函数中,你需要将邻接表存储到邻接表图`ALGraph`结构中。首先要为`G`动态分配内存空间,然后逐个初始化邻接表节点。

下面是修改后的代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAXV 50

  4. typedef int infoType;
  5. typedef int VertexType;
  6. typedef struct
  7. {
  8.     int edges;
  9.     int n, e;
  10. } Graph;

  11. typedef struct ANode
  12. {
  13.     int adjvex;
  14.     struct ANode *nextarc;
  15.     int weight;
  16. } ArcNode;
  17. typedef struct Vnode
  18. {
  19.     ArcNode *firstarc;
  20. } VNode;
  21. typedef VNode AdjList;
  22. typedef struct
  23. {
  24.     AdjList adjlist[MAXV];
  25.     int n, e;
  26. } ALGraph;

  27. void Matrix(Graph *G, ALGraph *AdjG)
  28. {
  29.     printf("请输入顶点总数:\n");
  30.     scanf("%d", &(G->n));
  31.     printf("请输入边数总数:\n");
  32.     scanf("%d", &(G->e));
  33.     for (int i = 0; i < G->n; i++)
  34.     {
  35.         AdjG->adjlist[i].firstarc = NULL;
  36.     }
  37.     printf("请输入每条边的起点、终点和权值:\n");
  38.     for (int k = 0; k < G->e; k++)
  39.     {
  40.         int start, end, weight;
  41.         scanf("%d%d%d", &start, &end, &weight);
  42.         ArcNode *newArc = (ArcNode *)malloc(sizeof(ArcNode));
  43.         newArc->adjvex = end;
  44.         newArc->weight = weight;
  45.         newArc->nextarc = AdjG->adjlist[start].firstarc;
  46.         AdjG->adjlist[start].firstarc = newArc;
  47.     }
  48. }

  49. void DispAdj(ALGraph *G)
  50. {
  51.     int i;
  52.     ArcNode *p;
  53.     printf("\n-------------------------------");
  54.     printf("\n图的邻接表表示:\n");

  55.     for (i = 0; i < G->n; i++)
  56.     {
  57.         printf("\n   AdjList[%d]", i);
  58.         p = G->adjlist[i].firstarc;

  59.         while (p != NULL)
  60.         {
  61.             printf("-->%d(权值:%d)", p->adjvex, p->weight);
  62.             p = p->nextarc;
  63.         }
  64.     }
  65.     printf("\n");
  66. }

  67. int main()
  68. {
  69.     Graph g;
  70.     ALGraph *G = (ALGraph *)malloc(sizeof(ALGraph));
  71.     Matrix(&g, G);
  72.     DispAdj(G);
  73.     return 0;
  74. }
复制代码


希望对你有所帮助!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-10 02:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表