title: How to delete nodes in a doubly linked list
body: <p>I'm having trouble deleting nodes in a doubly linked list, the program crash and i can't figure out the problem. Can you please help me? 
  This is the full code that creates new nodes, views them and deletes them.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  struct Test
  {
    int id;
  };
  
  typedef struct Node {
    struct Test structure;
    struct Node * next;
    struct Node *prev;
  
  }TNode;
  typedef TNode* Node;
  
  void NewNode(struct Test  p, Node *pp)
  {
    Node temp;
  
    temp = (Node)malloc(sizeof(struct Node));
  
    temp-&gt;structure = p;
    temp-&gt;next = *pp;
    temp-&gt;prev = NULL;
  
    if(*pp != NULL)
    {
      (*pp)-&gt;prev = temp;
    }
  
    *pp = temp;
  
  }
  
  void ReadStructure(struct Test * p)
  {
    printf("\nID:");
    scanf(" %d", &amp;p-&gt;id);
  }
  
  void ViewList(Node node)
  {
    Node temp;
    while(node != NULL)
    {
      temp = node-&gt;prev;
      if(node-&gt;prev == NULL)
        {
          printf("Prev = NULL\n");
        }
      else
      {
      printf("Prev: %d\n", temp-&gt;structure.id);
      }
      printf("Curr: %d\n", node-&gt;structure.id);
      node = node-&gt;next;
    }
  }
  
  void Delete(Node * head, Node del)
  {
  
         if(*head == NULL || del == NULL)
          {
            return;
          }
         if(*head == del)
         {
           *head = del-&gt;next;
         }
         if(del-&gt;next != NULL)
         {
           del-&gt;next-&gt;prev = del-&gt;prev;
         }
         if(del-&gt;prev != NULL)
         {
           del-&gt;prev-&gt;next = del-&gt;next;
         }
         free(del);
         return;
  
  }
  
  int Menu()
  {
    int c;
  
    printf("*** M E N U ***\n"
       "1 - New Node\n"
       "2 - View List\n"
     "3 - Delete\n"
     "0 - Exit\n"
    "\n&gt;&gt; ");
    scanf(" %d", &amp;c);
  
    return c;
  }
  
  int main()
  {
    int c;
    struct Test test;
    Node list = NULL;
    Node del = NULL;
    do {
      c = Menu();
  
      switch (c)
      {
        case 1: ReadStructure(&amp;test);
                NewNode(test, &amp;list); break;
        case 2: ViewList(list); break;
        case 3: printf("\nElement to Delete: ");
                scanf("%d", &amp;del-&gt;structure.id);
                Delete(&amp;list, del); break;
        default: c = 0;
      }
  
    } while (c != 0);
  
    return 0;
  }
  </code></pre>
  
  <p>I think the problem is related to the scanf() for the Node del, but i'm not sure. When i just pass <code>list</code> or <code>list-&gt;next</code> as second argument to the function Delete() it works. Is everything all right with the code?</p>