#include <stdio.h>

typedef struct edge {
  int linked_node;
  int cost;
  struct edge * next;
} t_edge;

typedef t_edge * t_edge_list;

typedef struct {
  int number_of_nodes;
  t_edge_list * links;
} t_graph;

void init_graph(t_graph * graph, int nodes)
{
  int i;   
  graph->number_of_nodes = nodes;
  graph->links = (t_edge_list *)malloc(nodes*sizeof(t_edge_list));
  for (i = 0;i < nodes;i++)
    graph->links[i] = NULL;
}

void add_edge(t_graph * graph, int from, int to, int cost)
{
     t_edge * new_link;
     new_link = (t_edge *)malloc(sizeof(t_edge));
     new_link->linked_node = to;
     new_link->cost = cost;
     
     new_link->next = graph->links[from];
     graph->links[from] = new_link;
}

void print_graph(t_graph * graph)
{
   int i;
   t_edge * aux;
   for (i = 0; i < graph->number_of_nodes;i++) {
      printf("Node %d:", i);
      aux = graph->links[i];
      while (aux != NULL) {
        printf(" (%d, %d),", aux->linked_node, aux->cost);
        aux = aux->next;
      }
      printf("\n");
   }
}

int main(int argc, char * argv[])
{
   t_graph my_graph;
   init_graph(&my_graph, 11);
   add_edge(&my_graph, 0, 2, 4);
   add_edge(&my_graph, 0, 3, 3);
   add_edge(&my_graph, 0, 4, 20);
   add_edge(&my_graph, 2, 6, 9);
   add_edge(&my_graph, 2, 5, 2);
   print_graph(&my_graph);
   getchar();
}
