title: Maddening Linked List problem
body: <p>This has been plaguing me for weeks. It's something really simple, I know it. Every time I print a singly linked list, it prints an address at the end of the list.</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  struct node
  {
    int info;
    node *link;
  };
  
  node *before(node *head);
  node *after(node *head);
  void middle(node *head, node *ptr);
  void reversep(node *head, node *ptr);
  
  node *head, *ptr, *newnode;
  
  int main()
  {
  head = NULL;
  ptr = NULL;
  newnode = new node;
  head = newnode;
  
  for(int c1=1;c1&lt;11;c1++)
  {
    newnode-&gt;info = c1;
    ptr = newnode;
    newnode = new node;
    ptr-&gt;link = newnode;
    ptr = ptr-&gt;link;
  }
  
  ptr-&gt;link=NULL;
  
  head = before(head);
  head = after(head);
  middle(head, ptr);
  //reversep(head, ptr);
  
  ptr = head;
  cout&lt;&lt;ptr-&gt;info&lt;&lt;endl;
  while(ptr-&gt;link!=NULL)
  { 
  ptr=ptr-&gt;link;
    cout&lt;&lt;ptr-&gt;info&lt;&lt;endl;
  }
  
  system("Pause");
  return 0;  
  }
  
  node *before(node *head)
  {
    node *befnode;
    befnode = new node;
  
    cout&lt;&lt;"What should go before the list?"&lt;&lt;endl;
    cin&gt;&gt;befnode-&gt;info;
  
    befnode-&gt;link = head;
    head = befnode;
  
    return head;
  }
  
  node *after(node *head)
  {
    node *afnode, *ptr2;
    afnode = new node;
  
    ptr2 = head;
  
    cout&lt;&lt;"What should go after the list?"&lt;&lt;endl;
    cin&gt;&gt;afnode-&gt;info;
  
    ptr2 = afnode;
    afnode-&gt;link=NULL;
  
    ptr2 = head;
    return ptr2;
  }
  
  void middle(node *head, node *ptr)
  {
    int c1 = 0, c2 = 0;
    node *temp, *midnode;
  
    ptr = head;
    while(ptr-&gt;link-&gt;link!=NULL)
    {
      ptr=ptr-&gt;link;
      c1++;
    }
  
    c1/=2;  
    c1-=1;
  
    ptr = head;
  
    while(c2&lt;c1)
    {
      ptr=ptr-&gt;link;
      c2++;
    }
  
    midnode = new node;
  
    cout&lt;&lt;"What should go in the middle of the list?"&lt;&lt;endl;
    cin&gt;&gt;midnode-&gt;info;
    cout&lt;&lt;endl;
  
    temp=ptr-&gt;link;
    ptr-&gt;link=midnode;
    midnode-&gt;link=temp;
  }
  
  void reversep(node *head, node *ptr)
  {
    node *last, *ptr2;   
  
    ptr=head;
    ptr2=head;
  
    while(ptr-&gt;link!=NULL)
      ptr = ptr-&gt;link;
  
    last = ptr;
  
    cout&lt;&lt;last-&gt;info;
  
    while(ptr!=head)
    {
      while(ptr2-&gt;link!=ptr)
        ptr2=ptr2-&gt;link;
  
      ptr = ptr2;
      cout&lt;&lt;ptr-&gt;info;
    }
  }
  </code></pre>
  
  <p>I'll admit that this is class work, but even the professor can't figure it out, and says that its probably something insignificant that we're overlooking, but I can't put my mind to rest until I find out what it is.</p>