title: Traversing a single linked list in order
body: <p>I have been trying to think a way to traverse a single linked list.</p>
  
  <p>This is so far what I have done:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  typedef struct node {                                                               
        int data;               // will store information
        node *next;             // the reference to the next node
  };  
  
  
  int printList(node *traverse) {
      if (traverse-&gt;next == NULL) {
          return -1;
      }
      traverse=traverse-&gt;next;
      printList(traverse);
      cout &lt;&lt; traverse-&gt;data &lt;&lt; endl;
      return 0;
  }
  
  int main() {
      node *head = NULL;      
      for (int i = 0; i &lt; 10; i++) {
          node *newEntry = new node;
          newEntry-&gt;data = i;
          newEntry-&gt;next = head;
          head = newEntry;
      }
      printList(head);
      return 0;
  }
  </code></pre>
  
  <p>I cannot think a way to print the last digit(9) in <code>printList()</code> function. How would I be able to achieve this?
  My second question is, how can I traverse the same in a while loop rather than a recursive function.</p>
  
  <p>As some of you tried to answer before, I am not looking to traverse this from 9 to 0, This should traverse from 0 to 9, you can see the output from <a href="http://codepad.org/ynEdGc9S" rel="nofollow">http://codepad.org/ynEdGc9S</a></p>