title: C - How to Iterate through sub-nodes in a linked list?
body: <p>I have the following program which I'm developing:</p>
  
  <pre><code>...
  
  typedef struct node_tag{
      int id;
      char pLabel[10];
      struct node_tag *pNext;
      struct node_tag *pInner;
      }NODE;
  
  ...
  
  int main(int argc, char** argv)
      {
  
      NODE *list = create_node("start");
      add_node(&amp;list, "MUSIC");
      add_node(&amp;list, "VIDEOS");
      add_node(&amp;list, "PICTURES");
      create_sub_node( &amp;list, "2015" ); // sub node of PICTURES
      create_sub_node( &amp;list, "Trip" ); // sub node of 2015
      print_nodes(list);
  
      return (EXIT_SUCCESS);
      }
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>|
  |-&gt; [ID:4] PICTURES
  |   |-&gt; [ID:40] 2015
  |   |   |-&gt; [ID:400] Trip
  |
  |-&gt; [ID:3] VIDEOS
  |
  |-&gt; [ID:2] MUSIC
  |
  |-&gt; [ID:1] start
  |
  </code></pre>
  
  <p>Everything works as I want so far, as can be seen on the output. However I want to implement different methodology of creating sub nodes. What I've got now is very limited as it can only create the depth of 2 sub nodes but I want to have unlimited depth:</p>
  
  <pre><code>void create_sub_node( NODE **handle, char* label )
      {
      NODE *new = malloc( sizeof(NODE) );
  
      new-&gt;pNext = NULL;
      new-&gt;pInner = NULL;
      strncpy( new-&gt;pLabel , label , strlen(label) +1 );   
  
      if( (*handle)-&gt;pInner == NULL )
          {
          new-&gt;id = (*handle)-&gt;id * 10;
          (*handle)-&gt;pInner = new;        
          }
      else if( (*handle)-&gt;pInner-&gt;pInner == NULL ) 
          {
          new-&gt;id = (*handle)-&gt;pInner-&gt;id * 10;
          (*handle)-&gt;pInner-&gt;pInner = new;
          }
      }
  </code></pre>
  
  <p>I tried implementing while loops that could iterate through inner nodes until they found NULL one and then creating new node. The problem I'm having is that as I iterate through the nodes, the pointer addresses change and I'm left with one big mess that's no longer working. </p>
  
  <p>I tried making a copy of all the addresses of inner nodes, but then I wasn't able to re-assign them again.</p>
  
  <p>The thing is that I have to add the sub node to the list so I'll be changing various pointer addresses BUT to do that It looks like I need a copy of the list where I could play with it so that the original addresses don't change. </p>
  
  <p><strong>How do I iterate through the sub nodes and create new ones so that I don't have to hard-code bunch of IF statements ?</strong></p>