以下代码有什么问题

what is the issue with the below code

下面的邻接表表示

1---2--3-4
2---1--5
3---4--1
4---1--3
5---2--4

打印邻接表后,没有输出。有人可以解释是什么问题。下面的代码是邻接表中无向图的简单实现。

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

}

修复如下

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