title: Adding and removing elements in a user defined Java Doubly LinkedList
body: <p>I'm studying for an exam I have next week and I was hoping somebody could help me out when it comes to adding and removing elements from a doubly linked list.  The logic makes sense to me, unfortunately I am not that great at coding yet (only my second computer science course).  I have looked all over the web and in my text as well, but I can't seem to find an example close enough to my problem.  Below I will post what my professor is asking me to accomplish, as well as my current code.  Thank you in advance for any help.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I was having issues with adding as well, but somebody was able to help me out, however I'm still having issues with my remove method.  I find it odd that my professor has a remove method with a return type other than void.  Can somebody explain this?  Anyways, my updated code is below.</p>
  
  <p><strong>From the professor:</strong></p>
  
  <p>Fill in the missing code in CS401DblLinkedListImpl.java as 
  indicated in the class lecture.</p>
  
  <p>To test your code, create a doubly linked list and enter the 
  following elements of type String in it:</p>
  
  <p>Bill, Rohan, James, Krishna, Javier, Lisa</p>
  
  <p>(a) Print the linked list starting at the beginning.</p>
  
  <p>(b) Print the linked list starting at the end.</p>
  
  <p>(c) Remove Bill and print the linked list starting from beginning.</p>
  
  <p>(d) Remove Lisa and print the linked list starting from end.</p>
  
  <p>(e) Remove Krishna and print the linked list starting from the 
  beginning.</p>
  
  <p>This following set of code is my user defined class and the methods I will be testing are boolean add(E e), E remove(int n), void print_from_beginning(), and void print_from_end():</p>
  
  <pre><code>package week6;
  
  import java.util.Iterator;
  
  public class CS401DblLinkedListImpl&lt;E&gt; //implements CS401CollectionInterface&lt;E&gt; 
  {
     private LinkEntry&lt;E&gt; head = null;
     private LinkEntry&lt;E&gt; tail = null;
     private int size = 0;
  
     public CS401DblLinkedListImpl()
     {
        head = tail = null;
     }
  
     public boolean is_empty()
     {
        if (head == null) 
            return true;
  
        return false;
     }
  
     public int size()
     {
         int count = 0;
         for (LinkEntry&lt;E&gt; current = head; current != null; current = current.next)
             count++;
         return count;
     }
  
     /*
      * Add e to the end of the doubly linked list.
      * Returns true - if e was successfully added, false otherwise.
      */
     public boolean add(E e)
     {   
        LinkEntry&lt;E&gt; new_element = new LinkEntry&lt;E&gt;();
        new_element.element = e;
  
            if (head == null)
            {
            new_element.next = head;
            head = new_element;
            tail = head;
        }
        else
        {
            tail.next = new_element;
            new_element.previous = tail;
            tail = new_element;
        }
        return true;
     }
  
     /*
      * Remove the nth element in the list.  The first element is element 1.
      * Return the removed element to the caller.
      */
     public E remove(int n)
     {
        LinkEntry&lt;E&gt; current = new LinkEntry&lt;E&gt;();
        int i = 0;
  
        while (n == i++)
        {
            current.previous.next = current.next;
            if (current.next == null)
            {
                current.next.previous = current.previous;
            }
        }
        return (E) current;
     }
  
    /*
     * Print the doubly linked list starting at the beginning.
     */
     public void print_from_beginning()
     {
        LinkEntry&lt;E&gt; current = new LinkEntry&lt;E&gt;();
        for (current = head; current != null; current = current.next)
        {
            System.out.print(current.element + " ");
        }
     }
  
     /*
      * Print the doubly linked list starting the end.
      */
     public void print_from_end()
     {
        LinkEntry&lt;E&gt; current = new LinkEntry&lt;E&gt;();
        for (current = tail; current != null; current = current.previous)
        {
            System.out.print(current.element + " ");
        }
     }
  
     /* ------------------------------------------------------------------- */
     /* Inner classes                                                      */
     protected class LinkEntry&lt;E&gt;
     {
        protected E element;
        protected LinkEntry&lt;E&gt; next;
        protected LinkEntry&lt;E&gt; previous;
  
        protected LinkEntry() { element = null; next = previous = null; }
     }
     /* ------------------------------------------------------------------- */
     protected class CS401DblLinkedListImplIterate&lt;E&gt; implements Iterator&lt;E&gt;
     {
  
         protected LinkEntry&lt;E&gt; next;
  
         protected CS401DblLinkedListImplIterate()
         {
             next = (LinkEntry&lt;E&gt;) head;
         }
  
         @Override
         public boolean hasNext() {
             // TODO Auto-generated method stub
             return false;
         }
  
         @Override
         public E next() {
         // TODO Auto-generated method stub
         return null;
         }
  
         @Override
         public void remove() {
         // TODO Auto-generated method stub
         }
        }
  } /* CS401LinkedListImpl&lt;E&gt; */
  </code></pre>
  
  <p>Below is my main class where I test the methods:</p>
  
  <pre><code>package week6;
  
  import java.util.*;
  
  public class App {
  
      public static &lt;E&gt; void main(String[] args) {
  
      /*
       * Part 1 of lab 6: Testing CS401DblLinkedListImpl
       */
      System.out.println("Testing Lab 6: Part 1...");
      CS401DblLinkedListImpl&lt;String&gt; list = new CS401DblLinkedListImpl&lt;String&gt;();
  
      list.add("Bill");
      list.add("Rohan");
      list.add("James");
      list.add("Krishna");
      list.add("Javier");
      list.add("Lisa");
  
      System.out.println("List size after all names are added: " + list.size());
  
      //a. Print the linked list starting at the beginning.
      System.out.println("\nPrint the linked list starting at the beginning:");
      list.print_from_beginning();
  
      //b. Print the linked list starting at the end.
      System.out.println("\nPrint the linked list starting at the end:");
      list.print_from_end();
  
      //c. Remove Bill and print the linked list starting from beginning.
      System.out.println("\nRemove Bill and print the linked list starting from beginning:");
      list.remove(0);
      list.print_from_beginning();
  
      //d. Remove Lisa and print the linked list starting from end.
      System.out.println("\nRemove Lisa and print the linked list starting from end:");
      list.remove(4);
      list.print_from_end();
  
      //e. Remove Krishna and print the linked list starting from the beginning.
      System.out.println("\nRemove Krishna and print the linked list starting from the beginning:");
      list.remove(2);
      list.print_from_beginning();
  
      System.out.println("\nList size: " + list.size());
      }
  }
  </code></pre>
  
  <p>Finally, below is what I get when I run my program:</p>
  
  <pre><code>Testing Lab 6: Part 1...
  List size after all names are added: 6
  
  Print the linked list starting at the beginning:
  Bill Rohan James Krishna Javier Lisa 
  Print the linked list starting at the end:
  Lisa Javier Krishna James Rohan Bill 
  Remove Bill and print the linked list starting from beginning:
  Exception in thread "main" java.lang.NullPointerException
      at week6.CS401DblLinkedListImpl.remove(CS401DblLinkedListImpl.java:67)
      at week6.App.main(App.java:34)
  </code></pre>
  
  <p>FYI, CS401DblLinkedListImpl.java:67 refers to the following line of code in my remove method:</p>
  
  <pre><code>      current.previous.next = current.next;
  </code></pre>
  
  <p>Any help on this would be much appreciated.  I feel like I'm somewhat close to the answer, it just needs some clarifying.  Thanks.</p>