关于一元多项式的相加(单链表实现)
import java.util.Scanner;
public class List {
public Node ReadPloy(){ //输入节点
Node head ;
Node rear = new Node();
head = rear;
Scanner in = new Scanner(System.in);
int num = in.nextInt();
for(int i=0;i<num;i++ ){
if(head==null){
Node n = new Node();
n.coef = in.nextInt();
n.expon = in.nextInt();
n.next = null;
head= n;
rear = n;
}else{
Node n = new Node();
n.coef = in.nextInt();
n.expon = in.nextInt();
n.next = null;
rear.next = n;
rear = n;
}
}
rear.next= null;
head = head.next;
return head;
}
static void Attach(int c,int e,Node rear){ //把多项式的一个节点赋给rear节点的下一个
Node p = new Node();
p.coef = c;
p.expon = e;
p.next = null;
rear.next = p;
rear = p;
}
Node Add(Node a,Node b){ //两个多项式相加
Node first,last;
last = null;
first = last;
int sum;
while(a!=null&&b!=null){
if(a.expon>b.expon){
Attach(a.coef, a.expon, last);
a = a.next;
}else if(a.expon<b.expon){
Attach(b.coef, b.expon, last);
b = b.next;
}else{
sum = a.coef+b.coef;
if(sum!=0){
Attach(sum, a.expon, last);
a = a.next;
b = b.next;
}
}
}
while(a==null){
for(b = b.next;b!=null;b=a.next){
Attach(b.coef, b.expon, last);
}
}
while(b==null){
for(a=a.next;a!=null;a=a.next){
Attach(a.coef, a.expon, last);
}
}
last.next = null;
first = first.next;
return first;
}
static void PrintPloy(Node a){ //输出节点
boolean flag = false;
if(a==null){
System.out.println("0 0");
}
while(a!=null){
if(!flag){
flag = true;
}else{
System.out.print(" ");
}
System.out.print(a.coef+" "+a.expon);
a = a.next;
}
}
public static void main(String[] args) {
List list = new List();
Node a = list.ReadPloy();
Node b = list.ReadPloy();
PrintPloy(list.Add(a, b));
}
} 1 2 3
1 2 3
Exception in thread "main" java.lang.NullPointerException
at Test01.List.Attach(List.java:36)
at Test01.List.Add(List.java:58)
at Test01.List.main(List.java:101)
输入结果为上面,我不清楚哪里出了错,是不是attach方法在参数传递的时候出了问题,请各位帮忙看一下, Java 不会,但是 Node 的定义在哪里??$javac List.java
List.java:5: error: cannot find symbol
public Node ReadPloy(){ //输入节点
^
symbol: class Node
location: class List
List.java:32: error: cannot find symbol
static void Attach(int c,int e,Node rear){ //把多项式的一个节点赋给rear节点的下一个
^
symbol: class Node
location: class List
List.java:40: error: cannot find symbol
Node Add(Node a,Node b){ //两个多项式相加
^
symbol: class Node
location: class List
List.java:40: error: cannot find symbol
Node Add(Node a,Node b){ //两个多项式相加
^
symbol: class Node
location: class List
List.java:40: error: cannot find symbol
Node Add(Node a,Node b){ //两个多项式相加
^
symbol: class Node
location: class List
List.java:78: error: cannot find symbol
static void PrintPloy(Node a){ //输出节点
^
symbol: class Node
location: class List
List.java:6: error: cannot find symbol
Node head ;
^
symbol: class Node
location: class List
List.java:7: error: cannot find symbol
Node rear = new Node();
^
symbol: class Node
location: class List
List.java:7: error: cannot find symbol
Node rear = new Node();
^
symbol: class Node
location: class List
List.java:13: error: cannot find symbol
Node n = new Node();
^
symbol: class Node
location: class List
List.java:13: error: cannot find symbol
Node n = new Node();
^
symbol: class Node
location: class List
List.java:20: error: cannot find symbol
Node n = new Node();
^
symbol: class Node
location: class List
List.java:20: error: cannot find symbol
Node n = new Node();
^
symbol: class Node
location: class List
List.java:33: error: cannot find symbol
Node p = new Node();
^
symbol: class Node
location: class List
List.java:33: error: cannot find symbol
Node p = new Node();
^
symbol: class Node
location: class List
List.java:41: error: cannot find symbol
Node first,last;
^
symbol: class Node
location: class List
List.java:95: error: cannot find symbol
Node a = list.ReadPloy();
^
symbol: class Node
location: class List
List.java:96: error: cannot find symbol
Node b = list.ReadPloy();
^
symbol: class Node
location: class List
18 errors claws0n 发表于 2018-10-20 23:32
Java 不会,但是 Node 的定义在哪里??
不好意思,忘记发了
public class Node{
Node next;
int coef;
int expon;
public Node() {
super();
}
}
页:
[1]