title: Sieve of Eratosthenes C++ using single linked list
body: <p>Hi I have a problem with Sieve of Eratosthenes in C++. I have to do this using single linked list. My program is running and showing first declaration of list but I don't know how to delete non prime numbers properly. My function just isn't working for me. How should I change my delete function?</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  
  using namespace std;
  
  struct List
  {
      int number;
      List* next;
  };
  List* head = new List;
  void l_add(int n)
  {
      List* temp = head;
      for (int i = 2; i &lt;= n; i++)
      {
          temp-&gt;next = new List();
          temp-&gt;number = i;
          temp = temp-&gt;next;
      }
  }
  void l_print()
  {
      List* temp = head;
      while (temp-&gt;next != 0)
      {
          cout &lt;&lt; temp-&gt;number &lt;&lt; &quot; &quot;;
          temp = temp-&gt;next;
      }
      cout &lt;&lt; endl;
  }
  void l_delete(int n)
  {
      List* temp = head;
      for (int i = 2; i &lt; sqrt(n); i++)
      {
          if (temp-&gt;number % i == 0)
          {
              head = temp-&gt;next;
              delete temp;
              temp = head;
          }
          while (temp-&gt;next != 0)
          {
              if (temp-&gt;next-&gt;number % i == 0)
              {
                  temp-&gt;next = temp-&gt;next-&gt;next;
                  delete temp-&gt;next;
              }
              temp = temp-&gt;next;
          }
      }
  }
  int main()
  {
      int n;
      cout &lt;&lt; &quot;Enter up to which number to find prime numbers using Sieve of Eratosthenes: &quot; &lt;&lt; endl;
      cin &gt;&gt; n;
      l_add(n);
      l_print();
      l_delete(n);
      l_print();
      return 0;
  }
  </code></pre>