gpt4 book ai didi

java - 使用节点类乘多项式

转载 作者:行者123 更新时间:2023-12-01 15:05:57 25 4
gpt4 key购买 nike

我创建了一个数据结构来使用自制的节点类来表示多项式。我有方法对两个长度为 n 的多项式进行加法和减法。我正在尝试添加一个乘法方法,该方法将返回两个多项式的乘积。我快完成这个问题了。此时,我尝试创建第三个链表(使用 Node 类)以存储和返回两个多项式的乘积。我的问题只是编码这个解决方案。我遇到空指针异常。

这是我的 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;
}

}

有什么想法吗?

最佳答案

请查看此部分:

**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);
z.data2 = x.addData2(y);
//System.out.println("Multiplied " + (x.data1 - y.data1) + " and " + y.data1);
**z = z.next;**
}
}

您正在迭代任意长度的节点列表,但新的 z 节点的长度为 1。您需要在迭代过程中向 z 添加节点。像这样的东西:

z.next = new Node();
z = z.next;

关于java - 使用节点类乘多项式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12954701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com