-3

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");
    }

}
Jay Bhaskar
  • 157
  • 2
  • 11
  • 1
    [Thou shalt not cast the result of malloc](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Paul R Jan 26 '15 at 09:06
  • 4
    [You probably want](http://en.wikipedia.org/wiki/Zero-based_numbering) `i=0; i < maxNodes;` instead of `i=1; i <= maxNodes;` – Michael Jan 26 '15 at 09:06
  • 1
    Btw, line 63: `src` is a `Node* `, and `adjlist[]` holds `List*`, so `adjlist[source] = src` shouldn't even *compile*, much less produce output. – WhozCraig Jan 26 '15 at 09:09
  • should be `4---1--3-5` ? – BLUEPIXY Jan 26 '15 at 10:12

1 Answers1

0

fix like such as following

#include <stdio.h>
#include <stdlib.h>

#define maxNode 5+1 //0 : dummy

typedef struct Node {
    int vertexNum;
    struct Node *next;
} Node;

typedef struct List{
    int n;//unused
    Node *head;
} List;

List *adjlist[maxNode] = { 0 };

void printList(void);

void addNode(int source, int destination);

int main(void){
    int i;
    for (i = 1; 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();

    //deallocate
    return 0;
}

void addNode(int source, int destination){
    if(adjlist[source]->head == NULL){
        Node *src = (Node *)malloc(sizeof(Node));
        src->vertexNum = source;
        src->next = NULL;
        adjlist[source]->head = src;
    }

    Node *dest = (Node *)malloc(sizeof(Node));
    dest->vertexNum = destination;
    dest->next = NULL;


    Node *tmp = adjlist[source]->head;
    while (tmp->next != NULL)
        tmp = tmp->next;

    tmp->next = dest;
}

void printList(void){
    int i;
    for (i = 1; i <= maxNode; ++i){
        Node *p = adjlist[i]->head;
        printf ("Adjacency list for vetex %d\n", i);//adjlist[i]->n ?
        while (p){
            printf ("%d ", p->vertexNum);
            p = p->next;
        }
        printf ("\n");
    }
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70