title: Sorted Insert in doubly linked list
body: <p>I am making a doubly linked list using a struct <code>ListItem</code> which has <code>prev</code> and <code>next</code> pointers and a value of type T.</p>
  
  <p>Am I doing it right?</p>
  
  <p>When I run the main code, I can see only 1, 15, and 16 in the display.</p>
  
  <pre><code>template &lt;class T&gt;
  void List&lt;T&gt;::insertSorted(T item)
  {
      ListItem&lt;T&gt; *node=new ListItem&lt;T&gt;(item);
      if (head==NULL)
      {
           head=node;
      }          
      else if (head!=NULL)
      {
           T c,d;
           ListItem&lt;T&gt; *temp,*temp2;
           temp=head;
           c=temp-&gt;value;
           int count=0;
           while (temp-&gt;next!=NULL)
           {
                 if (temp-&gt;prev!=NULL)
                 temp2=temp-&gt;prev;
  
                 if (c&gt;item &amp;&amp; item&gt;temp2-&gt;value)
                 {
                      if (temp-&gt;prev!=NULL)
                      {
                           temp2-&gt;next=node;
                           node-&gt;prev=temp2;
                           node-&gt;next=temp;
                           temp-&gt;prev=node;     
                           count++;
                      }  
                      else if (temp-&gt;prev==NULL)
                      {
                           head=node;
                           head-&gt;next=temp;
                           count++;
                      }              
                 }
                 temp=temp-&gt;next;
                 c=temp-&gt;value;
           }
           if (temp-&gt;value&lt;item)   //comparison with last temp
           {
               temp-&gt;next=node;
               node-&gt;prev=temp;
           }
           else if (temp-&gt;value&gt;item)
           {
                temp2=temp-&gt;prev;
                temp2-&gt;next=node;
                node-&gt;prev=temp2;
                node-&gt;next=temp;
                temp-&gt;prev=node;
          }
      }        
  }
  int main()
  {
      List&lt;int&gt; Mylist;
      for (int i=16;i&gt;0;i--)
      {
          Mylist.insertSorted(i);  //insertion should be in ascending order according 
                                    //to my fnction
      }
      Mylist.printList();  //prints the whole linked list
      system("PAUSE");
      return 0;
  }
  </code></pre>