title: doubly linked list java remove from back
body: <p>Every time I run my Doubly Linked List, all of the methods work except when I remove from the back of the list. I had a List that was 4, 3, 9. I removed from front (which took away the 4). Then, I called this method that should've only removed the 9. Instead, when I call for the DL list, it returns null (which removes the 3 as well). PLEASE help.</p>
  
  <pre><code>    public Object removeLast() {
      Object result = null;
  
      if (!isEmpty()) {
          result = lastNode.getData();
          lastNode = lastNode.getPreviousNode();
  
          if (lastNode == null)
              firstNode = null;
          else
              lastNode.setNextNode(null);
      }
      return result;
  }
  </code></pre>
  
  <p>Here is the rest of the code (which extends an interface; the generated code isn't necessary for the problem, so we haven't filled it in.. i.e. stuff after Node doesn't matter) THE CORRECT CODE:</p>
  
  <pre><code>    public class DList implements DoubleEndedListInterface {
  
      public Node lastNode;
      public Node firstNode; // reference to first node
      public int numberOfEntries;
  
      @Override
      public String toString() {
          return "" + firstNode;
  
      }
  
      public Node getNodeAt(int givenPosition) {
          assert !isEmpty() &amp;&amp; (1 &lt;= givenPosition)
                  &amp;&amp; (givenPosition &lt;= numberOfEntries);
          Node currentNode = firstNode;
  
          // traverse the list to locate the desired node
          for (int counter = 1; counter &lt; givenPosition; counter++)
              currentNode = currentNode.getNextNode();
  
          assert currentNode != null;
  
          return currentNode;
      } // end getNodeAt
  
      public void addFirst(Object newEntry) {
          Node newNode = new Node(newEntry);
  
          if (firstNode == null)
          {
              firstNode = newNode;
              lastNode = newNode;
              numberOfEntries++;
          }
          else 
          {
              newNode.setNextNode(firstNode);
              firstNode.setPreviousNode(newNode);
              firstNode = newNode;
              numberOfEntries++;
          } // end if
      } // end add
  
  
      public void addLast(Object newEntry) {
          Node newNode = new Node(newEntry);
  
          if (firstNode == null)
          {
              firstNode = newNode;
              //lastNode = newNode;
              numberOfEntries++;
          }
          else 
          {
              lastNode.setNextNode(newNode);
              lastNode = newNode;
              numberOfEntries++;
          } // end if
      } // end add
  
      @Override
      public Object removeFirst() {
          Object result =  firstNode.getData();
          firstNode = firstNode.getNextNode();
          if (numberOfEntries == 1)
              lastNode = null; // solitary entry was removed
  
          numberOfEntries--;
  
          return result; // return removed entry, or
                          // null if operation fails
      } // end remove
  
      @Override
      public Object removeLast() {
          Object result = null;
  
          if (!isEmpty()) {
              result = lastNode.getData();
              lastNode = lastNode.getPreviousNode();
  
              if (lastNode == null)
                  firstNode = null;
              else
                  lastNode.setNextNode(null);
          }
          return result;
      }
  
      @Override
      public Object getFirst() {
          // TODO Auto-generated method stub
          return firstNode.getData();
      }
  
      @Override
      public Object getLast() {
          // TODO Auto-generated method stub
          return lastNode.getData();
      }
  
      @SuppressWarnings("unchecked")
      @Override
      public void moveToEnd() {
          Object temp = (Object) remove(1);
          add(numberOfEntries + 1, temp);
  
      }
  
      public class Node {
          public Object data; // entry in bag
          public Node next; // link to next node
          public Node previous;
  
          public Node(Object dataPortion) {
              this(dataPortion, null, null);
          } // end constructor
  
          public Node(Object dataPortion, Node nextNode, Node previousNode) {
              data = dataPortion;
              next = nextNode;
              previous = previousNode;
          } // end constructor
  
          public Object getData() {
              return data;
          } // end getData
  
          public void setData(Object newData) {
              data = newData;
          } // end setData
  
          public Node getNextNode() {
              return next;
          } // end getNextNode
  
          public Node getPreviousNode() {
              return previous;
          }
  
          public void setPreviousNode(Node previous) {
              this.previous = previous;
          }
  
          public void setNextNode(Node nextNode) {
              next = nextNode;
          } // end setNextNode
  
          @Override
          public String toString() {
              return "Node [data=" + data + ", next=" + next + "]";
          }
  
      } // end Node
  
      @Override
      public void add(Object newEntry) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public boolean add(int newPosition, Object newEntry) {
          // TODO Auto-generated method stub
          return false;
      }
  
      @Override
      public Object remove(int givenPosition) {
          // TODO Auto-generated method stub
          return null;
      }
  
      @Override
      public void clear() {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public boolean replace(int givenPosition, Object newEntry) {
          // TODO Auto-generated method stub
          return false;
      }
  
      @Override
      public Object getEntry(int givenPosition) {
          // TODO Auto-generated method stub
          return null;
      }
  
      @Override
      public boolean contains(Object anEntry) {
          // TODO Auto-generated method stub
          return false;
      }
  
      @Override
      public int getLength() {
          // TODO Auto-generated method stub
          return 0;
      }
  
      @Override
      public boolean isEmpty() {
          boolean result;
  
          if (numberOfEntries == 0) // or getLength() == 0
          {
              assert firstNode == null;
              result = true;
          } else {
              assert firstNode != null;
              result = false;
          } // end if
  
          return result;
      } // end isEmpty
  
      @Override
      public void swap(int i, int j) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public boolean isTooBig() {
          // TODO Auto-generated method stub
          return false;
      }
  
      @Override
      public void reduceArray() {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public Object[] toArray() {
          // TODO Auto-generated method stub
          return null;
      }
  
      @Override
      public void makeRoom(int newPosition) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void removeGap(int givenPosition) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public boolean removeObject(Object anObject) {
          // TODO Auto-generated method stub
          return false;
      }
  
      @Override
      public int getPosition(Object anObject) {
          // TODO Auto-generated method stub
          return 0;
      }
  
      @Override
      public void append(ListInterface bList) {
          // TODO Auto-generated method stub
  
      }
  }
  </code></pre>