|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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);
- }
复制代码
求修改使得能输出邻接表
你好!根据你的代码,我发现了一些问题并进行了修改。具体修改如下:
1. 首先,在主函数中声明了一个`Graph g`变量,但没有输入顶点数和边数的值。我将这部分代码迁移到了`Matrix`函数中。
2. 在`Matrix`函数中,你需要遍历图的边,使用`for`循环语句,从1到`G.e`。在循环中,接收每条边的起点、终点和权值,并创建对应的邻接表。
3. 接着,在`Matrix`函数中,你需要将邻接表存储到邻接表图`ALGraph`结构中。首先要为`G`动态分配内存空间,然后逐个初始化邻接表节点。
下面是修改后的代码:
- #include <stdio.h>
- #include <stdlib.h>
- #define MAXV 50
- typedef int infoType;
- typedef int VertexType;
- typedef struct
- {
- int edges;
- int n, e;
- } Graph;
- typedef struct ANode
- {
- int adjvex;
- struct ANode *nextarc;
- int weight;
- } ArcNode;
- typedef struct Vnode
- {
- ArcNode *firstarc;
- } VNode;
- typedef VNode AdjList;
- typedef struct
- {
- AdjList adjlist[MAXV];
- int n, e;
- } ALGraph;
- void Matrix(Graph *G, ALGraph *AdjG)
- {
- printf("请输入顶点总数:\n");
- scanf("%d", &(G->n));
- printf("请输入边数总数:\n");
- scanf("%d", &(G->e));
- for (int i = 0; i < G->n; i++)
- {
- AdjG->adjlist[i].firstarc = NULL;
- }
- printf("请输入每条边的起点、终点和权值:\n");
- for (int k = 0; k < G->e; k++)
- {
- int start, end, weight;
- scanf("%d%d%d", &start, &end, &weight);
- ArcNode *newArc = (ArcNode *)malloc(sizeof(ArcNode));
- newArc->adjvex = end;
- newArc->weight = weight;
- newArc->nextarc = AdjG->adjlist[start].firstarc;
- AdjG->adjlist[start].firstarc = newArc;
- }
- }
- 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 = (ALGraph *)malloc(sizeof(ALGraph));
- Matrix(&g, G);
- DispAdj(G);
- return 0;
- }
复制代码
希望对你有所帮助!如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|