调试这段代码:#include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX_VERTEX_NUM 20 struct ArcNode{ int adjvex; ArcNode *nextarc; int *info; };//表结点 typedef struct{ char data; ArcNode *firstarc; }VNode,AdjList[MAX_VERTEX_NUM];//头结点 struct ALGraph{ AdjList vertices; int vexnum; }; void CreateGraph(ALGraph *G) {scanf("%d",&G->vexnum); int i; for(i=0;i<G->vexnum;i++) {G->vertices[i].data=getchar(); G->vertices[i].firstarc=NULL; getchar(); } char num[20]; for(i=0;i<G->vexnum;i++) {char *token; int con[20];//存分割后的整型数 scanf("%s",num); token=strtok(num," "); int j=1; while(token!=NULL)//分割num,依次存入con {con[j]=atoi(token); j++; token=strtok(NULL," "); } int k; ArcNode *q; q=NULL; G->vertices[i].firstarc->nextarc=NULL; for(k=1;k<j;k+=2) {q=(ArcNode*)malloc(sizeof(ArcNode)); q->adjvex=con[k]; q->nextarc=G->vertices[i].firstarc; G->vertices[i].firstarc=q; q->info=(int*)malloc(sizeof(int)); *(q->info)=con[k+1]; } } } int main() {ALGraph *G0=(ALGraph*)malloc(sizeof(ALGraph));//一开始没写这一条,导致运行到Create函数中scanf(G->vexnum)是出现内存无效引用 CreateGraph(G0); int i; for(i=0;i<G0->vexnum;i++) {printf("%c ",G0->vertices[i].data); ArcNode *p; p=G0->vertices[i].firstarc; while(p) {printf("(%d,%d)%d ",i,p->adjvex,*(p->info)); p=p->nextarc; } printf("\n"); } free(G0); return 0; }
时间: 2023-12-07 16:05:37 浏览: 162
这段代码存在一些问题,需要进行调试:
1. 在 CreateGraph 函数中,每个顶点的邻接表首先应该被初始化为 NULL,而不是直接赋值为 G->vertices[i].firstarc。因此,应该将 G->vertices[i].firstarc=NULL 放到 G->vertices[i].data=getchar() 和 getchar() 之间。
2. 在为每个邻接点分配内存时,应该先判断该顶点是否有邻接点。如果没有,那么该邻接点应该是该顶点的第一个邻接点,此时应该将 G->vertices[i].firstarc 指向该邻接点;否则,应该将该邻接点接到邻接表的最后。因此,应该将 G->vertices[i].firstarc->nextarc=NULL 放到与上述判断语句相同的 if 语句中。
以下是修改后的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_VERTEX_NUM 20
struct ArcNode {
int adjvex;
ArcNode* nextarc;
int* info;
}; // 表结点
typedef struct {
char data;
ArcNode* firstarc;
} VNode, AdjList[MAX_VERTEX_NUM]; // 头结点
struct ALGraph {
AdjList vertices;
int vexnum;
};
void CreateGraph(ALGraph* G) {
scanf("%d", &G->vexnum);
int i;
for (i = 0; i < G->vexnum; i++) {
G->vertices[i].data = getchar();
G->vertices[i].firstarc = NULL;
getchar();
}
char num[20];
for (i = 0; i < G->vexnum; i++) {
char* token;
int con[20]; // 存分割后的整型数
scanf("%s", num);
token = strtok(num, " ");
int j = 1;
while (token != NULL) // 分割 num,依次存入 con
{
con[j] = atoi(token);
j++;
token = strtok(NULL, " ");
}
int k;
ArcNode *q, *p;
q = NULL;
for (k = 1; k < j; k += 2) {
q = (ArcNode*)malloc(sizeof(ArcNode));
q->adjvex = con[k];
q->nextarc = NULL;
q->info = (int*)malloc(sizeof(int));
*(q->info) = con[k + 1];
if (G->vertices[i].firstarc == NULL) {
G->vertices[i].firstarc = q;
}
else {
p = G->vertices[i].firstarc;
while (p->nextarc != NULL) {
p = p->nextarc;
}
p->nextarc = q;
}
}
}
}
int main() {
ALGraph* G0 = (ALGraph*)malloc(sizeof(ALGraph));
CreateGraph(G0);
int i;
for (i = 0; i < G0->vexnum; i++) {
printf("%c ", G0->vertices[i].data);
ArcNode* p;
p = G0->vertices[i].firstarc;
while (p) {
printf("(%d,%d)%d ", i, p->adjvex, *(p->info));
p = p->nextarc;
}
printf("\n");
}
free(G0);
return 0;
}
```
阅读全文