title: C++ singly linked list using a classes
body: <p>I want to create singly linked list (using classes), where in each list there will be: pointer to text,int number, pointer to next list.</p>
  
  <p>I need to implement 3 functions:
  inserts(which inserts a list into singly linked list and sorts elements with strcmp according to text which is pointed by pointer)
  removes(int num) which removes first list in which number occurs.
  print() which prints the whole singly linked list.</p>
  
  <p>I have problem with removes function which gives error in runtime and I have a conjecture where is the problem <code>if ( tmp-&gt;next == NULL &amp;&amp; tmp-&gt;number==num ) {
      delete tmp;
      first = NULL;
      }</code>, but I have no idea why is it so .</p>
  
  <p>Also I am not sure how should I implement sorting into insert function, so if you have any ideas and if you could explain me where in my removes function the error is I would really appreciate it.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>    #include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;cstring&gt;
  
  using namespace std;
  
  class list
  {
  private:
      int number;
  
      char* word;
      list* next;
  public:
      void inserts(int num, char* text);
      void removes(int num);
      void print();
  };
  list* first;
  
  void list::print() {
      cout &lt;&lt;"This is our list:"&lt;&lt;endl;
  
      // Temp pointer
      list *tmp = first;
  
      // No nodes
      if ( tmp == NULL ) {
      cout &lt;&lt; "EMPTY list" &lt;&lt; endl;
      return;
      }
  
      // One node in the list
      if ( tmp-&gt;next == NULL ) {
      cout &lt;&lt;"NUMBER:\t"&lt;&lt; tmp-&gt;number;
      cout &lt;&lt;"\tWORD:\t"&lt;&lt; tmp-&gt;word &lt;&lt; endl;
      cout &lt;&lt;"--------------------------------"&lt;&lt;endl;
  
      }
      else {
      // Parse and print the list
      while ( tmp != NULL ){
           cout &lt;&lt;"NUMBER:\t"&lt;&lt; tmp-&gt;number;
           cout &lt;&lt;"\tWORD:\t"&lt;&lt; tmp-&gt;word &lt;&lt; endl;
           cout &lt;&lt;"--------------------------------"&lt;&lt;endl;
  
          tmp = tmp-&gt;next;
      }
  }
  }
  
  void list::inserts(int num, char* word){
      // Create a new list
      list* newlist = new list; 
      newlist-&gt;number=num;
  
      newlist-&gt;word=word;
      newlist-&gt;next=NULL;
  
      // Create a temp pointer
      list *tmp = first;
  
      if ( tmp != NULL ) {
      // Nodes already present in the list
      // Parse to end of list
      while ( tmp-&gt;next != NULL ) {
          tmp = tmp-&gt;next;
      }
  
      // Point the last node to the new node
      tmp-&gt;next=newlist;
      }
      else {
      // First node in the list
      first = newlist;
      }
  }
  
  void list::removes(int num){
  int k = 0;
      list* tmp=first;
      if(tmp==NULL)
          return;
         //Last node of the list
  
     if ( tmp-&gt;next == NULL &amp;&amp; tmp-&gt;number==num ) {
      delete tmp;
      first = NULL;
      }
      else {
      //Parse thru the nodes
      list* prev;
      prev = new list;
      while ( tmp != NULL )
      {
          if ( tmp-&gt;number == num &amp;&amp; k == 0)
              first = first-&gt;next;
  if ( tmp-&gt;number == num)
  break;
          prev = tmp;
  
          tmp = tmp-&gt;next;
  k++;
      } 
  
      //Adjust the pointers
      prev-&gt;next=(tmp-&gt;next);
      //Delete the current node
  delete tmp;
  delete prev;
  
  }
  }
  
  
  int main ()
  {
      first-&gt;print();
      first-&gt;inserts(1200,"endian");
      first-&gt;print();
     /* first-&gt;inserts(10,"endianness");
      first-&gt;inserts(1200,"PEEK");
      first-&gt;inserts(1200,"POKE");
      first-&gt;inserts(1200,".MIL");
      first-&gt;print();*/
  first-&gt;removes(100);
  first-&gt;print();
  getchar();
  }
  </code></pre>