Java 使用节点类将两个列表添加到一起

Java 使用节点类将两个列表添加到一起,java,list,nodes,Java,List,Nodes,我试图创建一个数据结构来保存多项式。到目前为止,我已经创建了一个节点类来存储如下数据:(系数、指数、链接到下一个节点) 不过,我在尝试将两个列表添加到一起时遇到了问题。我的方法在每个列表中添加第一个值,但随后终止 下面是我的代码: public class Node{ //Fields public int data1; public int data2; public Node next; //constructors public Node(){ data1 = 0;

我试图创建一个数据结构来保存多项式。到目前为止,我已经创建了一个节点类来存储如下数据:(系数、指数、链接到下一个节点) 不过,我在尝试将两个列表添加到一起时遇到了问题。我的方法在每个列表中添加第一个值,但随后终止

下面是我的代码:

    public class Node{

//Fields
public int data1;
public int data2;
public Node next;



//constructors
public Node(){
    data1 = 0;
    data2 = 0;
    next = null;
}


public Node(int d1){
    data1 = d1;
    data2 = 0;
    next = null;
}

public Node(int d1, Node n){
    data1 = d1;
    data2 = 0;
    next = n;
}

public Node(int d1, int d2){
    data1 = d1;
    data2 = d2;
    next = null;
}
public Node(int d1,int d2, Node n){
    data1 = d1;
    data2 = d2;
    next = n;
}

//Methods

//Fetch data
public int getData1(){
    return data1;
}

public int getData2(){
    return data2;
}

//store Data
public void setData1(int d){
    data1 = d;
}

public void setData2(int d){
    data2 = d;
}

public void addData1(Node n2){
    data1 = data1 + n2.data1;
}

//getNext
public Node getNext(){
    return next;
}

//Get data of next node
public int getNextData1(){
    return next.data1;
}

public int getNextData2(){
    return next.data2;
}

//Store Link
public void setNext(Node n){
    next = n;
}

public boolean containsLink(){
    if(this.next != null){
        return true;
    }else{
        return false;
    }
}

public static void displayAll(Node head){
    for( ; head != null; head = head.next ){
        System.out.printf("%d, %d\n", head.data1, head.data2);
    }
}

public static int numOfNonZeroData1(Node n){
    int count = 0; 
    for( ; n != null ; n = n.next ){
        if(n.data1 != 0){
            count++;
        }
    }
    return count;
}

public static int numOfNonZeroData2(Node n){
    int count = 0; 
    for( ; n != null ; n = n.next ){
        if(n.data2 != 0){
            count++;
        }
    }
    return count;
}

public static int listLength(Node head){
    int counter = 0;
    for(; head!=null; head = head.next){
        counter++;
    }
    return counter;
}

public static void toPolynomial(Node head){
    int order = Node.listLength(head);
    int i = 0;
    int increment = Node.numOfNonZeroData2(head);
    //sortList(head);
    for( ; head != null; head = head.next){
        if(head.data2 != 0){
            if(i >= 0 && i < order){
                System.out.printf("%dx^%d", head.data1, head.data2);
                i++;
                if(i < increment && head.data1 >= 0){
                    System.out.print("+");      //case integer is positive
                }else if(i != increment && head.data1 <= 0){
                    System.out.println(" ");    //case integer is negative
                }
            }

        }
    }
    System.out.println();
}




public static Node mergeLists(Node n1, Node n2){
    if ( n1 == null) 
        return n2;
    else if ( n2 == null) 
        return n1;
    else {
        n1.next = mergeLists( n1.next, n2 );     // Note how we exchange p and q here
        return n1;
    }
}









public static Node addPolynomials(Node n1, Node n2){
    for( ; n1 != null; n1 = n1.next){
        for(; n2 != null; n2 = n2.next){
            if(n1.getData2() == n2.getData2()){
                n1.addData1(n2);
                System.out.println("added " + (n1.data1 - n2.data1) + " and " + n2.data1 );
            }

        }

    }
    return n1;
}

public static void subtractPolynomials(Node n1, Node n2){

}

public static void multiplyPolynomials(Node n1, Node n2){

}
 }
}

有什么想法吗

为了提供更多信息,我使用链表作为更有效的数据结构。我正在输入两个链接列表,并尝试将相应的元素添加到一起。我将附上我的全部代码

例如:如果输入为 5x^1+7x^4+9x^5+3x^1+1x^4+29x^5

我想让程序输出
8x^1+8x^4+38^5根据我从你的问题中得到的信息,问题是当你的外循环第二次执行时,
n2
已经到达了它的最后位置

因为,您没有在内部循环中再次初始化
n2
。 因此,从第一次迭代开始,它是
null
。所以你的外环只能运行一次

尝试使用以下方法更改您的方法:-

public static Node addPolynomials(Node n1, Node n2) {
    Node x = n1;
    Node y = n2;

    for(x = n1; x != null; x = x.next){
        for(y = n2; y != null; y = y.next){
            if(x.getData2() == y.getData2()){
                x.addData1(y);
                System.out.println("added " + (x.data1 - y.data1) + " and " + y.data1);
            }

        }
    }

    return x;
}

我会使用一系列的协同效率。它更快、更简单。你为什么要实现自己的链表而不是使用ArrayList这样的内置集合类?你提供的信息非常少。很难说你到底想做什么。你能编辑你的问题并添加两个输入和所需输出的示例吗?@SimonHillary。不客气。但下一次当你发布此类问题时。请发布输入和预期输出。这样更容易理解。:)行。为任何不礼貌的行为道歉,我是新来的:P@SimonHillary. 没问题。这与礼仪无关。只是为了更好地了解您的问题,以便您能够得到更好的解决方案:)
public static Node addPolynomials(Node n1, Node n2) {
    Node x = n1;
    Node y = n2;

    for(x = n1; x != null; x = x.next){
        for(y = n2; y != null; y = y.next){
            if(x.getData2() == y.getData2()){
                x.addData1(y);
                System.out.println("added " + (x.data1 - y.data1) + " and " + y.data1);
            }

        }
    }

    return x;
}