我正在制作一个可以递归添加多项式(由链接列表表示)的程序。但是我在行if (p1.data.exp == p2.data.exp) {
我已经通过将这两个术语设置为其中一个来尝试调试,但我一直收到错误(这是否意味着它们都是空的?)。我不知道这是怎么发生的,因为如果它们为null,它应该在到达这个部分之前返回。 有没有人有建议?
主:
public class Polynomial {
private Node poly;
public Polynomial(){
poly = new Node();
}
private Polynomial(Node node){
poly = node;
}
public void addTerm(int coef, int exp){
Term term = new Term(coef, exp);
Node node = new Node(term, null);
Node iterator = poly;
//if the list is empty just add the node
if (poly.next == null){
System.out.println("poly.next is null. adding node to first pos.");
poly.next = node;
return;
}
//if list isn't empty find the appropriate spot for it
while (iterator.next != null){
System.out.println("iterator.next != null...");
if (exp < iterator.next.data.exp){
System.out.println("\texp < iterator.next.data.exp");
node.next = iterator.next;
iterator.next = node;
return;
}
if (exp == iterator.next.data.exp){
System.out.println("\texp == iterator.next.data.exp");
iterator.next.data.coef += coef;
return;
}
iterator = iterator.next;
}
//if we get to this point then the list isn't empty
//and it doesn't fit inside the list, hence it must
//be added to the end of the list
System.out.println("list wasn't empty, didn't fit inside");
iterator.next = node;
return;
}
@Override
public String toString(){
Node iterator = poly;
String out = "";
if (poly.next == null){
return out;
}
while(iterator.next != null){
out += iterator.next.data.coef;
out += "*x^";
out += iterator.next.data.exp;
out += " + ";
iterator = iterator.next;
}
return out.substring(0, out.lastIndexOf('+'));
}
public Polynomial addPolynomial (Polynomial that){
Polynomial ret = new Polynomial();
Polynomial iterator = this;
return new Polynomial(addPolys(this.poly, that.poly));
}
public Node addPolys(Node p1, Node p2) {
// If P1 is null, just use P2
if (p1 == null) {
p1 = p2;
p2 = null;
}
// if P1 is still null, no P2 either so finished
if (p1 == null) return null;
Node ret = new Node();
// if P2 is null now, just one poly remains
if (p2 == null) {
ret.data = p1.data;
p1 = p1.next;
} else {
// do we combine nodes?
if (p1.data.exp == p2.data.exp) {
ret.data = new Term(p1.data.coef + p2.data.coef, p1.data.exp
+ p2.data.exp);
p1 = p1.next;
p2 = p2.next;
} else {
// we just copy a node
// make p1 the bigger exponent
if (p1.data.exp < p2.data.exp) {
Node t = p1;
p1 = p2;
p2 = t;
}
ret.data = p1.data;
p1 = p1.next;
}
}
ret.next = addPolys(p1, p2);
return ret;
}
/*
* Term
*/
private class Term implements Comparable{
int coef;
int exp;
public int getCoef() {
return coef;
}
public void setCoef(int coef) {
this.coef = coef;
}
public int getExp() {
return exp;
}
public void setExp(int exp) {
this.exp = exp;
}
public Term(int coef, int exp) {
this.coef = coef;
this.exp = exp;
}
public int compareTo(Object rhs){
Term that = (Term)rhs;
return this.exp - that.exp;
}
}//end Term
/*
* Node
*/
private class Node{
Term data;
Node next;
public Term getData() {
return data;
}
public void setData(Term data) {
this.data = data;
this.next = null;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Node() {
this.data = null;
this.next = null;
}
public Node(Term data, Node next) {
this.data = data;
this.next = next;
}
}//end Node
}
测试:
public class Polynomials {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Polynomial p1 = new Polynomial();
Polynomial p2 = new Polynomial();
Polynomial p0 = new Polynomial();
p1.addTerm(1, 2);
p1.addTerm(1, 4);
p1.addTerm(1, 6);
p2.addTerm(1, 1);
p2.addTerm(1, 3);
p2.addTerm(1, 5);
System.out.println("p1 = " + p1.toString());
System.out.println("p2 = " + p2.toString());
System.out.println("Adding p1 to p2...");
p0 = p1.addPolynomial(p2);
System.out.println(p0.toString());
}
}
答案 0 :(得分:1)
data
对象中的Node
个字段之一未初始化。我猜这是将数据设置为空。
Node node = new Node(term, null);
因此,当您执行p1.data.exponent
时,引用该对象的任何节点变量都将引发异常。
答案 1 :(得分:0)
我猜p1.data
或p2.data
是null
,因此您无法从中获取指数。
答案 2 :(得分:0)
在这部分
if (p1.data.exp == p2.data.exp)
属性'data'或'exp'为空。
我会给你一个建议,让你的代码更具保护性:
再见