title: How to create an empty doubly-linked list in C?
body: <p>I wrote most of the code for my doubly linked list in C, but it seems that the reason I'm struggling to add values into the list is due to a mistake I made when creating an empty doubly linked list. Here's my attempt at it, what am I doing wrong here and how would I go about fixing it?</p>
  <pre><code>struct node
  {
      struct node *next;
      struct node *prev;
      char *value;
  };
  
  // The type for a list.
  typedef struct list
  {
      struct node head;
  } List;
  
  // The type for a list position.
  typedef struct list_pos
  {
      struct node *node;
  } ListPos;
  
  List *list_create(void)
  {
    List *lst = (List*)malloc(sizeof(List));
    if(lst == NULL)
    {
      printf(&quot;No more memory!\n&quot;);
      return 0;
    }
  
    return lst;
  }
  
  static struct node *make_node(const char *value)
  {
    struct node *result = malloc(sizeof(struct node));
    result-&gt;value = strdup(value);
    result -&gt; next = NULL;
    result -&gt; prev = NULL;
    return result;
  }
  </code></pre>
  <hr />
  <p>Example usage: main function: <code>List *lst = list_create();</code> from function <code>add_values()</code>:</p>
  <pre><code>static void add_values(List *lst) {
    char str[2] = &quot;A&quot;;
    ListPos pos = list_first(lst);
    for (char ch = 'A'; ch &lt;= 'Z'; ch++) {
      str[0] = ch;
      pos = list_insert(pos, str);
      pos = list_next(pos);
    }
  }
  </code></pre>
  <p>Implementation of functions:</p>
  <pre><code>int main(void){
  List *lst = list_create();
  ...
  }
  
  add_values static void add_values(List *lst) {     
  char str[2] = &quot;A&quot;;     ListPos pos = list_first(lst);     
  for (char ch = 'A'; ch &lt;= 'Z'; ch++) {         
  str[0] = ch;         
  pos = list_insert(pos, str);         
  pos = list_next(pos);     
   } 
  }
  </code></pre>