邻接列表的图形声明
Graph Declaration for Adjacency List
我正在得到
错误:从类型"struct listnode*"分配给类型"struct listnode"时类型不兼容
错误:'->' have('struct listnode')的类型参数无效
我在这里的指针做错了什么?提前致谢
#include <stdio.h>
#include <stdlib.h>
struct listnode{
int vertex;
struct listnode *next;
};
struct Graph{
int V;
int E;
struct listnode *adj;
};
void print(struct Graph *G);
struct Graph* initategraph(){
int i,source,dest;
struct listnode *temp;
struct Graph *G=(struct Graph*)malloc(sizeof(struct Graph));
if(!G)
{printf("Memory not available");
return;
}
printf("Enter the vertex no. and edges n");
scanf("%d %d",&G->V,&G->E);
G->adj = malloc(G->V*sizeof(struct listnode));
for(i=0;i<G->V;i++)
{
G->adj[i] =(struct listnode *)malloc(sizeof(struct listnode));
G->adj[i]->vertex = i;
G->adj[i]->next=G->adj[i];
}
for(i=0;i<G->E;i++){
scanf("%d %d",&source,&dest);
temp= (struct listnode *)malloc(sizeof(struct listnode));
temp->vertex=dest;
temp->next=G->adj[source];
G->adj[source]->next=temp;
temp =(struct listnode *)malloc(sizeof(struct listnode));
temp->vertex=source;
temp->next=G->adj[dest];
G->adj[dest]->next=temp;
}
return G;
}
void print(struct Graph *G){
int i;
struct listnode *temp;
for(i=0;i<G->V;i++){
temp=G->adj[i];
while(temp)
{
printf("%d --> ",temp->vertex);
temp=temp->next;
}
printf("nn");
}
}
void main()
{
struct Graph *G;
G=initategraph();
print(G);
}
G->adj = malloc(G->V*sizeof(struct listnode));
已经分配了一个动态内存,其中包含 struct listnode
类型的 G-V
元素。因此,请从循环内部删除此语义不正确的语句:G->adj[i] =(struct listnode *)malloc(sizeof(struct listnode));
G->adj[i]
的类型struct listnode
不是struct listnode*
。将G->adj[i]->vertex
更改为G->adj[i].vertex
。
G->adj[i].next
的类型是struct listnode*
,G->adj[i]
的类型是struct listnode
。将G->adj[i]->next=G->adj[i];
更改为G->adj[i].next = &( G->adj[i] );
。 G->adj[i]
是第 i
个元素本身,而不是指向第 i
个元素的指针。
注意 &( G->adj[i] )
与 G->adj + i
类似。 adj[i]
类似于*(adj + i)
,&(adj[i])
类似于adj + i
。
注意G->adj[i].next = &( G->adj[i] );
后的成员next
引用自己的struct listnode
。我不知道这是否是你想做的。
像这样调整你的代码:
struct Graph* initategraph(){
int i,source,dest;
struct listnode *temp;
struct Graph *G=(struct Graph*)malloc(sizeof(struct Graph));
if( G =0 NULL )
{
printf("Memory not available");
return NULL;
}
printf("Enter the vertex no. and edges n");
scanf("%d %d",&G->V,&G->E);
G->adj = malloc(G->V*sizeof(struct listnode));
for( i=0; i<G->V; i++ )
{
G->adj[i].vertex = i;
G->adj[i].next = &( G->adj[i] );
}
for( i=0; i<G->E; i++ ){
scanf("%d %d",&source,&dest);
temp= (struct listnode *)malloc(sizeof(struct listnode));
temp->vertex=dest;
temp->next = &( G->adj[source] );
G->adj[source].next = temp;
temp =(struct listnode *)malloc(sizeof(struct listnode));
temp->vertex=source;
temp->next = &( G->adj[dest] );
G->adj[dest].next = temp;
}
return G;
}
注意:我不知道算法是否在做你想要它做的事情。我只修复了编译错误。
- .cpp和.h文件中的模板专用化声明
- 未在作用域中声明unordered_map
- C++避免重复声明的语法是什么
- 如何确保C++函数在定义之前声明(如override关键字)
- 错误:未在此范围内声明'reverse'
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 为什么在定义函数之前先声明它
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- #ifdef和未声明的标识符
- 没有显式声明的int[]中的foreach
- 在基于范围的for循环中使用结构化绑定声明
- 在将变量声明为引用时,堆在释放后使用
- C++:无法访问声明的受保护成员
- C++图形类指针混淆
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 邻接列表的图形声明
- 声明在 Linux 上存在有关 X11 图形库的问题
- 面向Boost图形类型的前向结构声明