使用Node类将两个列表添加到一起

时间:2012-10-12 13:53:42

标签: java list nodes

我正在尝试创建一个数据结构来保存多项式。到目前为止,我已经创建了一个Node类来存储数据,如下所示:( Coefficient,exponent,link to next node) 尝试添加两个列表时,我遇到了问题。我的方法在每个列表中添加了第一个值,但随后终止。

这是我的代码:

    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){

}
 }

和主要方法:

    public class LinkedListsMain {


public static void main(String[] args) {

    Node head;
    Node head2;

    head = new Node(5,  1, new Node(7, 4,  new Node(9 ,5, new Node(12, new Node (15)))));
    head2 = new Node(3, 1, new Node(1, 4, new Node(29, 5)));
    Node.displayAll(head);
    Node.displayAll(head2);
    System.out.println("Length: " + Node.listLength(head));
    System.out.println("Length: " + Node.listLength(head2));
    Node.toPolynomial(head);
    Node.toPolynomial(head2);
    //Node.mergeLists(head, head2);
Node.addPolynomials(head, head2);
    System.out.println("Add completed");
    Node.toPolynomial(head);
}

}

有什么想法吗?

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

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

我希望程序输出 8X ^ 1 + 8×^ 4 + 38 ^ 5

1 个答案:

答案 0 :(得分:2)

从我的问题中得到的问题是,当你的外循环第二次执行时,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;
}