简体   繁体   中英

Graph adjacency list representation

I am trying to write a basic graph program in c. However, an error message displays saying that the The arrayOfVertices is undeclared . I am using adjacency list representation and the code is


#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;
}

My approach is to create an array (of struct graphnode type) containing as many nodes as entered by the user. The structure contains two fields, one containing the number and the other containing pointers.


--- graph.c 2012-07-18 21:33:52.405175347 +0200
+++ graph2.c    2012-07-18 21:40:01.150704287 +0200
@@ -1,3 +1,4 @@
+
 #include <stdio.h>
 #include <stdlib.h>

@@ -11,9 +12,10 @@
 {
 // creating a new linked list which is to be added
 struct graphnode *tmp;
+tmp = malloc (sizeof *tmp);
 tmp->name=endNode;
 tmp->next=G[startNode];
-startNode=tmp;
+G[startNode] = tmp;
 }

 void printGraph(struct graphnode *G[], int numofnodes)
@@ -22,13 +24,11 @@
 for(i=0;i<numofnodes;i++)
 {
     struct graphnode *tmp;
-    tmp=G[i];
     printf("%d",i);

-    while(tmp!=NULL)
+    for(tmp = G[i]; tmp; tmp = tmp->next)
     {
         printf("%d",tmp->name);
-        tmp=tmp->next;
     }
 }
 }
@@ -36,21 +36,24 @@
 int main(void)
 {
 int numofnodes;
+int i;
+struct graphnode **arrayOfVertices;
+
 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
+arrayOfVertices = malloc ( numofnodes * sizeof *arrayOfVertices);

 for(i=0;i<numofnodes;i++)
 {
+    arrayOfVertices[i] = malloc (sizeof *arrayOfVertices[i] );
     arrayOfVertices[i]->name=i;
     arrayOfVertices[i]->next=NULL;
 }

 addNode(arrayOfVertices,0,1);
-addNode(aarrayOfVertices,0,2);
+addNode(arrayOfVertices,0,2);
 printGraph(arrayOfVertices,numofnodes);

 return 0;

Note: I've only spotted the obvious errors. I replaced the variable sized array by a dynamically allocated one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM