//这是一个关于图的创建和输出图的故事,但是我一直卡在输出图模块那里,不知道为什么卡在那里没有输出,找不出啥问题,求大佬帮忙调试一下
#include <bits/stdc++.h>
using namespace std;
const int N = 3;
typedef struct arcnode {
int weight;
struct arcnode * nextarc;
int adjvex;
}ArcNode; //图邻接表中边结点
typedef struct {
ArcNode *firstarc;
int data;
}VNode;// 图邻接表中顶点结点
typedef struct {
int n, e;
VNode vertex[N];
}Graph;//图类型
void CreatGraph(Graph *G, int A[N][N], int n, int e) {//创建图
G = (Graph *)malloc(sizeof(Graph));
for(int i = 0; i < n; i++) G->vertex[i].firstarc = NULL;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(A[i][j]) {
ArcNode *p = (ArcNode *)malloc(sizeof(ArcNode));
p->adjvex = j;
p->nextarc = G->vertex[i].firstarc;
p->weight = A[i][j];
G->vertex[i].firstarc = p;
free(p);
}
}
}
G->e = e;
G->n = n;
}
void GrawMap(Graph *G) {//输出图,卡在这里,在for循环里输出任何字符控制台都没反应
ArcNode *q;
for(int i = 0; i < G->n; i++) {
q = G->vertex[i].firstarc;
printf("%3d: ", i);
while(q != NULL) {
printf("%3d[%d]->", q->adjvex, q->weight);
q = q -> nextarc;
}
printf("!\n");
}
}
int main() {
int a[3][3] = {0, 0, 0, 1, 0, 2, 0, 0, 0};
Graph *G;
CreatGraph(G, a, 3, 2);
GrawMap(G);
return 0;
}

初级数据结构图的输出问题
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
关注
参考通义千问和郭老师的小迷弟雅思莫了-编写提供,主要是为了帮助您快速解决问题,如下回答,您可以参考下,如有问题,评论区留言。
首先,感谢您提供代码。经过分析,我发现问题出在两个地方:
-
在
CreatGraph
函数中,当给图的边结点分配内存后不应该立即释放。free(p)
应该放在添加完所有边之后。 -
在
main
函数中,对Graph *G
进行动态内存分配。因为CreatGraph
函数需要修改传入的图结构指针的内容,所以在调用CreatGraph
函数前,应先对G
进行初始化。
修复后的代码如下:
#include <bits/stdc++.h> using namespace std; const int N = 3; typedef struct arcnode { int weight; struct arcnode * nextarc; int adjvex; } ArcNode; // 图邻接表中边结点 typedef struct { ArcNode *firstarc; int data; } VNode; // 图邻接表中顶点结点 typedef struct { int n, e; VNode vertex[N]; } Graph; // 图类型 void CreatGraph(Graph *G, int A[N][N], int n, int e) { // 创建图 G->n = n; G->e = e; for(int i = 0; i < n; i++) { G->vertex[i].firstarc = NULL; for(int j = 0; j < n; j++) { if(A[i][j]) { ArcNode *p = (ArcNode *)malloc(sizeof(ArcNode)); p->adjvex = j; p->weight = A[i][j]; p->nextarc = G->vertex[i].firstarc; G->vertex[i].firstarc = p; } } } } void GrawMap(Graph *G) { // 输出图 ArcNode *q; for(int i = 0; i < G->n; i++) { q = G->vertex[i].firstarc; printf("%3d: ", i); while(q != NULL) { printf("%3d[%d]->", q->adjvex, q->weight); q = q -> nextarc; } printf("!\n"); } } int main() { int a[3][3] = {0, 1, 2, 0, 0, 0, 0, 0, 0}; Graph *G = (Graph *)malloc(sizeof(Graph)); // 这里需要先为G分配内存 CreatGraph(G, a, 3, 2); GrawMap(G); // 不要在C++程序中忘记释放动态分配的内存(虽然此处程序结束会自动回收,但建议养成良好习惯) // for(int i = 0; i < G->n; i++) { // ArcNode *p = G->vertex[i].firstarc; // while(p) { // ArcNode *temp = p; // p = p->nextarc; // free(temp); // } // } // free(G); return 0; }
现在这段代码应该可以正确创建并输出图形了。注意,我还在注释中给出了释放动态分配内存的部分,实际使用时请根据需求选择是否释放。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用-
悬赏问题
- ¥15 STM32多路复用器ADC采样
- ¥20 Linux(ubuntu)操作相关问题
- ¥15 python深度学习代码求跑
- ¥100 对接美团闪购医药接口相关问题
- ¥15 嵌入式软件电子烟开发
- ¥15 职场 Excel 查重问题
- ¥15 求怎么用idea2021.3.2创建web项目并配置tomcat
- ¥100 or-tools的相关问题
- ¥15 有可能用平板通过拓展坞来烧录程序吗(keil5的那种)
- ¥15 状态图的并发态问题咨询