2014-06-24 4 views
0

이것은 여기에서 처음으로 묻습니다. 자바에서 연결된 목록을 사용하여 다항식을 만들었습니다.다항식 링크 된 목록이 목록을 반환하지 않습니다

나는 다항식의 문자열을 연결된 목록의 노드로 변환하고 더하기와 빼기를 시도합니다.

다항식을 용어로 나눠서 링크 된 목록에 저장하는 방법을 만듭니다. 그 방법으로 목록을 인쇄하려고했는데 제대로 작동했습니다. 그러나 toString 메서드를 사용하여 목록을 인쇄하거나 메서드를 추가하거나 빼면 내 목록이 null이라고 표시됩니다. 누군가가 나를 도와 줄 수 있고 왜 그 목록이 null로 가는지 설명해 줄 수 있습니까?

이 내 용어 클래스입니다

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(); 
    } 
} 
} 

당신의 addsubtract 방법에서

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

답변

1

는 기본 생성자를 사용하여 목록을 작성하는 내 PolynomialInterface , 그것의를 놓는에서 null으로 변경하고 다른 작업은 수행하지 않고 빈 목록을 반환합니다. addsubtract의 기능을 실제로 구현해야합니다. 알맞은 입문서는 How do I implement a Linked List in Java?을 참조하십시오.

+0

각 메소드에서'other'리스트를 반환하는 것에 대해 더 자세히 설명해 주시겠습니까? 어떻게 돌려 줄 수 있습니까? – user3769256

+0

죄송합니다. 나는 그 두 번째 문장을 답장하고 썼을 때 충분한 방법을 읽지 않았습니다. 내 대답을 편집했습니다. – tophyr

관련 문제