title: LinkedStack operation methods
body: <p>I am currently beginning on LinkedStacks and I was wondering why isn't the toString and pop method working? The methods I used are the default given by the book that is Pop and toString methods, the rest I worked for and are functioning well. The push methods add elements perfectly. The peek, looks at the top element without altering the List and Size well returns the count of the times I used the push method. The pop method strangely only works one time then gives out an error. Note on the example that the book gives of the toString method in the section of Stacks doesn't seem function either. I will gladly take any pointers but please know that I am just a beginner and I am learning. This is the code of the class:</p>
  
  <h1>Code</h1>
  
  <pre><code>public class LinkedStack&lt;T&gt; implements Stack&lt;T&gt; {
  
  
  
  private int count;
  private LinearNode&lt;T&gt; top;
  
  //-----------------------------------------------------------------
  // Creates an empty stack using the default capacity.
  //-----------------------------------------------------------------
  public LinkedStack()
  {
  count = 0;
  top = null;
  }
  
  
      @Override
      public boolean IsEmpty()
  
      {
          if(top == null)
          {
          System.out.println("Stack is empty");
          }
          return top == null;
      }
  
      @Override
      public void Push(T element)
  {
  
      top = new LinearNode(element, top);
      System.out.println(element);
      count++;
  
  
  }
  
      @Override 
      public T Pop() 
  {
      T result;
      System.out.println("Lets pop the top element!");
  
      if (count == 0) 
      {
      System.out.println("Pop operation failed. "+ "The stack is empty.");
      }
  
      result = top.getElement();
      top = top.getNext();
      count--;
      System.out.println("The element that we have poped is :" + result);
      return result;
  
  }
  
      Override
      public String toString()
  {
  
      String result = "&lt;top of stack&gt;\n";
  LinearNode current = top;
  while (current != null)
  {
  result += current.getElement() + "\n";
  current = current.getNext();
  }
  return result + "&lt;bottom of stack&gt;";
  
  }
  
      @Override
      public T Peek() {
          System.out.println("Lets peek the top element!");
          if(count == 0)
          {
           System.out.println("Peek failed stack is empty");
          }
          System.out.println("The element that we have peeked is: " + top.getElement());
          return top.getElement();
  
      }
  
      @Override
      public int Size() {
          System.out.println("The size of the list now is: " + count);
          return count;
      }
  
  
      }
  </code></pre>
  
  <h1>Code of main class:</h1>
  
  <p>public class LSmain {</p>
  
  <pre><code>public static void main(String[]args)
  {
   LinkedStack&lt;Integer&gt; main = new LinkedStack&lt;&gt;();   
   main.Push(1);
   main.Push(2);
   main.Push(3);
   main.Size();
   main.Peek();
   main.Pop();
   main.Pop();
   main.Size();
   main.toString();
  
  
  }
  </code></pre>
  
  <p>}</p>
  
  <h1>Output using the pop method twice</h1>
  
  <pre><code>1
  Exception in thread "main" java.lang.NullPointerException
  2
  3
  The size of the list now is: 3
  Lets peek the top element!
  The element that we have peeked is: 3
  Lets pop the top element!
  The element that we have poped is: 3
  Lets pop the top element!
      at LinkNod.LinkedStack.Pop(LinkedStack.java:64)
      at LinkNod.LSmain.main(LSmain.java:22)
  Java Result: 1
  BUILD SUCCESSFUL (total time: 0 seconds)
  </code></pre>
  
  <h1>Output without using pop method twice</h1>
  
  <pre><code>run:
  1
  2
  3
  The size of the list now is: 3
  Lets peek the top element!
  The element that we have peeked is: 3
  Lets pop the top element!
  The element that we have poped is: 3
  The size of the list now is: 2
  </code></pre>
  
  <h1>Analysis</h1>
  
  <p>/<strong>This actually creates a new Node and stores the value that is inserted in the parameters
  Mistake was  that I cannot give top an assignment to the same list, because if I do this the List will be made of only one Node which is the top. Like my past code said, I was only saying that the top brick will equal to itself. So what I understand from the class was to make a new object type LinearNode that stored the elements then top will equal the value of that new Node. Given by this case Pop will work since there will be more Nodes not one. Extra note on the toString method was that simply that the return; in java sometimes displays the value and most of the times it doesnt meaning that you need to add a System.out.println(); in the driver when you call the method or in the method instead.</strong>/</p>
  
  <h1>Push method corrected:</h1>
  
  <pre><code>@Override
      public void Push(T element)
  {
  
       LinearNode&lt;T&gt; current = new LinearNode&lt;&gt;(element); 
  
       current.setNext(top);
       top = current;
       count++;
  
  
  }
  </code></pre>
  
  <h1>Code of main class:</h1>
  
  <pre><code>public class LSmain {
  
      public static void main(String[]args)
      {
       LinkedStack&lt;Integer&gt; list = new LinkedStack&lt;&gt;();   
       System.out.println("Let's make a List!");
       System.out.println("Push 3 times.");
       System.out.println("Check the size.");
       System.out.println("Peek the top element.");
       System.out.println("Pop three times.");
       System.out.println("The size now should be zero!" + "\n");
       list.Push(1);
       list.Push(2);
       list.Push(3);
       System.out.println(list.toString());
       list.Size();
       list.Peek();
       list.Pop();
       list.Pop();
       list.Pop();
       list.Size();
  
  
  
      }
  </code></pre>
  
  <h1>Functioning output</h1>
  
  <pre><code>run:
  Let's make a List!
  Push 3 times.
  Check the size.
  Peek the top element.
  Pop three times.
  The size now should be zero!
  
  &lt;top of stack--&gt;[3][2][1]&lt;--bottom of stack&gt;
  
  Let's check the size of the list!
  The size of the list is: '3'
  
  Lets peek the top element!
  The element that we have peeked is: [3]
  
  Lets pop the top element!
  The element that we have poped is: '3'
  
  Lets pop the top element!
  The element that we have poped is: '2'
  
  Lets pop the top element!
  The element that we have poped is: '1'
  
  The size of the list is...Woah.
  The list size is now: '0'
  Push more elements!
  BUILD SUCCESSFUL (total time: 3 seconds)
  </code></pre>
  
  <p>Thanks for the help! 
  PS: I forgot to change the methods declaration to camel case. </p>
  
  <h1>LinearNode code#</h1>
  
  <p>/<strong>Had incorrect parameters that gave trouble when I tried to correct the push method by creating a new Node object.</strong>/</p>
  
  <p>package LinkNod;</p>
  
  <pre><code>public class LinearNode&lt;T&gt; {
  
      private LinearNode&lt;T&gt; next; //se guarda la referencia del Nodo
      private T element;          //Lista vacia
  
      public LinearNode()
  {
  next = null;
  element = null;
  }
  //-----------------------------------------------------------------
  // Creates a node storing the specified element.
  //-----------------------------------------------------------------
  public LinearNode (T elem)
  {
  next = null;
  element = elem;
  }
  //-----------------------------------------------------------------
  // Returns the node that follows this one.
  //-----------------------------------------------------------------
  public LinearNode&lt;T&gt; getNext()
  {
  return next;
  }
  //-----------------------------------------------------------------
  // Sets the node that follows this one.
  //-----------------------------------------------------------------
  public void setNext (LinearNode&lt;T&gt; node)
  {
  next = node;
  }
  //-----------------------------------------------------------------
  // Returns the element stored in this node.
  //-----------------------------------------------------------------
  public T getElement()//asigna valor
  {
  return element;
  }
  
  public void setElement(T elem)
  {
  
      element = elem;
  
  
  }
  
  
  
  }
  </code></pre>