title: Represent polynom in linked list
body: <p>I need to represent a polynom in classes:</p>
  
  <blockquote>
    <p>PolyNode (with the next variables):</p>
    
    <ol>
    <li>int _power</li>
    <li>double _coefficient</li>
    <li>PolyNode _next</li>
    </ol>
    
    <p>Polynom (with the next constructor &amp; method):</p>
    
    <ol>
    <li>public Polynom () - empty constructor - empty list</li>
    <li>public Polynom (PolyNode p)  - gets param from PolyNode and insert him as first on the list</li>
    <li>public Polynom addNode (PolyNode p) - get parameter from PolyNode and add him to Polynom. <strong>and returns the new Polynom</strong></li>
    </ol>
  </blockquote>
  
  <p>This is the test case:</p>
  
  <pre><code>   // Create two Polynoms
      Polynom p1 = new Polynom();
      p1.addNode(new PolyNode(0,2));
      p1.addNode(new PolyNode(2,4));   
      System.out.println("\nP1:");
      System.out.println(p1);
  
      Polynom p2 = new Polynom(new PolyNode(0,2));
      p2.addNode(new PolyNode(2,-1));   
      p2.addNode(new PolyNode(4,5));  
      System.out.println("\nP2:");
      System.out.println(p2);
  </code></pre>
  
  <p>This is the wanted output:</p>
  
  <blockquote>
    <p>P1:
    4.0x^2+2.0</p>
    
    <p>P2:
    5.0x^4-1.0x^2+2.0</p>
  </blockquote>
  
  <p>That is the PolyNode class I wrote:</p>
  
  <pre><code>public class PolyNode
  {
       char _operation;
       int _power;      
       double _coefficient;     
       PolyNode _next; 
  
  
  
       public PolyNode()
  {
    _next = null;
    _operation = '+';
    _coefficient = 1;
    _power = 1;
  }
  
    public PolyNode(char oper, double coeff, int power, PolyNode next)
  {
    _operation = oper;
    _coefficient = coeff;
    _power = power;
    _next = next;
  }
  
  
  public PolyNode(PolyNode next)
  {
    _next = next;
  }
  
  public PolyNode(int power, int coeff)
  {
  _power = power;
    _coefficient = coeff;
  }
  
  public void setSign(char oper)
  {
    _operation = oper;
  }
  
  
  public void setCoef(double coeff)
  {
    _coefficient = coeff;
  }
  
  
  public void setPower(int power)
  {
    _power = power;
  }
  
  
  public void setNext(PolyNode next)
  {
    _next = next;
  }
  
  
  public char getSign()
  {
    return _operation;
  }
  
  
  public double getCoeff()
  {
    return _coefficient;
  }
  
    public int getPower()
  {
    return _power;
  }
  
  public PolyNode getNext()
  {
    return _next;
  }
  
  public boolean isEnd()
  {
    return (_next == null);
  }
  
  }
  </code></pre>
  
  <p>That is the Polynom class that I wrote:</p>
  
  <pre><code>public class Polynom
  {
      private PolyNode _head;
  
  
      public Polynom ()
      {
          _head = null;
      }
  
          public Polynom (Polynom poly)
      {
          Polynom r = new Polynom (poly);
      }
  
      public Polynom (PolyNode p)
      {
          _head = p;
      }
  
      public Polynom addNode (PolyNode p)
      {
          Polynom r = new Polynom (p);
          PolyNode current;
  
          if (_head == null)
                _head = p;
  
           else
           {
               current = _head;
               while (current._next !=null)
                  current = current._next;
                  current._next = p;
  
              }
                  return r;
          }
  
          public String toString()
          {
              String s = "";
  
              while (_head != null)
              {
              s += _head.getCoeff() + "x^" + _head.getPower(); 
             _head = _head._next;
          }
  
              return s;
  
          }
      }
  </code></pre>
  
  <p>And this is my wrong output:</p>
  
  <blockquote>
    <p>P1:
    2.0x^04.0x^2</p>
    
    <p>P2:
    2.0x^0-1.0x^25.0x^4</p>
  </blockquote>
  
  <p>I didn't understand the idea of linked lists!</p>
  
  <p>The <code>toString()</code> method needs to output like that example:</p>
  
  <blockquote>
    <p>r = 3.8x10 − 5.9x3 + 5.5x2 − 11.0 </p>
  </blockquote>
  
  <p>will display like that on the toString():</p>
  
  <blockquote>
    <p>3.8 x^10 – 5.9 x^3 + 5.5 x^2 – 11.0</p>
  </blockquote>