[Solved] Adjacency List for Graphs in C


Your program is causing memory leak. You should use an array of ADI*, not an array of ADI, to store what is returned from adjancencyList(). Using subscripting looks better than incrementing in this case. One more tips is that A = &( A[0] ); does virtually nothing.

Also note that they say you shouldn’t cast the result of malloc() in C.

Try this:

int main(void)
{
    int i, n, v;
    printf("Input number of vertices ");
    scanf( "%d", &n );
    ADI **A = malloc( n * sizeof( ADI* ) );

    for( i = 0; i < n; i++ )
    {
        printf( "Input vertex name:" );
        scanf( " %d ", &v );
        A[i] = adjancencyList( v );
    }

    for( i = 0; i < n; i++ )
    {
        printf(" %d ", A[i]->val  );
        printf( " \n " );
    }
     return 0;

}

2

solved Adjacency List for Graphs in C