title: Evaluate a Postfix Expression Using a Singly Linked List
body: <p>I'm writing a program that asks the user for a postfix expression, and then outputs the result to the expression. I am attempting to do this using a Singly Linked List, and using the Adapter Pattern to create a stack.</p>
  
  <p>The code for the <code>SinglyLinkedList</code> class, the <code>LinkedStack</code> class, and the <code>Stack</code> implementation are all straight out of a Data Structures book that I own. So the <code>SinglyLinkedListTest</code> class is the only one that has my own code in it (and has errors).</p>
  
  <p>I've written a program that simply uses a stack to evaluate a postfix expression before, but I'm getting confused  this time with the extra classes included.</p>
  
  <p>I'm sure I have a ton of errors, but the most obvious ones to me are in my <code>SinglyLinkedListTest</code> class, every time I push a value onto the stack. I know the issue is that I am attempting to push Objects and characters onto the stack instead of the arguments that match push(E e), but I don't know how to alter my code to make this work.</p>
  
  <p>Any suggestions or input would be greatly appreciated.</p>
  
  <p><strong>Here is my Stack Implementation:</strong></p>
  
  <pre><code>package PostFix;
  
  public interface Stack&lt;E&gt; 
  {
      int size();
  
      boolean isEmpty();
  
      void push(E e);
  
      E pop();
  }
  </code></pre>
  
  <p><strong>Here is my LinkedStack class:</strong></p>
  
  <pre><code>package PostFix;
  
  public class LinkedStack &lt;E&gt; implements Stack&lt;E&gt;
  {
      private SinglyLinkedList&lt;E&gt; list = new SinglyLinkedList&lt;&gt;();
  
      public LinkedStack()
      {
  
      }
  
      public int size()
      {
          return list.size();
      }
  
      public boolean isEmpty()
      {
          return list.isEmpty();
      }
  
      public void push(E e)
      {
          list.addFirst(e);
      }
  
      public E pop()
      {
          return list.removeFirst();
      }
  }
  </code></pre>
  
  <p><strong>Here is my SinglyLinkedList class:</strong></p>
  
  <pre><code>package PostFix;
  
  public class SinglyLinkedList&lt;E&gt;
  {
      private static class Node&lt;E&gt;
      {
          private E element;
          private Node&lt;E&gt; next;
  
          public Node(E e, Node&lt;E&gt; n)
          {
              element = e;
              next = n;
          }
  
          public E getElement()
          {
              return element;
          }
  
          public Node&lt;E&gt; getNext()
          {
              return next;
          }       
      }
  
      private Node&lt;E&gt; head = null;
      private Node&lt;E&gt; tail = null;
      private int size = 0;
  
      public SinglyLinkedList()
      {
  
      }
  
      public int size()
      {
          return size;
      }
  
      public boolean isEmpty()
      {
          return size == 0;
      }
  
      public void addFirst(E e)
      {
          head = new Node&lt;&gt;(e, head);
  
          if (size == 0)
          {
              tail = head;
          }
          size++;
      }
  
      public E removeFirst()
      {
          if (isEmpty())
          {
              return null;
          }
          E answer = head.getElement();
          head = head.getNext();
          size--;
  
          if (size == 0)
          {
              tail = null;
          }
          return answer;
      }
  }
  </code></pre>
  
  <p><strong>Here is my <em>final</em> SinglyLinkedListTest class:</strong></p>
  
  <pre><code>package PostFix;
  import java.util.Scanner;
  
  public class SinglyLinkedListTest 
  {
      public static void main(String[] args)
      {
          Double num1, num2, answer;
          char c;
  
          Stack&lt;Double&gt; stack = new LinkedStack&lt;&gt;();
          Scanner input = new Scanner(System.in);
  
          System.out.println("Enter the expression you would like to evaluate: ");
          String someString = input.nextLine();
  
          for (int index = 0; index &lt; someString.length(); index++)
          {
              c = someString.charAt(index);
  
              if (Character.isDigit(c))
              {
                  stack.push((double)Character.digit(c, 10));
              }
              else if (c == '+')
              {
                  num2 = stack.pop();
                  num1 = stack.pop();
                  answer = num1+num2;
                  stack.push(answer);
              }
              else if (c == '-')
              {
                  num2 = stack.pop();
                  num1 = stack.pop();
                  answer = num1-num2;
                  stack.push(answer);
              }
              else if (c == '*')
              {
                  num2 = stack.pop();
                  num1 = stack.pop();
                  answer = num1*num2;
                  stack.push(answer);
              }
              else if (c == '/')
              {
                  num2 = stack.pop();
                  num1 = stack.pop();
                  answer = num1/num2;
                  stack.push(answer);
              }     
          }
          System.out.println("The result is: " + stack.pop());
      }
  }
  </code></pre>