我正在做一个创建链表以形成两个多项式的项目,然后为用户提供了对列表进行数学运算的选项。我已经到了我正在阅读我的列表的位置,并使用堆栈来正确订购它。然后我想确保我的列表被正确读取并且到目前为止这么好,除了我的尾部没有被读入。或者也许它是我的while循环中的语法?
import java.util.*;
public class IntegerNode {
private int coefficient;
private int exponent;
private IntegerNode next;
public IntegerNode() {
coefficient = 0;
exponent = 1;
next = null;
}
public IntegerNode(int c, int e) {
coefficient = c;
exponent = e;
next = null;
}
public IntegerNode(int c, int e, IntegerNode n) {
coefficient = c;
exponent = e;
next = n;
}
public void setCoefficient(int newCoefficient) {
coefficient = newCoefficient;
}
public void setExponent (int newExponent) {
exponent = newExponent;
}
public int getExponent() {
return exponent;
}//end getItem
public int getCoefficient() {
return coefficient;
}//end getItem
public void SetNext (IntegerNode nextNode) {
next = nextNode;
}//end setNext
public IntegerNode getNext () {
return next;
}//end getNext
}//end class IntegerNode
class Driver {
public static void main(String[] args) {
//First polynomial:
Scanner sc = new Scanner(System.in);
System.out.println("Enter coefficient: ");
int coeff = sc.nextInt();
System.out.println("Enter exponent: ");
int exp = sc.nextInt();
Stack<IntegerNode> p1 = new Stack<IntegerNode>();
IntegerNode n = new IntegerNode(coeff, exp);
p1.push(n);
while (exp != 0){
System.out.println("Enter coefficient: ");
coeff = sc.nextInt();
System.out.println("Enter exponent: ");
exp = sc.nextInt();
IntegerNode m = new IntegerNode(coeff, exp);
p1.push(m);
}
IntegerNode tail = p1.pop();
IntegerNode head = null;
while(!p1.empty()){
head = p1.pop();
head.SetNext(tail);
System.out.println(head.getCoefficient() + " to " + tail.getCoefficient());
tail = head;
}
IntegerNode cur = tail;
while(!(cur.getNext()).equals(null)){
System.out.println(cur.getCoefficient()+" "+cur.getExponent());
System.out.println("NEXTNODE: " + cur.getNext().getCoefficient());
cur = cur.getNext();
}
//Read in two integers, check each time if exponent is zero
//create linked list in reverse order of input
//Use stack to create LIFO list
//Repeat above, but for second polynomial
//Prompt user to choose which operation
//Prompt user to specify what order if subtracted
//iterate through p1
//for every element in p1:
//iterate through p2 and see if that element matches the current element
//in p1. If so, then add their coefficients
//Store results as you go
//Output the results and indicate what operation was done
// TODO Auto-generated method stub
}
这是错误:
Exception in thread "main" java.lang.NullPointerException
at Driver.main(IntegerNode.java:85)
那将是我最后一次循环。有什么想法吗?
答案 0 :(得分:0)
while(!(cur.getNext()).equals(null))
而不是上面尝试一次:
while(!(cur.getNext())==null)
永远不能在对象引用上调用.equals()方法。这是一个方法,就像其他方法一样,所以如果对象引用真的为null,它只会抛出一个NullPointerException。
试试这个:
do{
} while(((cur.getNext())!=null))
答案 1 :(得分:0)
首先,当你这样做时:
IntegerNode tail = p1.pop();
......你应该确保有东西可以弹出。
第二,这:
while(!(cur.getNext()).equals(null))
...可能导致您的问题。您正在与null
对象进行比较。不能这样做。使用此:
while(cur.getNext() != null)
就打印你的最后一个值而言,你得到了你的最后一个节点,然后你就离开了它们,所以你永远不会打印最后一个节点上的内容。我会使用do{}while()
。