创建使用adjaceny LIST表示创建图形的函数时,我收到以下错误:
错误:' - >'的无效类型参数(有' int')
我已将此错误标记为(在评论中)//错误。以下是代码:
typedef struct GRAPH
{
int V;
int E;
int *adj; //head pointer to the Linked List
} graph;
typedef struct NODE //Node of the Linked List
{
int vertexNumber;
struct NODE *next;
} node;
graph *adjListOfGraph()
{
int i,x,y;
node *temp;
graph *g;
g = (node *)malloc(sizeof(graph));
if(!g)
{
printf("Memory Error in creating the graph");
return;
}
scanf("Number of Vertex: %d, Number of Edges: %d", &g->V,&g->E);
g->adj=(node *)malloc(g->V *sizeof(node));
for(i=0;i<g->V;i++)
{
g->adj[i] = (node *)malloc(sizeof(node));
g->adj[i]->vertexNumber = i; //error
g->adj[i]->next = g->adj[i]; //error
}
for(i=0;i<g->E;i++)
{
scanf("Reading edges: %d %d", &g->V,&g->E);
temp = (node *)malloc(sizeof(node));
temp->vertexNumber = y;
temp->next = g->adj[x];
g->adj[x]->next = temp; //error
temp = (node *)malloc(sizeof(node));
temp->vertexNumber =y;
temp->next = g->adj[y];
g->adj[y]->next = temp; //error
}
return g;
}
请查看注释为错误的行。我搜索了很多,并尝试更换 - &gt;通过。但没用。
答案 0 :(得分:2)
编译器抱怨,因为您尝试取消引用adj[j]
int
。看看你的代码,似乎你可能想要
node **adj;
而不是
int *adj;
其他问题:
malloc
(and family)。return;
应为return NULL;
,因为函数adjListOfGraph
旨在返回graph*
,而不是任何内容。请注意,对于您的scanf
scanf("Number of Vertex: %d, Number of Edges: %d", &g->V,&g->E);
要接受输入,您必须输入"Number of Vertex: <some number>, Number of Edges: <some number>"
而不只是"<some number> <some number>"
。