多项式链接列表不返回列表

问题描述:

这是我第一次在这里询问。我在java中使用链接列表创建多项式。多项式链接列表不返回列表

我试图将多项式的字符串转换为链接列表中的节点,并执行加法和减法。

我创建了一个方法,将多项式分解为条件并将其存储在链表中。我试图打印出该方法内的列表,并且它可以工作。但是,当我想使用toString方法打印列表或调用add或subtract方法时,它说我的列表为空。有人可以帮助我,并向我解释为什么列表为空?

这是我的期限类

public class Term 
{ 
    int coef; 
    int exp; 
    Term next; 

public Term() 
{ 
    coef = 0; 
    exp = 0; 
    next = null; 
} 

public Term(int coef, int exp) 
{ 
    this.coef = coef; 
    this.exp = exp; 
    this.next = null; 
} 

public void setCoef(int coef) 
{ 
    this.coef = coef; 
} 

public void setExp(int exp) 
{ 
    this.exp = exp; 
} 

public int getCoef() 
{ 
    return coef; 
} 

public int getExp() 
{ 
    return exp; 
} 

public void setNext(Term next) 
{ 
    this.next = next; 
} 

public Term getNext() 
{ 
    return next; 
} 

public String toString() 
{ 
    String str = ""; 

    if (exp == 0) 
    { 
     if (coef < 0) 
      str += coef; 
     else 
      str += "+" + coef; 
    } 
    else if (exp == 1) 
    { 
     if (coef < 0) 
      str += coef + "x"; 
     else 
      str += "+" + coef + "x"; 
    } 
    else if (coef < 0) 
     str += coef + "x^" + exp; 
    else 
     str += "+" + coef + "x^" + exp; 

    return str; 
} 
} 

我LinkedListPolynomial类

public class LinkedListPolynomial implements PolynomialInterface 
{ 
int coefficient, exponent; 
private Term head; 

public LinkedListPolynomial() 
{ 
    head = null; 
} 

public LinkedListPolynomial(int coefficient, int exponent) 
{ 
    this.coefficient = coefficient; 
    this.exponent = exponent; 
    head = new Term(coefficient, exponent); 
} 

public LinkedListPolynomial(String n) 
{ 
    String s1New = n.replaceAll("-", "+-"); 
    String[] arr = s1New.split("\\+"); 

      //if the first term contains negative coefficient, the first term is empty 
    if (arr[0].isEmpty()) { 
     for (int i = 1; i < arr.length; i++) 
     { 
      if (arr[i].contains("x^")) 
      { 
       String str = arr[i].substring(0, arr[i].indexOf("x^")); 
       String poly = arr[i].substring(arr[i].indexOf("x^") + 2); 

       coefficient = Integer.parseInt(str); 
       exponent = Integer.parseInt(poly); 

       if (head == null) 
       { 
        head = new Term(coefficient, exponent); 
       } 
       else 
       { 
        Term newNode = new Term(coefficient, exponent); 
        newNode.setNext(head); 
        head = newNode; 
       } 
      } 
      else 
      { 
       coefficient = Integer.parseInt(arr[i]); 
       exponent = 0; 

       if (head == null) 
       { 
        head = new Term(coefficient, exponent); 
       } 
       else 
       { 
        Term newNode = new Term(coefficient, exponent); 
        newNode.setNext(head); 
        head = newNode; 
       } 
      } 
     } 
    } 
    else 
    { 
     for (int i = 0; i < arr.length; i++) 
     { 
      if (arr[i].contains("x^")) 
      { 
       String str = arr[i].substring(0, arr[i].indexOf("x^")); 
       String poly = arr[i].substring(arr[i].indexOf("x^") + 2); 

       coefficient = Integer.parseInt(str); 
       exponent = Integer.parseInt(poly); 

       if (head == null) 
       { 
        head = new Term(coefficient, exponent); 
       } 
       else 
       { 
        Term newNode = new Term(coefficient, exponent); 
        newNode.setNext(head); 
        head = newNode; 
       } 
      } 
      else 
      { 
       coefficient = Integer.parseInt(arr[i]); 
       exponent = 0; 

       if (head == null) 
       { 
        head = new Term(coefficient, exponent); 
       } 
       else 
       { 
        Term newNode = new Term(coefficient, exponent); 
        newNode.setNext(head); 
        head = newNode; 
       } 
      } 

     } 
    } 

    selectionSort(); 

    int i = 0; 
    while (head != null) 
    { 
     System.out.print(head); 
     head = head.getNext(); 
     i++; 
    } 
    System.out.println("\n"); 

} 

public PolynomialInterface add(PolynomialInterface other) 
{ 
    LinkedListPolynomial sum = new LinkedListPolynomial(); 
    LinkedListPolynomial parameter = (LinkedListPolynomial) other; 

    return sum; 
} 

public PolynomialInterface subtract(PolynomialInterface other) 
{ 
    LinkedListPolynomial subtract = new LinkedListPolynomial(); 
    LinkedListPolynomial parameter = (LinkedListPolynomial) other; 

    return subtract; 
} 

public String toString() 
{ 
    String str = ""; 
    Term current = head; 

    if (current == null) 
     str += "it's null"; 
    else 
    { 
     while (current.getNext() != null) { current = current.getNext(); 
     str += current.getCoef() + "x^" + current.getExp(); } 
    } 

    return str; 

} 

public void selectionSort() 
{ 
    for (Term node1 = head; node1 != null; node1 = node1.getNext()) // number of iterations 
    { 
     Term min = node1;// assumes min node is the node under 
          // considerations 
     // selects the min node 
     for (Term node2 = node1; node2 != null; node2 = node2.getNext()) 
     { 
      if (min.getExp() < node2.getExp()) 
      { 
       min = node2; 
      } 

     } 
     // swaps the min node with the node in its actual position 
     Term temp = new Term(node1.getCoef(), node1.getExp()); 
     node1.coef = min.getCoef(); 
     node1.exp = min.getExp(); 
     min.coef = temp.getCoef(); 
     min.exp = temp.getExp(); 
    } 
} 
} 

我PolynomialInterface

public interface PolynomialInterface 
{ 
PolynomialInterface add(PolynomialInterface other); 
PolynomialInterface subtract(PolynomialInterface other); 
String toString(); 
} 

在你addsubtract方法,您使用的是默认的构造函数创建列表,它设置了它的headnull并且不做任何其他事情,然后返回该空列表。您需要实际实施addsubtract的功能。见How do I implement a Linked List in Java?作为一个体面的底漆。

+0

你能解释一下更多关于返回每个方法中的'other'列表吗?我该如何回报它? – user3769256

+0

对不起,当我回复并写下第二句话时,我没有读足够的方法。编辑我的答案。 – tophyr