自作のノード クラスを使用して多項式を表現するためのデータ構造を作成しました。長さ n の 2 つの多項式を加算および減算するメソッドがあります。2 つの多項式の積を返す乗算メソッドを追加しようとしています。私はほとんど問題を終えました。この時点で、2 つの多項式の積を格納して返すために、(Node クラスを使用して) 3 番目の連結リストを作成しようとしています。私の問題は、このソリューションをコーディングすることだけです。null ポインター例外が発生しています。
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 int addData1(Node n2){
data1 = data1 + n2.data1;
return data1;
}
public int addData2(Node n2){
data2 = data2 + n2.data2;
return data2;
}
public void subData1(Node n2){
data1 = data1 - n2.data1;
}
public int multiplyData1(Node n2){
data1 = data1*n2.data1;
return 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;
}
//copy list [Recursive method found on website StackOverflow.com]
public Node copyData1( Node p ) {
if( p == null )
return null;
else
return new Node(p.data1, copyData1(p.next));
}
public Node copyData2( Node p ) {
if( p == null )
return null;
else
return new Node(p.data2, copyData2(p.next));
}
//===============================================================
public static void toPolynomial(Node head){
int order = Node.listLength(head);
int i = 0;
int increment = Node.numOfNonZeroData2(head);
for( ; head != null; head = head.next){
if(head.data2 != 0 && head.data1 != 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 );
return n1;
}
}
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);
}
}
}
System.out.println("Add completed");
return x;
}
public static Node subtractPolynomials(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.subData1(y);
System.out.println("Subtracted " + (x.data1 - y.data1) + " and " + y.data1);
}
}
}
System.out.println("Subtract completed");
return x;
}
public static Node multiplyPolynomials(Node n1, Node n2){
Node x = n1;
Node y = n2;
Node z = new Node();
for(x = n1; x != null; x = x.next){
for(y = n2; y != null; y = y.next){
z.data1 = x.multiplyData1(y); // error is here
z.data2 = x.addData2(y);
//System.out.println("Multiplied " + (x.data1 - y.data1) + " and " + y.data1);
z = z.next;
}
}
System.out.println("Multiplication completed");
return x;
}
}
何か案は?