I am trying to build an adjacency list but get the following error
graph.c:16: error: subscripted value is neither array nor pointer
I read that this error occurs when a non array is trying to be indexed. When I am able to add an element to it directly (line 58: graph[i] = root), can I please know what is the error in assigning a member of the structure array to a NODE?
#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);
}
scanf("&d", n);should bescanf("%d", &n);And also line 54 (?) should bescanf("%d", &val);strcmp()will always result inbreak;. See my answer below.