title: Java linked list implementation
body: <p>I am learning Linked Lists right now and I'm a little confused.  I'm working on an assignment where we're creating our own implementation of some methods to be used with a linked list.  </p>
  
  <p>I watched a lot of videos on the concept of Linked List and how Nodes work like getting data and setting the next element but I'm confused on how to make a list to test the methods I'm implementing.  I know this may sound silly but I start to get super confused with multiple parameters in methods and return statements get involved and I've never worked with a generic data type before either so those basic ideas might be confusing me here.</p>
  
  <p><strong>I'd love some help with creating a list so I can test the methods as I'm creating them, I'm having difficulty printing out a list.  I get that what I'm doing in the main is creating new nodes and adding to some list but I honestly don't know where this supposed LinkedList is or what it's called.  Any help is greatly appreciated!!  FYI, I'm very new to Java, this is my second class and it's online and not a very well constructed class, please be nice lol</strong></p>
  
  <p>I get the concept of this BUT what is the name of the list (or linked list) these are being added to?</p>
  
  <pre><code>    ListNode node4 = new ListNode("Fourth", null);
      ListNode node3 = new ListNode("Third", node4);
      ListNode node2 = new ListNode("Second", node3);
      ListNode node1 = new ListNode("First", node2);
  </code></pre>
  
  <p>I DON'T KNOW HOW I CAN PRINT THESE OUT INDIVIDUALLY LIKE I CAN WITH THE ABOVE BECAUSE I CAN USE THE NAMES ABOVE</p>
  
  <pre><code>ListNode value = new ListNode("First", new ListNode("Second", new ListNode("Third", null) ));
  </code></pre>
  
  <p>Here is my code with the testing class where I have questions as comments which may help you understand what I'm confused about if I'm not making sense above.  I left out the ListNode class as it is your basic getNext() setNext() Node class called ListNode with a generic type ....</p>
  
  <pre><code>//Testing program for SinglyLinkedList class
  public class LinkedListDriver {
  
  public static void main(String[] args) {
      //List&lt;String&gt; list = new LinkedList&lt;String&gt;();                   //comment out this line to test your code
      SinglyLinkedList&lt;String&gt; list = new SinglyLinkedList&lt;String&gt;();  //remove comment to test your code
      SinglyLinkedList SLL = new SinglyLinkedList();
  
      ListNode node4 = new ListNode("Fourth", null);
      ListNode node3 = new ListNode("Third", node4);
      ListNode node2 = new ListNode("Second", node3);
      ListNode node1 = new ListNode("First", node2);
  
  //ListNode value = new ListNode("First", new ListNode("Second", new ListNode("Third", null) ));
  
  System.out.println(node2.getData());
  
  //Is this the correct way to add a new node to this method?
      SLL.addLast(new ListNode("Fifth", null));
  //I doubt my printList is correct as I do not know what parameter I am       supposed to pass to it.    
      SLL.printList();
  }
  </code></pre>
  
  <p>SinglyLinkedList class</p>
  
  <pre><code>//This class implements a very simple singly-linked list of Objects
  public class SinglyLinkedList&lt;E&gt; {
       ListNode&lt;E&gt; first; // first element
  
  public SinglyLinkedList()  {
      first = null;
  }
  
  public E getFirst() {
     if (first == null) {
        throw new NoSuchElementException();
     }
     else
        return first.getData();
     }
  
  public void addFirst(E value) {
      first = new ListNode&lt;E&gt;(value, first);
  }
  
  // Methods below implemented by you. Note: while writing methods, keep in mind
  // that you might be able to call other methods in this class to help you - you
  // don't always need to start from scratch(but you'll have to recognize when)
  public void addLast(E value) {    
      ListNode&lt;E&gt; temp = first;
      //If list is empty make new node the first node.
      if(temp == null) {
          first = new ListNode &lt;E&gt;(value, null);
          first.setNext(null);
      }
      //Otherwise loop to end of list and add new node.
      else {
          while(temp.getNext() != null) {
              temp = temp.getNext();
          }
      temp.setNext(new ListNode&lt;E&gt;(value, null));
      }
  }//end addLast
  
  // throws an exception - you decide when and which one
  public E getLast() {
      ListNode&lt;E&gt; temp = first;
      if(temp == null) {
          throw new NullPointerException("There are no elements in this list to get.");
      }
      else {
          while(temp.getNext() != null) {
              temp = temp.getNext();
          }
          return temp.getData();
      }
  }
  
  // throws an exception - you decide when and which one
  public E removeFirst() {
      if(first == null) {
          throw new NullPointerException("There are no elements in this list to remove.");
      }
      ListNode&lt;E&gt; tempRemove = first;  
      return null;//just so it'll compile
    }
  
    // throws an exception - you decide when and which one
    public E removeLast() {
       return null; //just so it'll compile
    }
  
    // return the number of elements in the list
    public int size() {
        return 0;//just so it'll compile
    }
  
    // return true if o is in this list, otherwise false
    public boolean contains(E obj) {
        return true;//just so it'll compile
    }
  
   public void printList(java.io.PrintStream out) {
       if(first == null) {
          System.out.println("The list is empty");
       }
       ListNode&lt;E&gt; current = first;
       while(current != null) {
           System.out.println(current.toString());
           current = current.getNext();
       }
    }
  
    public String toString() {
         String s = "[";
         ListNode&lt;E&gt; current = first;
      //write code to traverse the list, adding each object on its own line
      while(current.getNext() != null) {
          current = current.getNext();
      }
  
    s += "]";
    return s;
    }
  
    // OPTIONAL: just for fun...and a challenge
    public void reverse() {
    }
  }    
  </code></pre>