title: Node deletion in a doubly-linked list
body: <p>I am working on a function that would delete a node of a doubly-linked list. Here is my header file:</p>
  
  <pre><code>class LinkedList
  {
  private:
        struct Node
        {
           int data;
           Node *next;
           Node *previous;
        };
  
        int count;
        Node *head;
        Node *tail;
  
  public:
        LinkedList() {head = NULL; tail = NULL; count = 0;} //Constructor
  
        void insert(const int );
        bool remove(const int );
        bool contains(const int );
  
        size_t lenght() {return count;}
  };
  </code></pre>
  
  <p>My other functions work fine, but its my remove function that breaks on run-time.
  When i run my code i get a segmentation fault, and after 2 days of trying to figure out the flaw in my logic I am turning to the community for some help. I would be grateful for any feed-back at this point, thank you. Here is my remove function:</p>
  
  <pre><code>bool LinkedList::remove(const int item)
  {//if the list is empty returns false
  if(head == NULL) {return false;}
  
  Node *hptr = head;
  Node *tptr = tail;
  
  if((hptr -&gt; data) == item)
  {//if the node is at the head of the list
    hptr = hptr -&gt; next;
    delete head;
    hptr -&gt; previous = NULL;
    head = hptr;
    --count;
    return true;
  
  } else if((tptr -&gt; data) == item) {
   //if the node is at the tail of the list
    tptr = tptr -&gt; previous;
    delete tail;
    tail = tptr;
    tptr -&gt; next = NULL;
    --count;
    return true;
  
  } else {//if the node is in he middle of the list
    Node *ptr_head = head;   Node *ptr_headp = NULL;
    Node *ptr_tail = tail;   Node *ptr_tailp = NULL;
  
    while((ptr_head -&gt; data) != item || (ptr_tail -&gt; data) != item)
    {//pointers pass each other then data was not found
       if((ptr_tail -&gt; data) &lt; (ptr_head -&gt; data)) {return false;}
     //traversing the list from the head and tail simultaniously
       ptr_headp = ptr_head;
       ptr_head = ptr_head -&gt; next;
  
       ptr_tailp = ptr_tail;
       ptr_tail = ptr_tail -&gt; previous;
    }
  
    if((ptr_head == ptr_tail) &amp;&amp; ((ptr_tail -&gt; data) == (ptr_head -&gt; data)))
    {//the item is at the intersection of both head and tail pointers
       ptr_headp -&gt; next = ptr_tailp;
       ptr_tailp -&gt; previous = ptr_headp;
       delete ptr_head;
       delete ptr_tail;
       --count;
       return true;
    }
  
    if((ptr_head -&gt; data) == item)
    {//the item is before middle node
       ptr_headp -&gt; next = ptr_head -&gt; next;
      (ptr_head -&gt; next) -&gt; previous = ptr_headp;
       delete ptr_head;
       --count;
       return true;
    }
  
    if((ptr_tail -&gt; data) == item)
    {//the item is after the middle node
       ptr_tailp -&gt; previous = ptr_tail -&gt; previous;
      (ptr_tail -&gt; previous) -&gt; next = ptr_tailp;
       delete ptr_tail;
       --count;
       return true;
    }
  }
  
  return false;
  }
  </code></pre>