title: Creating push and pop methods within a user defined LinkedList
body: <p>Recently, my class has been studying ArrayLists and LinkedLists.  This past week we received an assignment that asked us to create push and pop methods within our LinkedList stack class.  I understand the logic behind stacks such that it's last-in-first-out, but I am having trouble with the actual code.  I am fairly new to computer science (this being my second course ever) and this particular assignment is literally causing me to pull my hair out.  I already turned this assignment in, but we have a midterm next week and I would like to do well on it.  I have been all over the web and my textbook looking for help, but nothing.  My professor only refers me to the TA and the TA is only concerned with helping me with the logic, not the actual code.  I'll post the instructions my professor gave me below, as well as my code so far.  Thanks in advance.</p>
  
  <p><strong>From the professor:</strong></p>
  
  <p>Implement stacks using the template given in the following Java 
  files:</p>
  
  <p>CS401StackInterface.java CS401StackLinkedListImpl.java</p>
  
  <pre><code>public interface CS401StackInterface&lt;E&gt;
  {
     /**
      * Get the top element on the stack.
      * 
      * @return the first element on the stack.
      */
     public E pop();
  
     /**
      * Adds an element on the top of the stack.
      * 
      * @param e - The element to be added to the stack.
      */
     public void push(E e);
  
     /**
      * Determines the number of elements in this data structure.
      * 
      * @return the number of elements currently resident in this
      *         data structure.
      */
     public int size();
  }
  </code></pre>
  
  <p>Here is the actual class where I attempt to define my methods:</p>
  
  <pre><code>public class CS401StackLinkedListImpl&lt;E&gt; implements CS401StackInterface&lt;E&gt; 
  {
      private LinkEntry&lt;E&gt; head;
      private int num_elements;
  
      public CS401StackLinkedListImpl()
      {
          head = null;
          num_elements = 0;
      }
  
      public void setElement(LinkEntry&lt;E&gt; anElement){
          head = anElement;
      }
  
      /*Append the new element to the end of the list*/
      public void push(E e)
      {
          LinkEntry&lt;E&gt; temp = new LinkEntry&lt;E&gt;();
          temp.element = e;
          temp.next = head;
          head = temp;
      }
  
      /*Remove the most recently pushed element at the end of the list*/
      public E pop()
      {
          head.next = head;
          num_elements--;
          return (E) head;
      }
  
      public int size()
      {
          LinkEntry&lt;E&gt; temp = new LinkEntry&lt;E&gt;();
          for (temp = head; head != null; head = head.next)
              num_elements++;
          return num_elements;
      }
  
      public String toString()
      {
          String string = "";
          LinkEntry&lt;E&gt; temp = new LinkEntry&lt;E&gt;();
          for (temp = head; temp != null; temp = temp.next)
          {
              string += temp.element.toString() + "";
          }
          return string;
      }
  
  /* ------------------------------------------------------------------- */
  /* Inner classes                                                      */
      protected class LinkEntry&lt;E&gt;
      {
          protected E element;
          protected LinkEntry&lt;E&gt; next;
  
          protected LinkEntry() { element = null; next = null; }
      }
  }
  </code></pre>
  
  <p>Finally, here is my main class where I test my methods:</p>
  
  <pre><code>import java.util.*;
  
  public class App {
  
      public static &lt;E&gt; void main(String[] args) {
  
          CS401StackLinkedListImpl&lt;String&gt; my_stack = new CS401StackLinkedListImpl&lt;String&gt;();
          my_stack.push("Brian");
          my_stack.push("Chris");
          my_stack.push("Joe");
          System.out.println("Stack size: " + my_stack.size());
          my_stack.pop();
          System.out.println("Stack size: " + my_stack.size());
          my_stack.toString();
      }
  
  }
  </code></pre>
  
  <p>When I run my main class this is what it returns:</p>
  
  <pre><code>Stack size: 3
  Exception in thread "main" java.lang.NullPointerException
      at week6.CS401StackLinkedListImpl.pop(CS401StackLinkedListImpl.java:30)
      at week6.App.main(App.java:66)
  </code></pre>
  
  <p>Everything I've come across just tells me to create a new Stack, which is easy because then I don't have to worry about the "innards" of the code, but that's not what I need.  Thanks.</p>