java - 在java中使用链表进行多项式加法

标签 java linked-list polynomials

这是我使用链接列表实现的两个多项式相加的实现。
例如,如果我想添加
3x^2+5^x+3 和 4x^3+5x+2

首先,我检查两个多项式中是否有相似的指数,如果有,我将它们的系数相加,并将指数附加到字符串中。
添加类似的指数然后使用字符串后,我将两个多项式中的剩余部分添加到最终结果中。

 public class Node2{
        int coef;
        int exp;
        Node2 next;
        Node2(int c,int e,Node2 n){
            coef=c;
            exp=e;
            next=n;
        }
        Node2(int c,int e){
            coef=c;
            exp=e;

        }


}

public class LinkedPoly{
    static String exponent="";
    Node2 head;
    Node2 current;

LinkedPoly(){
    head=null;

}
public void createList(int c,int e){
    head=new Node2(c,e,head);
}

public static LinkedPoly add(LinkedPoly list1,LinkedPoly list2){
    LinkedPoly addList=new LinkedPoly();

    Node2 temp1=list1.head;
            Node2 temp3=temp1;
    Node2 temp2=list2.head;
            Node2 temp4=temp2;
    while(temp1.next!=null){
        while(temp2.next!=null){
            if(temp1.exp==temp2.exp){

            addList.createList((temp1.coef+temp2.coef),temp1.exp);
            exponent+=temp1.exp;

            }
            temp2=temp2.next;
        }
        temp1=temp1.next;
        temp2=temp4;
    }
    String[] array=exponent.split("");
    for(int i=1;i<array.length;i++){

        while(temp3.next!=null){
            if(temp3.exp!=Integer.parseInt(array[i])){
                addList.createList(temp3.coef,temp3.exp);
            }
            temp3=temp3.next;
        }
        while(temp4.next!=null){
            if(temp4.exp!=Integer.parseInt(array[i])){
                addList.createList(temp4.coef,temp4.exp);
            }
            temp4=temp4.next;
        }
    }

    return addList;
}


public static void main (String args[]){
    LinkedPoly l1=new LinkedPoly();
    l1.createList(3,2);
    l1.createList(5,1);
    l1.createList(3,0);
    LinkedPoly l2=new LinkedPoly();
    l2.createList(4,3);
    l2.createList(5,1);
    l2.createList(2,0);

    LinkedPoly l3=add(l1,l2);
    System.out.println(l3.head.next.next.coef);
}

}

根据我的示例,指数字符串包括 1 和 0 ,但它只对 1 的系数求和。而且其余的加法也是错误的。

我看不出我错在哪里。另外,我怎样才能打印出最终的addList,以便我可以检查这个实现是否正常工作

最佳答案

这是一个有效的添加方法:

public static LinkedPoly add(LinkedPoly list1,LinkedPoly list2){
    LinkedPoly addList=new LinkedPoly();

    Node2 temp1=list1.head;
    Node2 temp3=temp1;
    Node2 temp2=list2.head;
    Node2 temp4=temp2;
    while(temp1.next!=null){
        while(temp2.next!=null){
            if(temp1.exp==temp2.exp){

                addList.createList((temp1.coef+temp2.coef),temp1.exp);
                exponent+=temp1.exp;

            }
            temp2=temp2.next;
        }
        temp1=temp1.next;
        temp2=temp4;
        addList.print();
    }
    String[] array=exponent.split("");


    while(temp3!=null){
        boolean exponentPresent = false;
        for(int i=1;i<array.length;i++){
            if(temp3.exp==Integer.parseInt(array[i])){
                exponentPresent = true;
            }
        }
        if (!exponentPresent) {
            addList.createList(temp3.coef,temp3.exp);
        }
        temp3=temp3.next;
    }
    while(temp4!=null){
        boolean exponentPresent = false;
        for(int i=1;i<array.length;i++){
            if(temp4.exp==Integer.parseInt(array[i])){
                exponentPresent = true;
            }
        }
        if (!exponentPresent) {
            addList.createList(temp4.coef,temp4.exp);
        }
        temp4=temp4.next;
    }


    return addList;
}

这是一个可以添加到 LinkedPoly 类的打印方法:

public void print() {
    current = head;
    System.out.print(current.coef + "x^" + current.exp);
    while (current.next != null) {
        current = current.next;
        System.out.print(" + " + current.coef + "x^" + current.exp);
    }
    System.out.println();
}

您的添加方法有两个主要问题。

问题#1。第一个是您对已包含的指数数组的循环位于对多项式链表节点的循环之外 - 它应该在内部。按照您之前的做法,您的流程如下:

a.从数组中取出已包含的指数之一 b.遍历每个多项式的所有项 C。如果其中任何一项的指数与 a 部分中的指数不匹配,请将其添加到结果中。 d.重复 a 部分,但使用下一个已包含的指数。

这种方法的问题在于,您只想在新项的指数与任何已包含的项不匹配时才向结果添加新项 - 而不仅仅是与其中一项不匹配。这就是为什么你的结果有所有这些额外的 x^1 项 - 当你的程序位于数组的“0”元素时,它会添加多项式的 x^1 项。

问题#2。您应该将 while (temp3.next!= null) 或 (temp4.next!=null) 替换为 (temp3!=null) 或 (temp4!=null)。否则,您的代码永远不会到达多项式的最后一个节点(它在最后一个节点之前停止,因为它正在检查最后一个节点之后是否存在“下一个”节点)。这就是为什么您的结果没有 x^3 和 x^4 项 - 您的循环在到达这些项之前就结束了。

需要考虑的一些事项

  1. 您使用了很多临时变量。尝试给它们提供更具描述性的名称,或者更好的是,找到一种不使用太多名称的方法。
  2. 我不确定为什么要将已使用的指数添加到“指数”字符串中,然后使用 split() 方法将其分解为数组。考虑从一开始就添加到数组中。
  3. 您的 add 方法可能会重组得更加简洁。您可以尝试以下操作:找到任意多项式中的最高指数,而不是查看两个多项式有哪些共同指数,处理这些指数,然后分别处理它们没有共同的指数。然后,循环遍历从 0 到该数字的所有指数度数。在每个循环中,循环遍历每个多项式,并将具有该指数的所有多项式的系数加在一起。这样,您的代码就会全部处于一个大循环中。
  4. 现在,您的代码无法确保多项式保持其项的顺序 - 无法阻止 x^2 项出现在 x^3 项之前,而 x^3 项出现在 x^1 项之前。考虑向 LinkedPoly 类添加 sort() 方法,在节点添加期间添加一些代码以确保多项式保持顺序,或者采用上面的建议#3,这将允许您在创建总和多项式时对其进行排序。或者,如果按顺序排列它们并不重要,请不要打扰:)

关于java - 在java中使用链表进行多项式加法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24394860/

相关文章:

求区间交集的Java算法

java - 使用 Book 对象的链表进行插入排序

打印链表后崩溃

c++ - 无法添加到开关中的链表

java - 每次运行应用程序时都会创建一个新数据库吗?

java - 创建简单的碰撞检测

使用链表创建多项式并将它们相加

r - R 中的多元多项式回归(预测)

java - 如何在递归中避免 StackOverFlow?

machine-learning - 机器学习中多项式和多项式回归有什么区别?