title: How to insert into a sorted singly linked list?
body: <p>I am making a sorted linked list where the user enters a number and the number is inserted into sorted position. I am currently having problems with the sort function.</p>
  
  <pre><code>  class Node
      {
      public:
          int data;
          class Node *next;
  
          Node(int info, Node *ptr = 0) 
          {
              data = info; 
              next = ptr;
          }
      };
      class Node *head = NULL;
  
  
      class stack
      {
      private:
          Node *temp;
      public: 
          stack()
          {
              temp = 0;
          }
          bool isEmpty()
          {
          return temp == NULL;
      }
  </code></pre>
  
  <p>I decided to go with an insert function(its called sort but really it just inserts a node into the linked list) which is why i have a current pointer and a previous pointer. I guess my question is am i on the right path? I am new to this so i just want a little bit of guidance to figure this out. Any tips will be appreciated.  </p>
  
  <pre><code> void sort(int data)
      {
          class Node *born = new Node(data);
          Node* current = head;
          Node* previous = NULL;
          //the list is empty case
          if (isEmpty())
              temp = born;
          else
          {
              while (current != NULL)
              {
                  if (current-&gt;data &gt;= temp-&gt;data)
                  {
                      born-&gt;next = current;
                      temp = born;
                      break;
                  }
                  else
                  {
                      previous = current;
                      current = temp-&gt;next;
                  }
              }
              if (current == head)
              {
                  born-&gt;next = head;
                  head = born;
              }
              else
              {
                  born-&gt;next = current;
                  previous-&gt;next = born;
              }
  
              /*else
              {
  
                  born-&gt;next = temp;
                  temp = born;
              }*/
          }
  
      }
  
      void print()
      {
          cout&lt;&lt;"stack from the top"&lt;&lt;endl;
              for(Node *top = temp; top != 0; top=top-&gt;next)
                  cout &lt;&lt; top-&gt;data &lt;&lt; " ";
              cout &lt;&lt; endl;
          /*while(temp != NULL)
          {
  
              cout&lt;&lt;temp-&gt;data&lt;&lt;" ";
              temp=temp-&gt;next;
          }
          */
      }
  
      int pop()
      {
          if (isEmpty())
                  return -999;        
  
              int intReturn = temp-&gt;data;
              Node *top;
  
              top = temp;
  
              temp = temp-&gt;next;
  
              delete top;
  
  
              return intReturn;
  
      }
  
      };
      int main(void)
      {
          int num=0;
  
          stack boss;
          while(num!=-1)
          {
              cout&lt;&lt;"Please Enter a Number"&lt;&lt;endl;
              cin &gt;&gt; num;
              if (num == -1)
                  boss.print();
              else
                  boss.sort(num);
          }
          cout&lt;&lt;endl&lt;&lt;endl;
  
  
          system("PAUSE");
          return 0;
  
      }
  </code></pre>