我已经编写了一个基于小型c图的实现的代码,并相应地列出了图的顶点的邻接列表。我的上述代码是:
#include<stdio.h>
#include<stdlib.h>
struct node {
int info;
struct node* next;
}* z, *adjv[50], *t;
void insert() {
int j, v, e, c, d, i;
z = (struct node*)malloc(sizeof(struct node));
z->next = z;
scanf("%d%d", &v, &e);
for (j = 1; j <= v; j++) {
adjv[j] = z;
}
for (j = 1; j <= e; j++) {
scanf("%d%d", &c, &d);
t = (struct node*)malloc(sizeof(struct node));
t->info = c;
t->next = adjv[d];
adjv[d] = t;
t = (struct node*)malloc(sizeof(struct node));
t->info = d;
t->next = adjv[c];
adjv[c] = t;
}
for (i = 1; i <= e; i++) {
while (adjv[i] != z) {
printf("%d", adjv[i]->info);
adjv[i] = adjv[i]->next;
}
}
}
int main() {
insert();
return 0;
}
当我为它提供vertices = 4 edges = 2且edge为(1,2)(3,4)时,它不会将其显示为断开连接的图形,因为邻接列表仅显示1和2的值。请帮我纠正这个问题,以便显示正确的邻接列表