title: Implement a doubly linked list in C
body: <p>I want to insert a node at the beginning of linked list, whenever <code>insertAtBeginning</code> method is called. My code builds well, but i don't get the desired output.</p>
  
  <p>I get the following output:</p>
  
  <pre><code>0------&gt;NULL
  </code></pre>
  
  <p>The desired output is:</p>
  
  <pre><code>9------&gt;8------&gt;7------&gt;6------&gt;5------&gt;4------&gt;3------&gt;2------&gt;1------&gt;0------&gt;NULL
  </code></pre>
  
  <p>Following is my code:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  struct dll{
      int data;
      struct dll* previous;
      struct dll* next;
  };
  
  
  struct dll* insertAtBeginning(int a, struct dll* head){
  
      if(head == NULL){
          head-&gt;data = a;
          head-&gt;previous = NULL;
          head-&gt;next = NULL;
          return head;
      }
      else{
          struct dll *first;
          first = (struct dll*) malloc( sizeof(struct dll));
          first-&gt;data = a;
          first-&gt;next = head;
          head-&gt;previous = first;
          first-&gt;previous = NULL;
          head = first;
          free(first);
          return head;
      }
  }
  
  
  void display_from_first(struct dll* head){
      struct dll *temp;
      temp = head;
  
      printf("\nThe linked list contains: ");
      while(temp != NULL) {
          printf("%d------&gt;",temp-&gt;data);
          temp = temp-&gt;next;
      }
      printf("NULL\n");
      free(temp);
      }
  
  
  int main(){
      int i = 0;
      struct dll *head1, *tail1;
      head1 = (struct dll*) malloc( sizeof(struct dll));
      head1-&gt;next = NULL;
      head1-&gt;previous = NULL;
  
      for(i=0; i&lt;10; i++){
          insertAtBeginning(i, head1);
      }
  
      display_from_first(head1);
  
      return 0;
  }
  </code></pre>