title: Removing node from doubly linked list gives segmentation fault - C
body: <p>I have written a program that can can add char characters to the start of a doubly linked list. Now once I have this list, the aim of my program is to remove certain char character(s) from the list entirely. For example (using curly brackets only for representative purposes): if list consists of <strong>{ a, b, b, a, c }</strong>, then my program can remove all &quot;b&quot; from the list to make it <strong>{ a, a, c}</strong>. Moreover, if my list is <strong>{b, a, c, a}</strong> or <strong>{a, c, a, b}</strong> and if I want to remove &quot;b&quot; then the program works fine for both cases and gives me <strong>{a, c, a}</strong>.</p>
  <p>But there's a number of issues (for all cases assume I want to remove &quot;b&quot;):</p>
  <ol>
  <li>if my list is <strong>{b, a, b, a, c}</strong> (&quot;b&quot; at front and somewhere in middle), I get segmentation fault (I think it has to do with using cursor in the while loop, but I don't know why exactly and how to fix it)</li>
  <li>if my list is <strong>{a, b, b, a, c, b}</strong> (&quot;b&quot; in middle and at last) then output gives me weird symbols (I'm assuming its a memory fault, don't know why)</li>
  </ol>
  <p>Here is the code I am using:</p>
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  struct list
  {
     int data;
     struct list* next;
     struct list* prev;
  };
  
  struct list* head; // global variable - pointer to head node of list.
  struct list* last; // global variable - pointer to last node of list
  
  //Creates a new list and returns pointer to it.
  struct list* GetNewNode(char x)
  {
     struct list* newNode
         = malloc(sizeof(struct list));
     newNode-&gt;data = x;
     newNode-&gt;prev = NULL;
     newNode-&gt;next = NULL;
     return newNode;
  }
  
  //Inserts a list at head of doubly linked list
  void InsertAtHead(char x)
  {
     struct list* newNode = GetNewNode(x);
     if(head == NULL)
     {
         head = newNode;
         return;
     }
  
     head-&gt;prev = newNode;
     newNode-&gt;next = head;
     head = newNode;
     struct list* temp = head;
  
     while (temp-&gt;next != NULL) temp = temp-&gt;next;
     last = temp;
  }
  
  void remove_element (char character)
  {
     struct list * cursor, *previous, *store_el;
     //int boolean = 0;
  
     if (head == NULL) return;
     else
     {
         cursor = head;
         while(cursor != NULL)
         {
             if (cursor-&gt;data == character)
             {
                 if (cursor-&gt;prev == NULL)
                 {
                    // printf(&quot;deleting from front\n&quot;);
                     previous = head;
                     head = head-&gt;next;
                     head-&gt;prev = NULL;
                     //boolean = 1;
                     //free(previous);
                 }
                 if (cursor-&gt;next == NULL)
                 {
                     //printf(&quot;deleting from back\n&quot;);
                     previous = last;
                     last = last-&gt;prev;
                     last-&gt;next = NULL;
                     //boolean = 1;
                     //free(previous);
                 }
                 else
                 {
                    // printf(&quot;deleting from middle\n&quot;);
                     previous = cursor;
                     cursor = cursor-&gt;next;
                     cursor-&gt;prev = previous-&gt;prev;
                     store_el = previous-&gt;prev;
                     store_el-&gt;next = cursor;
                     cursor = head;
  
                 }
                 free(previous);
                 //printf(&quot;head data = %c\n&quot;, cursor-&gt;data);
             }
             cursor = cursor-&gt;next;
         }
     }
  }
  
  //Prints all the elements in linked list in forward traversal order
  void Print()
  {
     struct list* temp = head;
     printf(&quot;Forward: &quot;);
     while(temp != NULL)
     {
         printf(&quot;%c &quot;,temp-&gt;data);
         temp = temp-&gt;next;
     }
     printf(&quot;\n&quot;);
  }
  
  int main()
  {
  
     char character;
     /*Driver code to test the implementation*/
     head = NULL; // empty list. set head as NULL.
  
     // Calling an Insert and printing list before and after deletion of character
     InsertAtHead('c');
     InsertAtHead('a');
     InsertAtHead('b');
     InsertAtHead('b');
     InsertAtHead('a');
     Print();
     printf(&quot;After deletion:\n&quot;);
     remove_element ('b');
     Print();
  }
  </code></pre>