0

多項式を保持するデータ構造を作成しようとしています。これまでのところ、次のようなデータを格納する Node クラスを作成しました: (係数、指数、次のノードへのリンク) ただし、2 つのリストを一緒に追加しようとすると問題が発生します。私のメソッドは、各リストに最初の値を追加しますが、その後終了します。

ここに私のコードがあります:

    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);
}

}

何か案は?

より多くの情報を提供するために、リンクされたリストをより効率的なデータ構造として使用しています。2 つのリンクされたリストを入力し、対応する要素を一緒に追加しようとしています。コード全体を同封します!

例: 入力が 5x^1+7x^4+9x^5 + 3x^1+1x^4+29x^5 の場合

プログラムに 8x^1+8x^4+38^5 を出力させたい

4

1 に答える 1

2

あなたの質問から得たものから、問題は、外側のループが 2 回目に実行されたときに、n2すでに最後の位置に到達していることです。

n2なぜなら、内部ループで再び初期化されていないからです。だから、それはnull最初の繰り返しからです。したがって、外側のループは 1 回だけ実行されます。

以下の方法でメソッドを変更してみてください: -

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;
}
于 2012-10-12T14:06:46.613 に答える