title: Reversing a Doubly Linked List Issues
body: <p>I am trying to reverse a Doubly Linked List with no success.  After reversing, the list appears to be empty.</p>
  
  <p>Here is my implementation:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;malloc.h&gt;
  #include &lt;stdlib.h&gt;
  typedef struct Item Item;
  typedef struct DLL DLL;
  
  struct Item {
      int value;
      Item* next;
      Item* prev;
  };
  
  struct DLL {
      Item* head;
      Item* tail;
      int size;
      void(*add)(DLL*, int);
      void(*addToTail)(DLL*, int);
  };
  
  void add(DLL* list, int val) {
      Item* new_item = (Item*) malloc(sizeof(Item));
      if (new_item == NULL) {
          exit(-1);
      }
      new_item-&gt;value = val;
      new_item-&gt;next = list-&gt;head-&gt;next;
      list-&gt;head-&gt;next = new_item;
      new_item-&gt;prev = list-&gt;head;
      list-&gt;size++;
  }
  
  void addToTail(DLL* list, int val) {
      Item* new_item = (Item*) malloc(sizeof(Item));
      if (new_item == NULL) {
          exit(-1);
      }
      new_item-&gt;value = val;
      new_item-&gt;prev = list-&gt;tail-&gt;prev;
      list-&gt;tail-&gt;prev = new_item;
      new_item-&gt;next = list-&gt;tail;
      list-&gt;size++;
  }
  
  Item* find(DLL* list, int val) {
      Item* iter = list-&gt;head-&gt;next;
      while (iter != list-&gt;tail) {
          if (iter-&gt;value == val) {
              return iter;
          }
          iter = iter-&gt;next;
      }
      return NULL;
  }
  
  void reverse(DLL* list) {
      Item* current = list-&gt;head;
      Item* temp = NULL;
      while (current != NULL) {
          temp = current-&gt;next;
          current-&gt;next = current-&gt;prev;
          current-&gt;prev = temp;
          current = current-&gt;prev;
      }
  
      temp = list-&gt;head;
      list-&gt;head = list-&gt;tail;
      list-&gt;tail = temp;
  }
  
  void printList(DLL* list) {
      Item* iter = list-&gt;head-&gt;next;
      while (iter != list-&gt;tail) {
          printf("%d\n", iter-&gt;value);
          iter = iter-&gt;next;
      }
  }
  
  DLL* initDLL() {
      DLL* list = (DLL*) malloc(sizeof(DLL));
      if (list == NULL) {
          exit(-1);
      }
  
      // Creating head &amp; tail
      list-&gt;head = (Item*) malloc(sizeof(Item));
      list-&gt;tail = (Item*) malloc(sizeof(Item));
      if (list-&gt;head == NULL || list-&gt;tail == NULL) {
          free(list);
          exit(-1);
      }
  
      // Initializing head &amp; tail values just for testing
      list-&gt;head-&gt;value = 100;
      list-&gt;tail-&gt;value = 200;
  
      list-&gt;head-&gt;prev = NULL;
      list-&gt;head-&gt;next = list-&gt;tail;
      list-&gt;tail-&gt;prev = list-&gt;head;
      list-&gt;tail-&gt;next = NULL;
  
      list-&gt;size = 0;
      list-&gt;add = add;
      list-&gt;addToTail = addToTail;
  
      return list;
  }
  
  int main() {
      DLL* my_list = initDLL();
      my_list-&gt;add(my_list, 1);
      my_list-&gt;add(my_list, 2);
      my_list-&gt;add(my_list, 3);
  
      printList(my_list);
      // Outputs:
      // 3
      // 2
      // 1
  
      reverse(my_list);
  
      printList(my_list);
      // Prints nothing since list-&gt;head-&gt;next == list-&gt;tail
  }
  </code></pre>
  
  <p>I expected</p>
  
  <blockquote>
  <pre><code>3
  2
  1
  1
  2
  3
  </code></pre>
  </blockquote>
  
  <p>but get only</p>
  
  <blockquote>
  <pre><code>3
  2
  1
  </code></pre>
  </blockquote>
  
  <p>The first <code>printList()</code> works as expected, but the second produces no output.</p>
  
  <p>Looking into the problem I've found that after reversing the list, for some reason <code>list-&gt;head-&gt;next</code> is pointing to <code>list-&gt;tail</code>, even though there are 3 elements in the list.</p>
  
  <p>I've searched online for examples but stumbled upon implementations which don't use a <code>DLL</code> structure such as mine, but only <code>Node</code> structure.</p>