title: Inserting a node in the given position c++
body: <p>I have a problem about inserting an element in a given position in a single linked list. Actually the problem is only at inserting into middle part,
  Here is my insert function;</p>
  
  <pre><code>void SimpleList::insert (const ItemType &amp; item, int position){        
      Node* prev = NULL;
      Node* curr = NULL;
      Node* newNode = new Node();
      newNode-&gt;data = item;
  
      int tempPos = 0;
  
      curr = head;
  
      if(head != NULL){
          while(curr-&gt;next != NULL &amp;&amp; tempPos != position)
          {
              prev = curr;
              curr = curr-&gt;next;
              tempPos++;
          }
          if(position==1)
          {
              newNode-&gt;next=head;
              head=newNode;
              size++;
  
          }
          else if(curr-&gt;next == NULL)
          {
  
              curr-&gt;next = newNode;
              last = newNode;
              newNode-&gt;next= NULL;
              size++;
          }
          else
          {
              prev-&gt;next = newNode;
              newNode-&gt;next = curr;
              size++;
          }
      }
      else  {
          last = head = newNode;
          last-&gt;next = NULL;
          size++;
      }
  }
  </code></pre>
  
  <p>and the output of my test code is;</p>
  
  <pre>
  after insert into empty list: should be 34
  34  
  after insert at beginning: should be  45, 34
  45  34  
  after inserts in middle: should be 45, 72, 56, 34
  45  34  56  72  
  after insert at end: should be 45, 72, 56, 34, 67
  45  34  56  72  67  
  </pre>
  
  <p>How can I fix that problem?
  Thank you all for your helps.</p>