Adjacency list representation of below
1---2--3-4
2---1--5
3---4--1
4---1--3
5---2--4
After printing adjacency list , it is giving no output. can somebody explain what is the issue. The below code is simple implementation of a undirected graph in adjacency list.
#include<stdio.h>
#include <stdlib.h>
#define maxNode 5
typedef struct Node
{
int vertexNum;
struct Node *next;
} Node;
typedef struct List
{
int n;
struct List *head;
} List;
List *adjlist[maxNode] = { 0 };
void printList ();
void addNode (int source, int destination);
int
main ()
{
/* init our graph nodes */
int i = 1;
for (i; i <= maxNode; i++)
{
adjlist[i] = (List *) malloc (sizeof (List));
adjlist[i]->head = NULL;
}
addNode (1, 2);
addNode (1, 3);
addNode (1, 4);
addNode (2, 1);
addNode (2, 5);
addNode (3, 4);
addNode (3, 1);
addNode (4, 1);
addNode (4, 3);
addNode (5, 2);
addNode (5, 4);
printList ();
return 0;
}
void
addNode (int source, int destination)
{
Node *src = (Node *) malloc (sizeof (Node));
src->vertexNum = source;
Node *dest = (Node *) malloc (sizeof (Node));
dest->vertexNum = destination;
/* now source and destination is an edge */
/* then it should be listed in list */
adjlist[source] = src;
adjlist[source]->head = NULL;
src->next = NULL;
List *tmp = adjlist[source];
while (tmp->head != NULL)
tmp = tmp->head;
tmp->next = dest;
dest->next = NULL;
}
void
printList ()
{
int i = 1;
for (i = 1; i <= maxNode; ++i)
{
Node *p = adjlist[i]->head;
printf ("Adjacency list for vetex %d\n", i);
while (p)
{
printf ("%d ", p->vertexNum);
p = p->next;
}
printf ("\n");
}
}