鱼C论坛

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

求助

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

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

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

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

typedef int infoType;
typedef int VertexType;
typedef struct
{
        int edges[MAXV][MAXV];
        int n,e;
}Graph;

typedef struct ANode
{
        int adjvex;
        struct ANode *nextarc;
        int weight; 
}ArcNode;
typedef struct Vnode
{
        ArcNode *firstarc;
}VNode;
typedef VNode AdjList[MAXV];
typedef struct
{
        AdjList adjlist;
        int n,e;
}ALGraph;

void Matrix()
{
        Graph G;
        printf("请输入顶点总数:\n");
        scanf("%d",&(G.n));
        printf("请输入边数总数:\n");
        scanf("%d",&G.e);
        for(int i=0;i<G.n;i++)
                for(int j=0;j<G.n;j++)
                        G.edges[i][j]=0;
        printf("请输入边的起始索引、终止索引和权值:\n");
        for(int i=0;i<G.e;i++)
        {
                int start;
                int end;
                int weight;
                scanf("%d %d %d",&start,&end,&weight);
                G.edges[start][end]=weight;
        }
        printf("输出有向带权图G的邻接矩阵:\n");
        for(int i=0;i<G.n;i++)
        {
                for(int j=0;j<G.n;j++)
                {
                        printf("%d\t",G.edges[i][j]);
                }
                printf("\n");
        }
}

void MatToList(Graph g,ALGraph *&G)    //创建邻接表 
{
        int i,j,n=g.n;
        ArcNode *p;
        G=(ALGraph *)malloc(sizeof(ALGraph));
        for(i=0;i<n;i++)
                G->adjlist[i].firstarc=NULL;
        for(i=0;i<n;i++)
                for(j=n-1;j>=0;j--)
                        if(g.edges[i][j]!=0)
                        {
                                p=(ArcNode *)malloc(sizeof(ArcNode));
                                p->adjvex=j;
                                p->nextarc=G->adjlist[i].firstarc;
                                G->adjlist[i].firstarc=p;
                        }
        G->n=n;
        G->e=g.e;
}

void DispAdj(ALGraph *G)
{
        int i;
        ArcNode *p;
        printf("\n-------------------------------");
        printf("\n图的邻接表表示:\n");
        
        for(i=0;i<G->n;i++)
        {
                
                printf("\n   AdjList[%d]",i);
                p=G->adjlist[i].firstarc;
                
                while(p!=NULL)
                {
                        printf("-->%d权值[%d]",p->adjvex,p->weight);
                        p=p->nextarc;
                }
                
         } 
         printf("\n");
}

int main()
{
        Matrix();
        Graph g;
        ALGraph *G;
        MatToList(g,G);
        DispAdj(G);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

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

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

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

下面是修改后的代码:
#include <stdio.h>
#include <stdlib.h>

#define MAXV 50

typedef int infoType;
typedef int VertexType;

typedef struct ANode
{
        int adjvex;
        struct ANode *nextarc;
        int weight; 
} ArcNode;

typedef struct Vnode
{
        ArcNode *firstarc;
} VNode;

typedef VNode AdjList[MAXV];

typedef struct
{
        AdjList adjlist;
        int n,e;
} ALGraph;

typedef struct
{
        int edges;
        int n,e;
} Graph;

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

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

void DispAdj(ALGraph *G)
{
        int i;
        ArcNode *p;
        printf("\n-------------------------------");
        printf("\n图的邻接表表示:\n");
        
        for (i = 0; i < G->n; i++)
        {
                printf("\n   AdjList %d", i);
                p = G->adjlist[i].firstarc;
                
                while (p != NULL)
                {
                        printf("-->%d 权值:%d", p->adjvex, p->weight);
                        p = p->nextarc;
                }
                
        } 
        printf("\n");
}

int main()
{
        Graph g;
        ALGraph G;

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

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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-18 20:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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