C 带结构的随机图
我试图实现一个随机图的代码,其中所有顶点都相互连接。边缘应随机选择。我写了这段代码:C 带结构的随机图,c,C,我试图实现一个随机图的代码,其中所有顶点都相互连接。边缘应随机选择。我写了这段代码: #include <stdio.h> #include <stdlib.h> #include <time.h> #define VOL 100 struct node{ int info; struct node *next; }; struct node *read_list(void){ struct node *p, *first=NUL
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define VOL 100
struct node{
int info;
struct node *next;
};
struct node *read_list(void){
struct node *p, *first=NULL;
int i,V;
for(i=0;i<V;i++){
p=malloc(sizeof(struct node));
p->next=first;
p->info=rand()%V;
first=p;
}
return(first);
}
void print_list(struct node *p){
while(p!=NULL){
printf("%d-> ", p->info);
p=p->next;
}
printf("NULL\n");
return;
}
int read_graph(struct node *G[]){
int i, V;
printf("Select a number of vertices:\n");
scanf("%d", &V);
for(i=0;i<V;i++){
printf("Adjacency list of vertex %d:\n", i);
G[i]=read_list();
}
return(V);
}
void print_graph(struct node *G[], int V){
int i;
printf("Adjacency lists of the graph:\n");
for(i=0;i<V;i++){
printf("Adjacency vertices to %d: ",i);
print_list(G[i]);
}
return;
}
int adj(int i, int j, struct node *G[]){
int r;
struct node *p;
p=G[i];
while (p!=NULL && p->info !=j)
p=p->next;
if(p==NULL)
r=1;
else
r=0;
return (r);
}
int main(){
srand(time(NULL));
struct node *G[VOL], *L;
int V;
V=read_graph(G);
print_graph(G, V);
L=read_list();
return 0;
}
#包括
#包括
#包括
#定义第100卷
结构节点{
国际信息;
结构节点*下一步;
};
结构节点*读取列表(无效){
结构节点*p,*first=NULL;
int i,V;
对于(i=0;inxt=first;
p->info=rand()%V;
第一个=p;
}
返回(第一);
}
无效打印列表(结构节点*p){
while(p!=NULL){
printf(“%d->”,p->info);
p=p->next;
}
printf(“NULL\n”);
返回;
}
int read_图(结构节点*G[]{
int i,V;
printf(“选择多个顶点:\n”);
scanf(“%d”和“&V”);
对于(i=0;不精确;
if(p==NULL)
r=1;
其他的
r=0;
回报率(r);
}
int main(){
srand(时间(空));
结构节点*G[VOL],*L;
INTV;
V=读取图(G);
打印图形(G,V);
L=读取列表();
返回0;
}
但是,它不起作用,我也不知道为什么。Xcode告诉我“构建成功”,但代码什么也不打印(目前只“选择一些顶点”):没有邻接列表,没有边。请检查它并告诉我错误在哪里好吗?在下面的
for
语句中,变量V
未初始化
...
struct node *read_list(void) {
struct node *p, *first = NULL;
int i, V; // <<<<<<<<<<<<<<<<<<< V not initialized
for (i = 0; i<V; i++) {
//^ trouble here
...
。。。
结构节点*读取列表(无效){
结构节点*p,*first=NULL;
int i,V;//“不起作用”不是错误描述。什么不起作用?它是否编译?它是否链接?它是否给出错误的输出?您期望得到什么?您的调试工作是什么?Xcode告诉我“构建成功”,但代码不打印任何内容(当前仅“选择多个顶点”):无邻接列表,无边…。@math.world。请编辑您的问题,并精确描述您遇到的问题。不要将相关信息放在注释中。或者键入顶点数,按Enter键,然后查看发生了什么。