title: Creating a 2 dimensional linked list in C
body: <p>For an assignment we have to implement a two dimensional linked list in C.
  My idea was to create several linked lists on their own and link their heads together.</p>
  
  <p>This is the code I came up with, which does not work unfortunately.
  When I try to traverse the result starting from the tree node, there is nothing printed.</p>
  
  <p>Can anyone point me in the right direction on whats wrong with that code?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct cell{
      int value;
      struct cell *next;
  };
  
  struct head {
     int value;
     struct head *next;
     struct cell *cells;
  };
  
  struct head *current_head, *new_head;
  struct cell *current_cell, *new_cell;
  
  int i;
  int j;
  void grid_init(int num_rows,int num_columns){
      for (i=0; i&lt;num_rows-1;i++) {
          struct cell * current_cell = (struct cell* ) malloc (sizeof(struct cell));
          current_head-&gt;cells = current_cell;
          for (j=0; j&lt;num_columns-1; j++) {
              struct cell* new_cell = (struct cell* ) malloc (sizeof(struct cell));
              current_cell-&gt;next = new_cell;
              current_cell-&gt;value=j;
              current_cell = new_cell;
          }
          struct head* new_head = (struct head *) malloc (sizeof(struct head));
          current_head-&gt;next = new_head;
          current_head-&gt;value=i;
          current_head = new_head;
      }
  };
  
  
  
  void print_grid(){
      struct head* temp = current_head;
      struct cell* tempcell = current_cell;
      while(temp-&gt;next!=NULL){
          tempcell = temp-&gt;cells;
          while(tempcell-&gt;next!=NULL){
              tempcell = temp-&gt;cells;
              printf("%i", tempcell-&gt;value);
              tempcell = tempcell-&gt;next;
          }
          temp = temp-&gt;next;
      }
  };
  
  int main(){
      current_head = (struct head *) malloc (sizeof(struct head));
      int a=5;
      int b=5;
      grid_init(a,b);
      print_grid();
  };
  </code></pre>