我已经使用自行开发的节点类创建了一个数据结构来重复多项式。我有方法来加和减两个长度为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;
}
}
任何想法?
答案 0 :(得分:1)
请看这一部分:
**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;