我正在尝试构建邻接列表,但出现以下错误
graph.c:16:错误:下标值既不是数组也不是指针
我读到当一个非数组试图被索引时会发生这个错误。当我能够直接向它添加一个元素时(第 58 行:graph[i] = root),我可以知道将结构数组的成员分配给节点的错误是什么吗?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10
struct node{
int data;
struct node * link;
};
typedef struct node * NODE;
NODE graph[MAX];
void displayGraph (graph, n){
int i;
NODE cur;
for (i=0;i<n;i++){
cur = graph[i];
while(cur != NULL){
printf("%d ", cur->data);
}
}
}
NODE insert (NODE root, NODE temp){
NODE mine;
mine = root;
if (mine == NULL)
return root;
while(mine != NULL){
mine = mine->link;
}
mine->link = temp;
return root;
}
main ()
{
int n=0;
int i;
int val;
char * choice;
NODE temp, root;
printf("Enter the number of nodes\n");
scanf("&d", n);
for (i=0;i<n;i++){
root = NULL;
while(1){
printf("Is there an adjacent node?Y:N");
scanf("%s", choice);
if(!strcmp(choice, "N"));
break;
printf("Enter the adjacent node\n");
scanf("%d", val);
temp = malloc(sizeof (struct node));
temp->data = val;
temp->link = NULL;
root = insert(root, temp);
}
graph[i] = root;
}
displayGraph (graph, n);
}