title: Trouble Adding an item to the back of a doubly linked list
body: <p>I’m new to doubly linked lists.  I’m working on a few pieces of code: one function to add an item to the front, another to add an item to the back, and forward and reverse display methods that output the linked list from front to back and back to front, respectively.<br>
  I came across an error in my output I’m trying to understand.  It seems my addFront and display functions are working but my addBack is probably where my error is.  I posted what my output is and what it showed be at the bottom of this post.
  This is my code thus far.</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  class doubleLinkedList
  {
  private:
      class node
      {
      public:
          int data;
          node* next;
          node* prev;
          node(int x)
          {
              data = x;
              next = NULL;
              prev = NULL;
          }
      };
  public:
      node* head;
      node* tail;
      int count;
  
  
  
      doubleLinkedList();        //default constructor
      ~doubleLinkedList();      //destructor
  
      void displayForward();    //display items from front to back
      void displayBackward();   //display items from back to front
  
      void addFront(int);      //add item to front of linked list
      void addBack(int);       //add item to back of linked list
  
      int removeFront();       //remove item from front of linked list
      int removeBack();        //remove item from back of linked list
  };
  
  //constructor
  doubleLinkedList::doubleLinkedList(){
      head = tail = NULL;
      count = 0;
  }
  
  
  //destructor
  doubleLinkedList::~doubleLinkedList(){
      node* current = head;
  
      while(current != NULL)
      {
          node* previous = current;
          current = current-&gt;next;
          delete previous;
      }
  
      head = tail = NULL;
      count = 0;
  }
  
  
  //display items in linked list from front to back
  void doubleLinkedList::displayForward(){
      node* pCurrent = head;
      while (pCurrent != NULL)
      {
          cout &lt;&lt; pCurrent-&gt;data &lt;&lt; " ";
          pCurrent = pCurrent-&gt;next;
      }
      cout &lt;&lt; count;
  }
  
  
  //display items in linked list from back to front
  void doubleLinkedList::displayBackward(){
      node* pCurrent = tail;
      while (pCurrent != NULL)
      {
          cout &lt;&lt;pCurrent-&gt;data &lt;&lt; " ";
          pCurrent = pCurrent-&gt;prev;
      }
      cout &lt;&lt; count;
  }
  
  
  //add item to front of linked list
  void doubleLinkedList::addFront(int x){
  
      node* n = new node(x);
      n-&gt;next = head;
      n-&gt;prev = NULL;
  
      if (head != NULL)
          head-&gt;prev = n;
          head = n;
          count++;
  
      if (tail == NULL)
          tail = n;
  }
  
  void doubleLinkedList::addBack(int x){
      node* n = new node(x);
  
      n-&gt;next = NULL;
      n-&gt;prev = tail;
      tail = n;
  
      count++;
  }
  </code></pre>
  
  <p>////////////////////My Test Code:///////////////////</p>
  
  <pre><code>int main()
  {
      doubleLinkedList list;
  
      list.addBack(40);
      list.addBack(50);
      list.addBack(60);
      list.addFront(30);
      list.addFront(20);
      list.addBack(70);
      list.addBack(80);
      list.addFront(10);
  
      list.displayForward();  //10 20 30 8   (the 8 value is the count/size i'm keeping track of)
          cout &lt;&lt; endl;
      list.displayBackward(); //80 70 60 50 40 8
          cout &lt;&lt; endl;
  
          system("pause");
          return 0;
          }
  </code></pre>
  
  <p>My output should display 10 20 30 40 50 60 70 80
  and                      80 70 60 50 40 30 20 10<br>
  but instead my displayForward displays the items I added to the Front and my displayBackward displays the items I added to the back.</p>