// NewPolynomial returns a new polynomial based on input polynomial expressions.
func NewPolynomial(e ...*PolyTerm) *Polynomial {
	p := &Polynomial{list.NewLinkedList(false, false, true)}
	// Insert in the corresponding location such that expression is descending exponent.
	for _, v := range e {
		i := 0
		inserted := false
		for it := p.LinkedListIterator(); it.Next(); i++ {
			exp := it.Value().(*PolyTerm)
			if exp.Exponent == v.Exponent {
				inserted = true
				exp.Coefficient += v.Coefficient
				if math.Abs(exp.Coefficient) < 0.00000001 {
					p.RemoveAt(i)
				}
				break
			} else if exp.Exponent < v.Exponent {
				inserted = true
				p.Insert(i, v)
				break
			}
		}
		if !inserted {
			p.LinkedList.Add(v)
		}
	}
	p.LinkedList.Add(&PolyTerm{0, 0})
	return p
}
// Copy returns a (deep) copy of polynomial.
func (p *Polynomial) Copy(i int) *Polynomial {
	n := &Polynomial{list.NewLinkedList(p.IsFixedSize(), p.IsReadOnly(), p.IsSynchronized())}
	for it := p.LinkedListIterator(); it.Next(); {
		exp := it.Value().(*PolyTerm)
		n.LinkedList.Add(&PolyTerm{exp.Coefficient, exp.Exponent})
	}
	return n
}