title: Pop method for doubly linked list implementation in C, do I need to use free
body: <p>As a beginner with C, I'm very unsure when I should be using free. I'm working on a pop method which removes an element from the end of a list. It should also return the value of the element removed.</p>
  
  <p>Here are my <code>List</code> and <code>ListNode</code> structs</p>
  
  <pre><code>typedef struct ListNode {
      struct ListNode *next;
      struct ListNode *prev;
      void *value;
  } ListNode;
  
  typedef struct List {
      int count;
      ListNode *first;
      ListNode *last;
  } List;
  </code></pre>
  
  <p>Heres the code for my <code>remove_from_back</code> function.</p>
  
  <pre><code>void *remove_from_back(List *list)
  {
      void *result = NULL;
  
      if (list-&gt;last == NULL) return result;
  
      result = list-&gt;last-&gt;value;
      if (list-&gt;last-&gt;value == list-&gt;first-&gt;value) {
          list-&gt;last = list-&gt;first = NULL;    
      } else {
          list-&gt;last = list-&gt;last-&gt;prev;
          list-&gt;last-&gt;next = NULL;        
      }   
      return result;
  }
  </code></pre>
  
  <p>add_to_back function which creates the memory for the nodes</p>
  
  <pre><code>void add_to_back(List *list, void *value)
  {  
      ListNode *node = calloc(1, sizeof(ListNode));
      node-&gt;value = value;
  
      if (list-&gt;first == NULL) {
          list-&gt;first = node;
          list-&gt;last = node;
      } else {
          list-&gt;last-&gt;next = node;
          node-&gt;prev = list-&gt;last;
          list-&gt;last = node;
      }
  
      list-&gt;count++;
  }
  </code></pre>