Java 使用链表乘法多项式

Java 使用链表乘法多项式,java,linked-list,Java,Linked List,我试图实现一种方法,将两个多项式相乘为列表,并返回一个新列表作为结果。 以下是我目前掌握的情况: private PolyNode multiply(PolyNode first, PolyNode second) { PolyNode temp = new PolyNode(0, 0); PolyNode res = temp; while(first!=null) { while(second!=null) { //Multi

我试图实现一种方法,将两个多项式相乘为列表,并返回一个新列表作为结果。 以下是我目前掌握的情况:

private PolyNode multiply(PolyNode first, PolyNode second) {
    PolyNode temp = new PolyNode(0, 0);
    PolyNode res = temp;

    while(first!=null) {
        while(second!=null) {
            //Multiply first term with all the terms with all terms in the second.
            temp = new PolyNode(first.coef * second.coef, first.power + second.power);
            temp = temp.next;
            second = second.next;
        }
        //Move to next term
        first = first.next;
    }
    return res.next;
}
但是当我打印列表时,它没有显示任何内容。打印方法适用于我的加法、减法和一阶导数方法。只是我的乘法方法不起作用。 以下是我的打印方法:

void printPoly(PolyNode curr) {

    if(curr == null) {
        return;
    }

    while (curr != null) {
        if(curr.power == 0) {
            System.out.print(curr.coef + "");
            curr = curr.next;
        }
        else if(curr.power == 1){
            System.out.print(curr.coef + "x ");
            curr = curr.next;
        }
        else {
            System.out.print(curr.coef + "x^" + curr.power + " + ");
            curr = curr.next;
        }
    }
    System.out.println();
}
有什么想法吗?谢谢

PolyNode temp = new PolyNode(0, 0);
PolyNode res = temp;
然后

因此,
res
指向某个对象
temp
现在指向一些与res无关的新对象

然后

我假设这是
null

基本上你并没有对结果做任何事情
只是玩你扔掉的临时变量。

我能够完成我的方法,所以它就在这里

private PolyNode multiply(PolyNode first, PolyNode second) {
    PolyNode temp = new PolyNode(0, 0);
    PolyNode res = temp;

    while(first!=null) {
        while(second!=null) {
            //Multiply first term with all the terms with all terms in the second.
            temp.next = new PolyNode(first.coef * second.coef, first.power + second.power);
            temp = temp.next;
            second = second.next;
        }
        //Move to next term.
        first = first.next;
    }
    return res.next;
}
下面是我的打印列表方法:

void printPoly(PolyNode curr) {

    if(curr == null) {
        return;
    }

    while (curr != null) {
        if(curr.power == 0) {
            System.out.print(curr.coef + "");
            curr = curr.next;
        }
        else if(curr.power == 1){
            System.out.print(curr.coef + "x ");
            curr = curr.next;
        }
        else {
            System.out.print(curr.coef + "x^" + curr.power + " ");
            curr = curr.next;
        }
    }
    System.out.println();
}

非常感谢对最佳实践的反馈

首先要检查的是
curr
是否为空。谢谢!我添加了如果curr==null,则返回;我实际上是想通过打印一条大的警告消息来检查它,比如“嘿,你为什么要打印一个空对象?”
private PolyNode multiply(PolyNode first, PolyNode second) {
    PolyNode temp = new PolyNode(0, 0);
    PolyNode res = temp;

    while(first!=null) {
        while(second!=null) {
            //Multiply first term with all the terms with all terms in the second.
            temp.next = new PolyNode(first.coef * second.coef, first.power + second.power);
            temp = temp.next;
            second = second.next;
        }
        //Move to next term.
        first = first.next;
    }
    return res.next;
}
void printPoly(PolyNode curr) {

    if(curr == null) {
        return;
    }

    while (curr != null) {
        if(curr.power == 0) {
            System.out.print(curr.coef + "");
            curr = curr.next;
        }
        else if(curr.power == 1){
            System.out.print(curr.coef + "x ");
            curr = curr.next;
        }
        else {
            System.out.print(curr.coef + "x^" + curr.power + " ");
            curr = curr.next;
        }
    }
    System.out.println();
}