Пример #1
0
  // Helper method to add new nodes to the end of a linked list.
  private void addToEnd(Node toAdd) {

    if (poly == null) {
      poly = toAdd;
    } else {
      Node tmp;
      for (tmp = poly; tmp.next != null; tmp = tmp.next) {}
      tmp.next = toAdd;
    }
  }
Пример #2
0
  // Helper method to combine all like terms in a list. MUST BE SORTED FIRST.
  private void compress() {

    Node tmp = poly;

    while (tmp.next != null) {
      if (tmp.term.degree == tmp.next.term.degree) {
        tmp.term.coeff += tmp.next.term.coeff;
        tmp.next = tmp.next.next;
      } else if (tmp.term.degree < tmp.next.term.degree) tmp = tmp.next;
    }
  }
Пример #3
0
  // Helper method to sort an unsorted linked list in ascending order. All like degrees will be next
  // to each other.
  private void sort() {

    Polynomial newList = new Polynomial();

    for (Node tmp1 = poly; tmp1 != null; tmp1 = tmp1.next) {
      Node n = new Node(tmp1.term.coeff, tmp1.term.degree, null);

      if (newList.poly == null) newList.poly = n;
      else {
        for (Node tmp2 = newList.poly; tmp2 != null; tmp2 = tmp2.next) {
          if (tmp2.next == null) {
            tmp2.next = n;
            break;
          } else if (tmp2.next.term.degree > n.term.degree) {
            n.next = tmp2.next;
            tmp2.next = n;
            break;
          }
        }
      }
    }
    poly = newList.poly;
  }