我正在尝试用 c 编写一个基本的图形程序。但是,会显示一条错误消息,指出The arrayOfVertices is undeclared
. 我正在使用邻接表表示,代码是
#include <stdio.h>
#include <stdlib.h>
struct graphnode
{
int name;
struct graphnode *next;
};
void addNode(struct graphnode *G[],int startNode, int endNode)
{
// creating a new linked list which is to be added
struct graphnode *tmp;
tmp->name=endNode;
tmp->next=G[startNode];
startNode=tmp;
}
void printGraph(struct graphnode *G[], int numofnodes)
{
int i;
for(i=0;i<numofnodes;i++)
{
struct graphnode *tmp;
tmp=G[i];
printf("%d",i);
while(tmp!=NULL)
{
printf("%d",tmp->name);
tmp=tmp->next;
}
}
}
int main(void)
{
int numofnodes;
printf("Enter the number of nodes: ");
scanf("%d",&numofnodes);
// Note that we have created depending upon the size inputted by the user
struct graphnode *arrayOfVertices[numofnodes];
int i; // for iteration
for(i=0;i<numofnodes;i++)
{
arrayOfVertices[i]->name=i;
arrayOfVertices[i]->next=NULL;
}
addNode(arrayOfVertices,0,1);
addNode(aarrayOfVertices,0,2);
printGraph(arrayOfVertices,numofnodes);
return 0;
}
我的方法是创建一个数组(struct graphnode 类型),其中包含用户输入的尽可能多的节点。该结构包含两个字段,一个包含数字,另一个包含指针。