I'm having problems printing vertices when using Adjacency Lists. I want to get only the starting vertices, without the neighbours, but I get their addresses instead. So, I'm having an issue with pointers. Right now I don't get why it's not printing the correct output. My code is:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct ADI{
int val;
struct ADI *urm;
}ADI;
ADI *adjancencyList( int vertex )
{
int neigh,i;
ADI *head, *elem, *vec;
head = ( ADI* ) malloc( sizeof( ADI ) );
head->val = vertex;
elem = head;
printf( "Input number of neighbours:" );
scanf( "%d", &neigh );
for( i = 0; i < neigh ; i++)
{ printf( "Neighbour:" );
vec = ( ADI* ) malloc( sizeof( ADI ) );
elem->urm = vec;
scanf( "%d", &elem->val );
}
return head;
}
int main()
{ int i, n, v;
printf("Input number of vertices ");
scanf( "%d", &n );
ADI *A = ( ADI* ) malloc( n * sizeof( ADI ) );
for( i = 0; i < n; i++ )
{
printf( "Input vertex name:" );
scanf( " %d ", &v );
A = adjancencyList( v );
A++;
}
A = &( A[0] );
for( i = 0; i < n; i++ )
{
printf(" %d ", A->val );
printf( " \n " );
A++;
}
return 0;
}