鱼C论坛

 找回密码
 立即注册
查看: 3393|回复: 4

关于一元多项式的相加(单链表实现)

[复制链接]
发表于 2018-10-20 19:17:19 | 显示全部楼层 |阅读模式
30鱼币

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));
        }
}

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-10-20 19:19:12 | 显示全部楼层
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)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-10-20 19:20:42 | 显示全部楼层
输入结果为上面,我不清楚哪里出了错,是不是attach方法在参数传递的时候出了问题,请各位帮忙看一下,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-10-20 23:32:32 | 显示全部楼层
Java 不会,但是 Node 的定义在哪里??
  1. $javac List.java
  2. List.java:5: error: cannot find symbol
  3.         public Node ReadPloy(){                        //输入节点
  4.                ^
  5.   symbol:   class Node
  6.   location: class List
  7. List.java:32: error: cannot find symbol
  8.         static void Attach(int c,int e,Node rear){     //把多项式的一个节点赋给rear节点的下一个
  9.                                        ^
  10.   symbol:   class Node
  11.   location: class List
  12. List.java:40: error: cannot find symbol
  13.         Node Add(Node a,Node b){                //两个多项式相加
  14.                  ^
  15.   symbol:   class Node
  16.   location: class List
  17. List.java:40: error: cannot find symbol
  18.         Node Add(Node a,Node b){                //两个多项式相加
  19.                         ^
  20.   symbol:   class Node
  21.   location: class List
  22. List.java:40: error: cannot find symbol
  23.         Node Add(Node a,Node b){                //两个多项式相加
  24.         ^
  25.   symbol:   class Node
  26.   location: class List
  27. List.java:78: error: cannot find symbol
  28.         static void PrintPloy(Node a){                //输出节点
  29.                               ^
  30.   symbol:   class Node
  31.   location: class List
  32. List.java:6: error: cannot find symbol
  33.                 Node head ;
  34.                 ^
  35.   symbol:   class Node
  36.   location: class List
  37. List.java:7: error: cannot find symbol
  38.                 Node rear = new Node();
  39.                 ^
  40.   symbol:   class Node
  41.   location: class List
  42. List.java:7: error: cannot find symbol
  43.                 Node rear = new Node();
  44.                                 ^
  45.   symbol:   class Node
  46.   location: class List
  47. List.java:13: error: cannot find symbol
  48.                                 Node n = new Node();
  49.                                 ^
  50.   symbol:   class Node
  51.   location: class List
  52. List.java:13: error: cannot find symbol
  53.                                 Node n = new Node();
  54.                                              ^
  55.   symbol:   class Node
  56.   location: class List
  57. List.java:20: error: cannot find symbol
  58.                                 Node n = new Node();
  59.                                 ^
  60.   symbol:   class Node
  61.   location: class List
  62. List.java:20: error: cannot find symbol
  63.                                 Node n = new Node();
  64.                                              ^
  65.   symbol:   class Node
  66.   location: class List
  67. List.java:33: error: cannot find symbol
  68.                 Node p = new Node();
  69.                 ^
  70.   symbol:   class Node
  71.   location: class List
  72. List.java:33: error: cannot find symbol
  73.                 Node p = new Node();
  74.                              ^
  75.   symbol:   class Node
  76.   location: class List
  77. List.java:41: error: cannot find symbol
  78.                 Node first,last;
  79.                 ^
  80.   symbol:   class Node
  81.   location: class List
  82. List.java:95: error: cannot find symbol
  83.                 Node a = list.ReadPloy();
  84.                 ^
  85.   symbol:   class Node
  86.   location: class List
  87. List.java:96: error: cannot find symbol
  88.                 Node b = list.ReadPloy();
  89.                 ^
  90.   symbol:   class Node
  91.   location: class List
  92. 18 errors
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-10-21 17:24:25 | 显示全部楼层
claws0n 发表于 2018-10-20 23:32
Java 不会,但是 Node 的定义在哪里??

不好意思,忘记发了

public class Node{
        Node next;
        int coef;
        int expon;
       
        public Node() {
                super();
        }       
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 13:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表