title: Recursively Delete Even Items from Doubly Linked List C++
body: <p>Here is the same question I am asking:
  <a href="https://stackoverflow.com/questions/37721465/deleting-even-nodes-from-a-doubly-link-list-c">deleting even nodes from a doubly link list c++</a></p>
  
  <p>The difference is, I want to understand what is wrong with my code. I don't want to have to just accept a completely different approach without understand why my code doesn't work. Here are two versions of my code I would like to know what is wrong with both. Both of them give me segmentation fault.</p>
  
  <pre><code>int removeEven(node *&amp;head)
  {
  if(!head)               //Base case: end of list reached
      return 0;
  int count = removeEven(head-&gt;next);     //Recurse to end of list
  if(head-&gt;data % 2 != 0)
      return count;
  else{
      ++count;
      if(head-&gt;next){
          head-&gt;next-&gt;previous = head-&gt;previous;
      }
      if(head-&gt;previous){
          head-&gt;previous-&gt;next = head-&gt;next;
      } if(!(head-&gt;previous)){
          node* temp = head;
          head = head-&gt;next;
          delete temp;
      }
      else
          delete head;
  }
  return count;
  }
  </code></pre>
  
  <p>This second one takes count = 0 as a default arg.</p>
  
  <pre><code>int removeEven(node *&amp;head, int count)
  if(head &amp;&amp; head-&gt;data % 2 != 0) //not null, not even
  {
      removeEven(head-&gt;next, count);
  }
  else if(head != NULL){      //not null, yes even
     ++count;
      if(head-&gt;next)
          head-&gt;next-&gt;previous = head-&gt;previous;
      if(head-&gt;previous)
          head-&gt;previous-&gt;next = head-&gt;next;
      node* temp = head;
      head = head-&gt;next;
      delete temp;
     removeEven(head, count);
  }
      return count;       //base case: null
  }
  </code></pre>