title: Traverse through a known structure in C
body: <p>I am almost done with the rest of my assignment and now just need a print method to printout the structure that is obtained.</p>
  
  <p>I want to know how to write a loop to traverse through a structure like this:</p>
  
  <pre><code>[""][ ][ ]--&gt;  [""][ ][/]
       |              |              
     ["A"][/][/]     [""][ ][ ]--&gt;  [""][ ][/]     
                          |              |                 
                        ["B"][/][/]    ["C"][/][/]
  </code></pre>
  
  <p>That is the structure for:</p>
  
  <pre><code>(a (b c))
  </code></pre>
  
  <p>OR</p>
  
  <pre><code>[""][ ][ ]--&gt;  [""][ ][ ]--&gt; [""][ ][/]
       |              |             |  
     ["A"][/][/]    ["B"][/][/]   ["C"][/][/] 
  </code></pre>
  
  <p>This is for:</p>
  
  <blockquote>
    <p>(a b c)</p>
  </blockquote>
  
  <p>Its code is: </p>
  
  <pre><code>struct conscell {
  char symbol;
  struct conscell *first;
  struct conscell *rest;
  
  };
  </code></pre>
  
  <p>So,the first space that you see is the symbol character, the next is a conscell pointer "first" and the last is a conscell pointer "rest".</p>
  
  <p>Imagine, the structure is internally built (assignment done so far). </p>
  
  <p>So now, after traversing through the structure, I should print out the appropriate list, with brackets.
  For the above example, it would be </p>
  
  <blockquote>
    <p>(a (b c))</p>
  </blockquote>
  
  <p><strong>I am done with the method: did a tree traversal with current nodes data(symbol), left node(first) and right node(rest). Just need to find where to put the brackets to get the correct output. 
  Right now I get:</strong></p>
  
  <blockquote>
    <p>a b c</p>
  </blockquote>
  
  <p>The print method:</p>
  
  <pre><code>// List is a pointer to struct conscell 
  // myList will be the pointer referring to our first conscell structure  
  void printList(List myList){
  List current, pre;
  
  
  if (myList == NULL)
      return;
  
  current = myList;
  
  while (current != NULL) {
  
      if (current-&gt;first == NULL) {
          printf("%c", current-&gt;symbol);
          current = current-&gt;rest;
      }
      else {
          /* Find the inorder predecessor of current */
          pre = current-&gt;first;
          while (pre-&gt;rest != NULL &amp;&amp; pre-&gt;rest != current)
              pre = pre-&gt;rest;
  
          /* Make current as right child of its inorder predecessor */
          if (pre-&gt;rest == NULL) {
              pre-&gt;rest = current;
              current = current-&gt;first;
          }
              /* Revert the changes made in if part to restore the original 
                tree i.e., fix the right child of predecssor */
          else {
              pre-&gt;rest = NULL;
              printf("%c ", current-&gt;symbol);
              current = current-&gt;rest;
          } /* End of if condition pre-&gt;right == NULL */
      } /* End of if condition current-&gt;left == NULL*/
  
  } /* End of while */
  }
  </code></pre>
  
  <p>I'd be very grateful if someone can help me with this.</p>