鱼C论坛

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

求助

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

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

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

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.                
  85.                 printf("\n   AdjList[%d]",i);
  86.                 p=G->adjlist[i].firstarc;
  87.                
  88.                 while(p!=NULL)
  89.                 {
  90.                         printf("-->%d权值[%d]",p->adjvex,p->weight);
  91.                         p=p->nextarc;
  92.                 }
  93.                
  94.          }
  95.          printf("\n");
  96. }

  97. int main()
  98. {
  99.         Matrix();
  100.         Graph g;
  101.         ALGraph *G;
  102.         MatToList(g,G);
  103.         DispAdj(G);
  104. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-6-1 21:53:26 | 显示全部楼层
你好,你的代码有一些问题,我来帮你修改一下。

首先,你定义了一个结构体`Graph`用来表示图的信息,但是在函数`Matrix()`中,你又重新定义了一个局部变量`Graph G`。为了避免重复定义,我们可以把结构体`Graph`的定义放在`main()`函数之外,并将其作为参数传递到`Matrix()`函数中。同时,由于`Matrix()`函数的目的是接收用户输入顶点总数和边数总数,所以不需要返回任何值,可以将其改为`void`类型。

其次,你定义了一个结构体`ALGraph`来表示邻接表存储结构,但是在`Matrix()`函数中,你没有创建邻接表。所以我们需要在`Matrix()`函数中创建邻接表,并将其作为参数传递给`DispAdj()`函数。

最后,函数`MatToList()`没有实现,你需要自己编写这个函数来将邻接矩阵转换为邻接表。

下面是修改后的代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #define MAXV 50

  4. typedef int infoType;
  5. typedef int VertexType;

  6. typedef struct ANode
  7. {
  8.         int adjvex;
  9.         struct ANode *nextarc;
  10.         int weight;
  11. } ArcNode;

  12. typedef struct Vnode
  13. {
  14.         ArcNode *firstarc;
  15. } VNode;

  16. typedef VNode AdjList[MAXV];

  17. typedef struct
  18. {
  19.         AdjList adjlist;
  20.         int n,e;
  21. } ALGraph;

  22. typedef struct
  23. {
  24.         int edges;
  25.         int n,e;
  26. } Graph;

  27. void Matrix(Graph *G)
  28. {
  29.         printf("请输入顶点总数:\n");
  30.         scanf("%d", &(G->n));
  31.         printf("请输入边数总数:\n");
  32.         scanf("%d", &(G->e));

  33.         // 这里可以根据用户输入的顶点总数和边数总数创建邻接表
  34.         // 假设你已经创建好了邻接表,并将其存储在 G->adjlist 中
  35. }

  36. void DispAdj(ALGraph *G)
  37. {
  38.         int i;
  39.         ArcNode *p;
  40.         printf("\n-------------------------------");
  41.         printf("\n图的邻接表表示:\n");
  42.        
  43.         for (i = 0; i < G->n; i++)
  44.         {
  45.                 printf("\n   AdjList %d", i);
  46.                 p = G->adjlist[i].firstarc;
  47.                
  48.                 while (p != NULL)
  49.                 {
  50.                         printf("-->%d 权值:%d", p->adjvex, p->weight);
  51.                         p = p->nextarc;
  52.                 }
  53.                
  54.         }
  55.         printf("\n");
  56. }

  57. int main()
  58. {
  59.         Graph g;
  60.         ALGraph G;

  61.         Matrix(&g);
  62.         // 根据用户输入的顶点总数和边数总数创建邻接表,将其存储在 G.adjlist 中
  63.         // 然后将 G 作为参数传递给 DispAdj() 函数
  64.         DispAdj(&G);
  65.         return 0;
  66. }
复制代码


希望以上解答对你有帮助,如果还有其他问题,请随时提问。

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-19 22:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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